简体   繁体   中英

How to do grouping in elasticsearch with searchkick rails

I have articles data indexed to elastic as follows.

{
"id": 1011,
"title": "abcd",
"author": "author1"
"status": "published"
}

Now I wanted to get all the article id grouped by status .

Result should someway look like this

{
"published": [1011, 1012, ....],
"draft": [2011],
"deleted": [3011]
}

NB: I tried normal aggs ( Article.search('*',aggs: [:status], load: false).aggs ) , it just giving me the count of each items in, I want ids in each item instead

@Crazy Cat You can transform you query in this way:

  1. sort(Inc/Dec order) your response from ES over field "status".
  2. Only Ask ES query to return only ID Field and status.
  3. Now the usage of sorting would be it would sort your response to like this: [1st N results of "deleted" status, then N+1 to M results to "draft" and then M+1 to K results to "published"].

Now the advantages of this technique:

  1. You will get flagged ids field of every document over which you can apply operations in you application.
  2. Your query would be light weight as compared to Aggs query. This way you would also get the metdata of every document ike docId of that document.

Now the Disadvantages:

  1. You would always have to give a high upper bound of your page size, but You can also play around with count coming in the metadata.
  2. Might take a bit more of network size as it returns redundant status in every document.

I Hope this redesign of your query might be helpful to you.

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