Determining 'size' in [_source ] [includes] query

Hi guys, I am just wondering if it possible to set the size for a particular field when we query.

The following here is my query. (Apologies for the mess, the query is done in the python elasticsearch API).

GET /index_name/_search
{

  "size": 10, 
  "query": {
      "bool": {
      "must": [],
      "filter": ["terms": {'employee_id.keyword': ['x'],
      "should": [],
      "must_not": []
    }
  },
    "_source": {"includes": ["employee_id","title_en","section_id","date_joined_at"]
    }
 "sort": [{"date_joined_at": {"order": "desc"}}
}

I want to have 'section_id' in _source limited to size 2. So the thing is section_id returns a ton of ids (like around 100). I just want to return thefirst two values

Welcome to our community! :smiley:
We aren't all guys though.

size is a query level thing.

What are you trying to achieve?

Hi Mark,

thanks for replying. I have cleaned up the code above and elaborated on what I really wanted.

So if you see the code:
"_source": {"includes": ["employee_id","title_en","section_id","date_joined_at"]

section_id here returns a lot of id (like around 100), when retrieving I want to limit this to just the first two ids. Is it possible to do so?

So you want the first two section_id values, from the top 10 hits?

What sort of field is that section_id, is it an array?

Yep you are right.
Yes it is an array.
The image there is an example of what the return looks like. I just want the top 2

image

The issue here is that arrays in Elasticsearch (and in JSON more generally) are not ordered at all. So the first two this time might be different next time, there's no guarantee.

Yeap no worries, I just want to cap it at 2. Since there are tons of ids it makes the search process slow

Ok, honestly I don't know of a way to do this sorry. Hopefully someone else can chime in with a suggestion.

Yeah it's very tricky :pensive: I have tried all possible stuff before posting it here.
But thank you very much for helping me

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.