0
votes

In my index I have a mapping that contains the locations property of type "nested". This property contains an array of locations. Each location is an object with the following structure:

{
    "id": string,
    "type": string
}

On my website I want to select multiple locations and find all documents that have ANY of these locations I selected. I was able to search by one location but I have to modify my query somehow to search for many locations.

I have the following index set up:

"mappings": {
    "users": {
        "properties": {
            "locations": {
                "type": "nested",
                "properties": {
                    "id": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "type": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            },
            "id": {
                "type": "long"
            }
        }
    }
}

Example document:

{
    "locations": [
        {
            "id": "UA",
            "type": "country"
        },
        {
            "id": "RU",
            "type": "country"
        },
        {
            "id": "OC",
            "type": "continent"
        }
    ]
}

My query that searches for users by location:

{
    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "path": "locations",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "match": { "locations.id": "RU" }
                                    },
                                    {
                                        "match": { "locations.type": "country" }
                                    }
                                ]
                            }
                        }
                    }
                }   
            ]
        }
    }
}

EDIT: I also want to retreive documents which have locations.type equal to continent. In SQL it would look like

WHERE (id IN ('UA', 'RU') AND type = 'country') 
OR (id IN ('EU', 'OC', 'NA') AND type = 'continent')
1

1 Answers

1
votes

for multiple value search use Terms and for OR condition use should (see here):

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "locations",
            "query": {
              "bool": {
                "should": [
                  {
                    "bool": {
                      "must": [
                        {
                          "terms": {
                            "locations.id": [
                              "UA",
                              "RU"
                            ]
                          }
                        },
                        {
                          "term": {
                            "locations.type": "country"
                          }
                        }
                      ]
                    }
                  },
                  {
                    "bool": {
                      "must": [
                        {
                          "terms": {
                            "locations.id": [
                              "EU",
                              "OC"
                            ]
                          }
                        },
                        {
                          "term": {
                            "locations.type": "continent"
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}