我有一个查询,应该搜索小写字母。
实际上,我只有一个带有小写过滤器的index_analyzer,但我还想添加search_analyzer以便进行不区分大小写的搜索。
"analysis": {
"analyzer" : {
"DefaultAnalyzer": {
"type": "custom",
"tokenizer": "whitespace",
"filter": [
"lowercase"
],
"char_filter": ["punctuation"]
},
"MyAnalyzer": {
"type": "custom",
"tokenizer": "first_letter",
"filter": [
"lowercase"
]
},
所以我只是想在映射中添加与search_analyzer相同的分析器
"index_analyzer": "DefaultAnalyzer",
"search_analyzer": "DefaultAnalyzer",
"dynamic" : false,
"_source": { "enabled": true },
"properties" : {
"name": {
"type": "multi_field",
"fields": {
"name": {
"type": "string",
"store": true
},
"startletter": {
"type": "string",
"index_analyzer": "MyAnalyzer",
"search_analyzer": "MyAnalyzer",
"store": true
}
}
},
这样做,如果我手动查询Elastic Search
curl -XGET host:9200/my-index/_analyze -d 'Test'
我看到查询词正确地小写
{
"tokens": [
{
"token": "test",
"start_offset": 0,
"end_offset": 4,
"type": "<ALPHANUM>",
"position": 1
}
]
}
但是从代码执行
虽然我希望独立于案例获得相同的结果。
在代码中,我只是使用术语过滤器创建查询,就像这样
{
"filter": {
"term": {
"name.startletter": "O"
}
},
"size": 10000,
"query": {
"match_all": {}
}
}
我做错了什么?为什么我没有得到任何结果?
最佳答案
问题是您正在使用术语过滤器。术语过滤器不会分析正在使用的文本:
Term Filter
Filters documents that have fields that contain a term (not analyzed). Similar to term query, except that it acts as a filter.
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-term-filter.html
由于它不会分析,因此不会使用您定义的分析器。
通常,您希望对未分析的字段使用术语过滤器和查询。将您的过滤器类型更改为将在查询期间进行分析的类型。
关于elasticsearch - ElasticSearch search_analyzer已应用,但未返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25161896/