如何在弹性搜索中对多个单词的通配符查询进行部分匹配? [英] How to partial match wildcard query with multiple words in elastic search?

查看:73
本文介绍了如何在弹性搜索中对多个单词的通配符查询进行部分匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设文档文本为这是一个示例文本,用于显示搜索结果的工作方式,而我的查询字符串是 mple tex .我希望此查询字符串与文本匹配,因为它与示例文本部分匹配.

Suppose the doc text is This is a sample text to show how the search results works and my query string is mple tex. I want this query string to match the text since it partially match against sample text.

如何在弹性搜索中做到这一点?ES中可以进行很多搜索吗?

How do I do that in elastic search? is much search possible in ES?

我目前使用的是match_phrase查询

What I have currently used is the match_phrase query

"query": {"match_phrase": {"description": "mple tex"}},

推荐答案

您要查找的内容称为中缀搜索,可以使用

What you are looking for is called infix search and can be easily accomplished using the ngram token filter, Please see below complete working example, which is better than doing the wildcard searches and doesn't use the query string which is not recommended for search boxes, as mentioned in official docs.

由于任何无效语法都会返回错误,因此我们不建议您使用query_string查询搜索框.

Because it returns an error for any invalid syntax, we don’t recommend using the query_string query for search boxes.

索引定义

{
    "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"
}

**搜索您的文字**

** Search for your text**

{
   "query": {
      "match": {
         "title": {
            "query": "mple tex"
         }
      }
   }
}

带有分数的搜索结果

 "max_score": 0.9168506,
        "hits": [
            {
                "_index": "my-index",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.9168506,
                "_source": {
                    "title": "This is a sample text to show how the search results works"
                }
            }
        ]

注意:请参考我的详细答案,以了解如何根据功能和非功能要求以及他们的交易选择最佳自动完成方法.关闭

这篇关于如何在弹性搜索中对多个单词的通配符查询进行部分匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆