Best way to find the length of array in elasticsearch


(Amit Khanduri) #1
> "asst_re": [
> {
> "rep_type": 345,
> "repo_id": 7144220
> }
> ,
> {
> "rep_type": 545,
> "repo_id": 7084555
> }]

I have to filter the "asst_re" array on the basis of length of the array.
What is the best and optimise way to do and achieve this thing in elasticsearch 6.2 ?
Thanks in Advance !!


(ddorian43) #2

best way: is to have the array length in another field at index-time
other way: do a script query (search for this has been done many times)


(Amit Khanduri) #3

@ddorian43 thanks for the answer !!!:+1:
I have one more query, what is the best way to count the number of words in a field ?
Ex - "name":"John doe bishop" , So the name field have 3 words, what should be the query for this type of filtering ?


(ddorian43) #4

the same as with array-length


(Amit Khanduri) #5

Thanks @ddorian43 !!!
What is the query for that using script ? Can you help me out for this ?:grinning:


(ddorian43) #6

Sorry you gotta search for it. But that is SLOW!
Better to always store as separate field "array_length": 5


(David Pilato) #7

Agreed with @ddorian43

Specifically because you asked for:

What is the best and optimise way


(Amit Khanduri) #8

@ddorian43 thanks for the justification !!:+1:
@dadoonet Yeah correct :grinning:


(system) #9

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