[英]Elasticsearch: How to search with different analyzers?
I'm using my custom analyzer autocomplete_analyzer
with filter edgeNGram
. 我正在使用带有过滤器
edgeNGram
自定义分析器autocomplete_analyzer
。 So mapping looks like: 因此映射看起来像:
"acts_as_taggable_on_tags" : {
"acts_as_taggable_on/tag" : {
"properties" : {
"name" : {
"type" : "string",
"boost" : 10.0,
"analyzer" : "autocomplete_analyzer"
}
}
}
}
When I search using query_string
, it works like autocomplete. 当我使用
query_string
搜索时,它就像自动完成一样。 For example, query "lon" returns ["lon", "long", "london",...]. 例如,查询“ lon”返回[“ lon”,“ long”,“ london”,...]。
But sometimes I need exact matching. 但是有时候我需要精确匹配。 How can I get just one exactly matching word "lon"?
我怎样才能得到一个完全匹配的单词“ lon”? Can I use another analyzers (eg
simple
or standard
) when I making a search query? 进行搜索查询时可以使用其他分析仪(例如,
simple
或standard
)吗?
I think you will need to store the data in 2 separate fields. 我认为您需要将数据存储在2个单独的字段中。 One would contain the tokens necessary for doing autocomplete queries, the other for the full search queries.
一个将包含执行自动完成查询所需的令牌,另一个将包含完整搜索查询。
If you have only one field with the tokens [lon, lond, londo, london]
then if you search against this field you cannot say "please only match the token london
as this is the full word/longest token". 如果只有一个带标记的字段
[lon, lond, londo, london]
则如果您搜索该字段,则不能说“请只匹配标记london
因为这是完整的单词/最长的标记”。
You can have the 2 fields done nicely for you with the multi-field. 通过多字段,您可以很好地完成这两个字段。 Take a look at the elasticsearch docs on multi-field .
看一下multi-field上的elasticsearch文档 。 The 'official' documentation is pretty good on this section, please check it out!
“官方”文档在本节中相当不错,请查看!
I would probably do this: 我可能会这样做:
"acts_as_taggable_on_tags" : {
"acts_as_taggable_on/tag" : {
"properties" : {
"name" : {
"type" : "multi_field",
"fields" : {
"name" : {
"type" : "string",
"boost" : 10.0
},
"autocomplete" : {
"type" : "string",
"analyzer" : "autocomplete_analyzer",
"boost" : 10.0
}
}
}
}
}
}
for autocomplete queries: 对于自动完成查询:
"query": {
"query_string": {
"query" : "lon",
"default_field": "name.autocomplete"
}
}
for normal queries: 对于普通查询:
"query": {
"query_string": {
"query" : "lon",
"default_field": "name"
}
}
Note the difference in "default_field". 注意“ default_field”中的区别。
The other answer given would not work; 给出的其他答案将无效。 the different
search_analyzer
would mean that a search for 'london' would not get tokenized into lon, lond, londo, london
. 不同的
search_analyzer
意味着对“ london”的搜索不会被标记为lon, lond, londo, london
。 But this would not stop a search for 'lon' from matching documents with a name of 'london' which I think is what you want. 但这不会阻止您从匹配文件中搜索名称为“ london”的“ lon”(我认为这是您想要的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.