Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch query_string query with multiple default fields

Tags:

I would like to avail myself of the feature of a query_string query, but I need the query to search by default across a subset of fields (not all, but also not just one). When I try to pass many default fields, the query fails. Any suggestions?

Not specifying a specific field in the query, so I want to search three fields by default:

{
    "query": {
        "query_string" : {
            "query" : "some search using advanced operators OR dog",
            "default_field": ["Title", "Description", "DesiredOutcomeDescription"]
        }
    }
}
like image 977
Michael Davidson Avatar asked Dec 27 '17 22:12

Michael Davidson


1 Answers

If you want to create a query on 3 specific fields as above, just use the fields parameter.

{
    "query": {
        "query_string" : {
            "query" : "some search using advanced operators OR dog",
            "fields": ["Title", "Description", "DesiredOutcomeDescription"]
        }
    }
}

Alternatively, if you want to search by default on those 3 fields without specifying them, you will have to use the copy_to parameter when you set up the mapping. Then set the default field to be the concatenated field.

PUT my_index

{
  "settings": {
    "index.query.default_field": "full_name" 
  },
  "mappings": {
    "my_type": {
      "properties": {
        "first_name": {
          "type": "text",
          "copy_to": "full_name" 
        },
        "last_name": {
          "type": "text",
          "copy_to": "full_name" 
        },
        "full_name": {
          "type": "text"
        }
      }
    }
  }
}

I have used this and don't recommend it because the control over the tokenization can be limiting, as you can only specify one tokenizer for the concatenated field.

Here is the page on copy_to.

like image 127
Daryl Van Sittert Avatar answered Sep 22 '22 13:09

Daryl Van Sittert