elasticsearchpyelasticsearch

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!


Solution

  • 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