假设文档文本为
This is a sample text to show how the search results works
,而我的查询字符串是mple tex
。我希望此查询字符串与文本匹配,因为它与sample text
部分匹配。
如何在弹性搜索中做到这一点? ES中可以进行很多搜索吗?
我目前使用的是match_phrase查询
"query": {"match_phrase": {"description": "mple tex"}},
答案 0 :(得分:0)
尝试使用description.keyword而不只是description。 description.keyword字段将使用原始格式或字符串格式的文本。这将帮助您获得完全匹配或通配符匹配。
如果已将自定义分析器应用于字段描述。然后在映射中应用关键字类型。
类似于上面的建议,但这很简单。
{
"query": {
"wildcard": {
"description": {
"value": "*ronavir*"
}
}
}
}
以上适用于单个单词。像它获取单词匹配冠状病毒的结果。但是对于多个单词,您需要对文本进行ngram分析。
答案 1 :(得分:0)
您可以使用query_string
查询来创建一个复杂的搜索,其中包括通配符,跨多个字段的搜索等等。要了解更多信息,请参阅Query String
搜索查询:
{
"query":{
"query_string":{
"default_field":"description",
"query":"*mple tex*"
}
}
}
搜索结果:
"hits": [
{
"_index": "my-index",
"_type": "_doc",
"_id": "1",
"_score": 2.0,
"_source": {
"description": "This is a sample text to show how the search results works"
}
}
答案 2 :(得分:0)
您要查找的内容称为中缀搜索,可以使用ngram token filter轻松完成。请参见下面的完整工作示例,这比进行通配符搜索更好,并且不使用{{1} },不建议使用搜索框,如官方文档中所述。
由于它针对任何无效语法返回错误,因此不建议 使用query_string查询搜索框。
索引定义
query string
为示例文档建立索引
{
"settings": {
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "ngram",
"min_gram": 1,
"max_gram": 10
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter"
]
}
}
},
"index.max_ngram_diff": 10
},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "standard"
}
}
}
}
**搜索您的文字**
{
"title":"This is a sample text to show how the search results works"
}
带有分数的搜索结果
{
"query": {
"match": {
"title": {
"query": "mple tex"
}
}
}
}
请查看详细的opster's blog on various autocomplete approaches,并且在此也提及了它们的权衡方法。