尽管有文档记录,但没有关于如何使用索引时间和不同的查询时间分析器创建索引的工作示例。
我只想在搜索中应用同义词过滤器。如果我指定分析器名称,我可以测试分析器,但是如果没有分析器名称,它就不会检测到默认的名称。
有什么不对的?
"settings": {
"index": {
"analysis": {
"filter": {
"synonym": {
"type": "synonym",
"synonyms": [
"testword => otherword"
]
}
},
"analyzer": {
"default_search": {
"filter": [
"lowercase",
"asciifolding",
"synonym"
],
"tokenizer": "standard"
},
"default_index": {
"filter": [
"lowercase",
"asciifolding"
],
"tokenizer": "standard"
}
}
}
注意两个不同的分析器,名为default_search
和default_index
。根据文档,这些应该被作为缺省值来提取。因此,如果我执行'testword‘搜索,它将搜索’其他词‘。
我可以确认在索引类型上设置了默认分析器名称:
"myIndex": {
"mappings": {
"myType": {
"index_analyzer": "default_index",
"search_analyzer": "default_search",
"properties": ...
我执行一个测试搜索:
在不指定分析器/myIndex/_analyze/?pretty=true&text=testword
的情况下进行调用(希望它能够按照配置的方式拾取default_search
)
{
"tokens" : [ {
"token" : "testword",
"start_offset" : 0,
"end_offset" : 9,
"type" : "<ALPHANUM>",
"position" : 1
} ]
}
用专用分析器myIndex/_analyze/?analyzer=default_search&pretty=true&text=testword
调用
{
"tokens" : [ {
"token" : "otherword",
"start_offset" : 0,
"end_offset" : 9,
"type" : "SYNONYM",
"position" : 1
} ]
}
在示例搜索中,索引包含一个字段值为“otherword”的项。下面的查询不返回结果,搜索“他物”返回所需的项。POST myIndex/_search
"query": {
"multi_match": {
"query": "testword",
"analyzer": "default_search",
"fields": [
"name"
]
}
}
发布于 2016-02-03 11:54:31
因为您要调用_analyze
端点,所以这不是搜索。实际上,您正在向ES发出请求,并要求它分析给它的令牌流,就好像ES正在索引这些令牌一样,所以默认的索引分析器就会启动。
如果要尝试default_search
分析器,则需要向_search
端点发送请求。
您需要使用otherword
索引文档,然后使用/_search?q=testword
进行搜索,您将看到default_search
分析器启动。
更新
您没有正确地定义默认分析器,即您需要在settings
(而不是在mappings
中)中这样做,并正确命名它们(即default
而不是default_index
)。
下面是我用于测试的索引:
curl -XPUT localhost:9200/myindex -d '{
"settings": {
"index": {
"analysis": {
"filter": {
"synonym": {
"type": "synonym",
"synonyms": [
"testword => otherword"
]
}
},
"analyzer": {
"default_search": {
"filter": [
"lowercase",
"asciifolding",
"synonym"
],
"tokenizer": "standard"
},
"default": {
"filter": [
"lowercase",
"asciifolding"
],
"tokenizer": "standard"
}
}
}
}
},
"mappings": {
"myType": {
"properties": {
"name": {
"type": "string"
}
}
}
}
}'
下面是我为测试建立索引的示例文档:
curl -XPUT localhost:9200/myindex/myType/1 -d '{
"name": "otherword"
}'
然后,在不指定任何分析器的情况下使用以下查询,我可以找到上面的文档:
curl -XPOST localhost:9200/myindex/myType/_search -d '{
"query": {
"multi_match": {
"query": "testword",
"fields": [
"name"
]
}
}
}'
响应:
{
...
"hits" : {
"total" : 1,
"max_score" : 0.30685282,
"hits" : [ {
"_index" : "myindex",
"_type" : "myType",
"_id" : "1",
"_score" : 0.30685282,
"_source":{"name":"otherword"}
} ]
}
}
https://stackoverflow.com/questions/35175291
复制相似问题