5

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"]
        }
    }
}

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that by using copy_to, you may miss out on the "field-length norm" factor for determining relevancy. Relying on the _all field (deprecated in ES 6.0 btw) suffers from the same effect. More info: elastic.co/guide/en/elasticsearch/guide/2.x/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.