Changing the default operator for search API

Hi,
I am new to Elastic search. The default_operator for search is configured as OR in the engine and I would like to change it to AND at the back end so that the client API does not need to change. Can someone please help.

Hi,

the default value ("OR") for "default_operator" is set directly in Elasticsearch and you can configure it only on a query level. In other words: You have to change the client API to include the parameter "default_operator" if you want "AND" instead.

Daniel

Thanks Dan. What happens when someone wants to do exact search usong the phrase within quotes, does the client needs to parse the keyword and remove AND from the operator or elastic search will make it a no op?

Hi,

I am not entirely sure if I understand you correctly. I assume you currently use a query string query using the URI search API. You cannot configure the default operator in Elasticsearch itself (e.g. in elasticsearch.yml) but as I said only on query level. Are you able to add the request parameter default_operator in your application? That should solve your problem.

Daniel

I can add that but my issue is how would you do an exact phrase search like looking for "Donald is winning" Will you expect the client API to remove that default_operator when a user sends a phrase search.

Hi,

if you have do a phrase query the default_operator is not involved. Consider this example:

POST /query-test/docs/1
{
   "content": "Donald is winning"
}

POST /query-test/docs/2
{
   "content": "Donald is not winning"
}

POST /query-test/docs/3
{
   "content": "Donald Duck is never lucky"
}

# matches only the doc id 1 (phrase query)
GET /query-test/_search?q="Donald is winning"

# matches all documents due to implicit operator default_operator=OR
GET /query-test/_search?q=Donald Duck

# match only doc id 3 (explicit AND operator)
GET /query-test/_search?q=Donald Duck&default_operator=AND

In short: There is no need to parse anything in the query string yourself.

Daniel