Search code examples
elasticsearchelasticsearch-6

Python Elasticsearch - Look in multiple fields for exact match


I have two fields field_1 and field_2. I have a to find exact match for my query in either field_1 or field_2. multi_match is not giving me exact matches. And constant_score does not support multi_match.

The elasticsearch version I'm using:

Version: 6.3.0, Build: default/rpm/424e937/2018-06-11T23:38:03.357887Z, JVM: 1.8.0_171


Solution

  • After a lot of reading the docs and trying different queries this worked for me:

    query = {
       "query" : {
          "constant_score" : { 
             "filter" : {
                "bool" : {
                  "should" : [
                     { "term" : {"field_1" : "<search query>"}},
                     { "term" : {"field_2" : "<search query>"}}
                  ]
               }
             }
          }
       }
    }