Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch query on array index

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.

like image 243
Thanigs Avatar asked Oct 20 '22 19:10

Thanigs


1 Answers

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

like image 113
Vineeth Mohan Avatar answered Oct 23 '22 21:10

Vineeth Mohan