How to filter field values ​​in ElasticSearch?

Based on the ElasticSearch document below:

{
    "_id": "3330481",
    "_type": "user",
    "_source": {
        "id": "3330481",
        "following": ["1", "2", "3", ... , "15000"]
    }
}

For this list of users ["50", "51", "52"] I want to check who the user with the ID 3330481 is following, since she is following 15,000 users, I don’t want to get the whole list, and then check it locally.

Is there a way to get only relevant users?

+4
source share
2 answers

I'm not sure if you can get a sub-array from a document.

However, one way to achieve this is to use nested fields. You can change the field diagram followinglike nestedas shown below.

 {
        "id": "3330481",
        "following": [
              {"userId":"1"}, {"userId":"2"},{"userId": "3"}, ... ]
 }

inner_hits, , .

{
    "query" : {
        "nested" : {
            "path" : "following",
            "query" : {
                "terms" : {"following.userId" : ["50","51","53"]}
            },
            "inner_hits" : {
                "size":0 // fetches all 
            }
        }
    }
}
+2

["50", "51", "52"] , minimum_should_match bool. :

{
  "query" : {
    "filtered" : {
      "filter" : {
        "bool": {
          "must": {
            "terms": {
              "following": ["50", "51", "52"],
              "minimum_should_match": 3
            }
          }
        }
      }
    }
  }
}
0

All Articles