3

I am trying to create an elastic search query to match multiple fields inside of an object inside of an array.

For example, the Elastic Search structure I am querying against is similar to the following:

"hits": [
            {
                "_index": "titles",
                "_type": "title",
                ...
                "_source": {
                    ...
                    "genres": [
                        {
                            "code": "adventure",
                            "priority": 1
                        },
                        {
                            "code": "action",
                            "priority": 2
                        },
                        {
                            "code": "horror",
                            "priority": 3
                        }
                    ],
                    ...
                },
                ...
        ]

And what I am trying to do is match on titles with specific genre/priority pairings. For example, I am trying to match all titles with code=action and priority=1, but my query is returning too many results. The above title is hit during this example due to the fact that the genre list contains both a genre with code=action AND another genre that matches priority=1. My query is similar to the following:

    "query": {
        "bool": {
            "filter": [
                {
                    "bool": {
                        "must":[
                            {"term": {
                                    "genres.code": {
                                        "value": "action",
                                        "boost": 1.0
                                    }
                            }},
                            {"term": {
                                    "genres.priority": {
                                        "value": 1,
                                        "boost": 1.0
                                    }
                            }}
                        ]
                    }                    
                },
                ...
            }

Is there any way to form the query in order to match a title with a single genre containing both priority=1 AND code=action?

1 Answer 1

8

I have recreated your problem. I added the following mapping

PUT titles 
{
  "mappings": {
    "title": {
      "properties": {
        "author": {
          "type": "text"
        },
        "genres": {
          "type": "nested"
        }
      }
    }
  }
}

Then I added values to the index. This was what was inserted

  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "titles",
        "_type": "title",
        "_id": "2",
        "_score": 1,
        "_source": {
          "author": "Author 1",
          "genres": [
            {
              "code": "adventure",
              "priority": 2
            },
            {
              "code": "action",
              "priority": 3
            },
            {
              "code": "horror",
              "priority": 1
            }
          ]
        }
      },
      {
        "_index": "titles",
        "_type": "title",
        "_id": "1",
        "_score": 1,
        "_source": {
          "author": "Author 2",
          "genres": [
            {
              "code": "adventure",
              "priority": 3
            },
            {
              "code": "action",
              "priority": 1
            },
            {
              "code": "horror",
              "priority": 2
            }
          ]
        }
      },
      {
        "_index": "titles",
        "_type": "title",
        "_id": "3",
        "_score": 1,
        "_source": {
          "author": "Author 3",
          "genres": [
            {
              "code": "adventure",
              "priority": 3
            },
            {
              "code": "action",
              "priority": 1
            },
            {
              "code": "horror",
              "priority": 2
            }
          ]
        }
      }
    ]
  }

My query is:

GET titles/title/_search 
{
  "query": {
    "nested": {
      "path": "genres",
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "genres.code": {
                  "value": "horror"
                }
              }
            },
            {
              "term": {
                "genres.priority": {
                  "value": 1
                }
              }
            }
          ]
        }
      }
    }
  }
}

The query returns

        "_source": {
          "author": "Author 1",
          "genres": [
            {
              "code": "adventure",
              "priority": 2
            },
            {
              "code": "action",
              "priority": 3
            },
            {
              "code": "horror",
              "priority": 1
            }
          ]
        }

This title is the only one that has code = 'horror' and priority = 1.

Sign up to request clarification or add additional context in comments.

3 Comments

This is the correct solution. The keyword I was looking for for both mapping and search was nested.
I was looking for this nested solution for a while and never get there. Thanks a lot Leandro to help me out with this!
Excellent level of detail. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.