How to search for exact matches in either one of two fields?


(Rajeev R Menon) #1

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


(David Pilato) #2

Could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.


(Rajeev R Menon) #3

I'm new to elasticsearch. What is a recreation script?


(David Pilato) #4

I guess you did not click on the link in my response. Otherwise you'd have seen a typical script that can be used to reproduce a problem:

DELETE index
PUT index/_doc/1
{
  "foo": "bar"
}
GET index/_search
{
  "query": {
    "match": {
      "foo": "bar"
    }
  }
}

(Christian Dahlqvist) #5

What is the mapping of your fields?


(Rajeev R Menon) #6

This is the mapping of my fields. Say my search term is "foo", I want results in which either "field_1" is "foo" or "field_2" is "foo". Also I want exact matches

{
  "test-index": {
    "mappings": {
      "names": {
        "properties": {
          "field_1": {
            "type": "keyword"
          },
          "field_2": {
            "type": "keyword"
          },
          "title": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

(Christian Dahlqvist) #7

Have you tried using term queries for the two fields within a bool clause?


(Rajeev R Menon) #8

Yes. It's not working. I used

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

(system) #9

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.