1

I have one index called user_info, under the index there are two types called user and events. Right now I need to query out 200 users and each user with 10 events. So I did the query as following(sodu script): for searching user:

{size:200,"match:{"user_address":"CA Sf"}}

And then I issued 200 quires to Event type: for search event:

{size:10,"term":{"userid":"id1"}}
{size:10,"term":{"userid":"id2"}}
....
{size:10,"term":{"userid":"id200"}}

Any idea please to optimize the above queries, thanks!

2 Answers 2

3

You can use below query:

{
"query": {
  "terms": {
     "userid": [
        // list of userId
     ]
  }
},
"aggs": {
  "group By user": {
     "terms": {
        "field": "userId"
     },
     "aggs": {
        "Group By event": {
           "terms": {
              "field": "event_type"
           },
           "aggs": {
              "top10": {
                 "top_hits": {
                    "size": 10
                 }
              }
           }
        }
      }
    }
  }
} 

This query would be slow if you run this for all 200 userIds. You better divide list of userIds into chunks and then run the above query.

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

2 Comments

great answer, a following question please, actually I have three event types: talk_event, action_event, history_event. each event should return 10 records base on each userId, should I use multiple search API to combine these types query.
Sorry, I may confused you, I mean there are three Index _types, such as: user_info/talk_event, user_info/action_event, user_info/history_event. the Index name is user_info.
1

Look at the terms queries

something like this

{size:200, "terms" : { "userid" : ["id1", "id2", ... ,"id200"]}}

2 Comments

As per the question, there has to be 10 events corresponding to every userId. Providing size 200 doesn't gurantee that each userId will have 10 records. Correct me If I am wrong.
Agreed, if that's really what the op wants your solution is the way to go

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.