简体   繁体   中英

elasticsearch filter on nested array

lets say records have city field as an array of city names. 

records ex:

  record 1:
    {
      cities : [
        {name: city1},
        {name : city2},
        {name : city3}
      ]
    }
    record 2:
    {
      cities : [
        {name: city2},
        {name : city3},
        {name : city4}
      ]
    }
    record 3:
    {
      cities : [
        {name: city3},
        {name : city4},
        {name : city5}
      ]
    }

requirement: My filter criteria is to fetch the records matches with city1 or city2 or city3 but since the record 1 matches all 3 it should come first and record 2 matches 2 so it should come 2nd and record 3 matches only one so it should come last.

You don't have to use the nested data-type as you don't have the nested properties or complex object, its very simple and easy to achieve.

Working example

Index mapping

{
    "mappings": {
        "properties": {
            "cities": {
                "type": "text"
            }
        }
    }
}

Index sample docs

{
    "cities": [
        "tel-aviv", "bangalore", "sf"
    ]
}

{
    "cities": [
        "tel-aviv"
    ]
}

{
    "cities": [
        "sf"
    ]
}

Search query

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "cities": "tel-aviv"
                    }
                },
                {
                   "match": {
                        "cities": "bangalore"
                    }
                },
                 {
                   "match": {
                        "cities": "sf"
                    }
                }
            ] 
        }
    }
}

And search result with proper expected result and score

 "hits": [
            {
                "_index": "cities",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.850198,
                "_source": {
                    "cities": [
                        "tel-aviv",
                        "bangalore",
                        "sf"
                    ]
                }
            },
            {
                "_index": "cities",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.9983525,
                "_source": {
                    "cities": [
                        "tel-aviv"
                    ]
                }
            },
            {
                "_index": "cities",
                "_type": "_doc",
                "_id": "3",
                "_score": 0.6133945,
                "_source": {
                    "cities": [
                        "sf"
                    ]
                }
            }
        ]

Adding another answer with nested bool queries :

Index Mapping:

{
    "mappings": {
        "properties":{
        "Cities": {
            "type": "nested",
            "dynamic": "true"
        }
    }}
}

Index Data:

{
  "Cities": [
    {
      "id": 1,
      "city": "Bangalore"
    },
    {
      "id": 2,
      "city": "Hyderabad"
    },
    {
      "id": 3,
      "city": "Delhi"
    }
  ]
}
{
  "Cities": [
    {
      "id": 1,
      "city": "Bangalore"
    },
    {
      "id": 2,
      "city": "abc"
    },
    {
      "id": 3,
      "city": "Def"
    }
  ]
}

Search Query:

{
  "query": {
    "bool": {
      "should": [
        {
          "nested": {
            "path": "Cities",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "Cities.city": "Bangalore"
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "Cities",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "Cities.city": "Hyderabad"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

Search Result:

 "hits": [
            {
                "_index": "nested-63806067",
                "_type": "_doc",
                "_id": "1",
                "_score": 3.297317,   <-- note this
                "_source": {
                    "Cities": [
                        {
                            "id": 1,
                            "city": "Bangalore"
                        },
                        {
                            "id": 2,
                            "city": "Hyderabad"
                        },
                        {
                            "id": 3,
                            "city": "Delhi"
                        }
                    ]
                }
            },
            {
                "_index": "nested-63806067",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.6486585,   <-- note this
                "_source": {
                    "Cities": [
                        {
                            "id": 1,
                            "city": "Bangalore"
                        },
                        {
                            "id": 2,
                            "city": "abc"
                        },
                        {
                            "id": 3,
                            "city": "Def"
                        }
                    ]
                }
            }
        ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM