Adam Lambert
Adam Lambert

Reputation: 1421

Elasticsearch 7 - prevent fields from being searchable

I know I can prevent fields from being indexed by setting the enabled mapping to false. This does work as expected but I am concerned that some of these fields will be needed in the future.

In my use case, I am searching for a product SKU of t-shirt-small-red and while ES7 does return the correct results, it also returns everything else as I am indexing the created_at and updated_at fields with dates 2020-02-08 00:00:00.

At least for now, I have no use for searching these within my app so I would like a way to exclude these from any search while keeping them indexed for future use. I am guessing I may want to perform filtering or aggregation on these in the future.

I know I can limit the search to just a single field but that does not work for this either. I need the search to work across every field apart from these 2 date fields.

Upvotes: 0

Views: 38

Answers (1)

Adam Lambert
Adam Lambert

Reputation: 1421

As in one of the comments, you can exclude them from the fields part of the query. I was not using fields before so it actually means specifying all the fields explicitly.

Additionally, I found that specifying the field type as date also ensures they did not show up as false positives in the search.

Upvotes: 1

Related Questions