5

How do I query/filter by index of an array in elasticsearch?

I have a document like this:-

PUT /edi832/record/1
{
    "LIN": [ "UP", "123456789" ]
}

I want to search if LIN[0] is "UP" and LIN[1] exists.

Thanks.

1 Answer 1

3

This might look like a hack , but then it will work for sure. First we apply token count type along with multi field to capture the the number of tokens as a field. So the mapping will look like this -

{
    "record" : {
        "properties" : {
            "LIN" : {
                "type" : "string",
                "fields" : {
                    "word_count": {
                        "type" : "token_count",
                        "store" : "yes",
                        "analyzer" : "standard"
                    }
                }
            }
        }
    }
}

LINK - http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-core-types.html#token_count

So to check if the second field exists , its as easy as checking if this field value is more than or equal to 2. Next we can use the token filter to check if the token "up" exists in position 0. We can use the scripted filter to check this. Hence a query like below should work -

{
  "query": {
    "filtered": {
      "query": {
        "range": {
          "LIN.word_count": {
            "gte": 2
          }
        }
      },
      "filter": {
        "script": {
          "script": "for(pos : _index['LIN'].get('up',_POSITIONS)){ if(pos.position == 0) { return true}};return false;"
        }
      }
    }
  }
}

Advanced scripting - http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-advanced-scripting.html

Script filters - http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-script-filter.html

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

3 Comments

Looks good. But if the author of the post has no problem with changing the mapping of the index, I'd say that approach plus the bool query is more efficient.
How can we make sure the token 'up' comes first , in the approach you have recommended ?
Thanks @Vineeth Mohan. I have accepted this answer. It provides enough pointers to build up on this.

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.