0
votes

I am trying implement a search which only returns exact matches for terms which are stored in a list. So e.g.

karl : ['health care', dental care]

I store it using the keyword analyser, since I am only interested in exact matches

doc = {
    "settings" : {
        "number_of_shards" : 1,
        "number_of_replicas": 0,
    },
    "mappings" : {
        "users" : {
            "properties" : {
               "plan_list" : { 
                    "type" : "text", 
                    "fields" : {
                        "raw" : {    
                            "type" : 'keyword'
                        }
                    } 
                }
            }
        }
    }
}

Now I would like to create search queries like

health care AND dental care

I found the query_string approach in the docs https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html , which is exactly what I need, except that I want the query string not be analysed. If I implement it like this

doc = { 
    "query": {
        "bool" : {
            "must" : [{
                "query_string": {
                    "query": health care AND dental care,                           
                    "fields": ["plan_list.raw"]
                }
            }]
        }
    }
}

it does

(plan_list.raw:health OR plan_list.raw:care) AND (plan_list.raw:dental OR plan_list.raw:care)

which is not what I want. I do not want it to split health care into two words. I want

(plan_list.raw:(health care)) AND (plan_list.raw:(dental care))

I know that query_string provides the default_operator, but putting that to 'and' does not solve the problem, since the word has not been analysed and therefore is not stored as two words.

I know that for the example above I can just create a bool/should or bool/must query with a term search, but my real case is more complicated and I would like to keep the possibility of directly using an AND/OR in a string. Any ideas?

1

1 Answers

2
votes

You have to put exact phrases in double quotes like \"health care\" AND \"dental care\" . Loot at the query below:

GET {index_name}/_search
{
  "query": {
    "query_string": {
      "default_field": "plan_list.raw",
      "query": "\"health care\" OR \"dental care\""
    }
  }
}

You care read about this section of official documentation:

where the author field contains the exact phrase "john smith"

author:"John Smith"