Josh
Josh

Reputation: 4816

elasticsearch - field filterable but not searchable

Using elastic 2.3.5. Is there a way to make a field filterable, but not searchable? For example, I have a language field, with values like en-US. Setting several filters in query->bool->filter->term, I'm able to filter the result set without affecting the score, for example, searching for only documents that have en-US in the language field.

However, I want a query searching for the term en-US to return no results, since this is not really an indexed field for searching, but just so I can filter.

Can I do this?

Upvotes: 0

Views: 1467

Answers (1)

Hugodby
Hugodby

Reputation: 1183

ElasticSearch use an _all field to allow fast full-text search on entire documents. This is why searching for en-US in all fields of all documents return you the one containing 'language':'en-US'. https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-all-field.html

You can specify "include_in_all": false in the mapping to deactivate include of a field into _all.

PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "title": { 
          "type": "string"
        },
        "country": {
          "type": "string"
        },
        "language": { 
          "type": "string",
          "include_in_all": false
        }
      }
    }
  }
}

In this example, searching for 'US' in all field will return only document containing US in title or country. But you still be able to filter your query using the language field. https://www.elastic.co/guide/en/elasticsearch/reference/current/include-in-all.html

Upvotes: 3

Related Questions