elasticsearch - field filterable but not searchable

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-25 09:14:17

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/38980506/elasticsearch-field-filterable-but-not-searchable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!