1
votes

I have an index with a text field.

"state": {
  "type": "text"
}

Now suppose there are two data.

"state": "vail"
and
"state": "eagle vail"

For one of my requirements,
- I need to do a term level query, such that if I type "vail", the search results should only return states with "vail" and not "eagle vail".

But another requirement for different search on the same index,
- I need to do a match query for full text search, such that if I type "vail", "eagle vail" should display as well.

So my question is, how do I do both term level and full text search in this field, as for doing a term level query, I would have to set it as "keyword" type such that it wont be analyzed.

1

1 Answers

1
votes

You can use "multi-field" feature to achieve this. Here is a mapping:

{
    "mappings": {
        "my_type": {
            "properties": {
                "state": {
                    "type": "text",
                    "fields": {
                        "raw": {
                            "type": "keyword"
                        }
                    }
                }
            }
        }
    }
}

In this case state will act as text field (tokenized) whereas state.raw will be keyword (single-token). When indexing a document you should only set state. state.raw will be created automatically.