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.

1

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.