6

Given the ElasticSearch document below:

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

For a given user list ["50", "51", "52"] I want to check which ones are followed by the user with id 3330481. Since she is following 15000 users, I don't want to get the whole list and then check it locally.

Is there any way to retrieve only the relevant users?

2 Answers 2

4

I am not sure whether you can fetch a sub-array from a doc.

However, one way of achieving this is by using nested fields. You can change the schema of following field to be nested as below.

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

Then you can use inner_hits to retrieve your matching elements in the array as below.

{
    "query" : {
        "nested" : {
            "path" : "following",
            "query" : {
                "terms" : {"following.userId" : ["50","51","53"]}
            },
            "inner_hits" : {
                "size":0 // fetches all 
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I used inner_hits as suggested to solve my problem. However, I am not sure about 'size=0'. It didn't return any results unless I remove it.
1

if you just want to retrieve all user's flollowing with ["50", "51", "52"] without affect score, you can use minimum_should_match with bool query. Example:

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

1 Comment

I am not interested in other users. I just want to check for the given user.

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.