maq
maq

Reputation: 1226

search array value by index in elasticsearch

i have two documents stored in elasticsearch database

one is

{
   q :["python" , "foo"]
}

and other is

{
   q :["python" , "moo","foo"]
}

now my problem is to search document in which python is on index 0 and foo is on index 1 how to acheive this? i have read about bool query of elasticsearch but could not figure out how to use it kindly help!

Upvotes: 1

Views: 85

Answers (1)

ChintanShah25
ChintanShah25

Reputation: 12672

You can do this with scripting, Try this

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "script": {
                "script": "_source.q[0] == \"python\" && _source.q[1] == \"foo\" "
              }
            }
          ]
        }
      }
    }
  }
}

Although document fields are suggested since they are loaded into memory and are fast to access but they are not ordered. Refer this and hence in this case we need to use _source

Upvotes: 1

Related Questions