【问题标题】:Elasticsearch match query with partial text match具有部分文本匹配的 Elasticsearch 匹配查询
【发布时间】:2016-08-12 03:21:02
【问题描述】:

关于弹性搜索的新手问题。我已经设置了 elasticsearch lucene 索引并使用搜索包含某些术语的名称,例如

search_response = es.search(index = 'sample', body = {'query':{'match':{'first_name':"JUST"}}})

这不会返回名称“JUSTIN”,但以下查询会

search_response = es.search(index = 'sample', body = {'query':{'match':{'first_name':"JUSTIN"}}})

我做错了什么? “匹配”查询不应该返回包含该术语的记录吗? 谢谢。

【问题讨论】:

标签: elasticsearch elasticsearch-py


【解决方案1】:

处理这种需求的最佳方法是创建一个使用edgeNGram token filter 的自定义分析器。忘记通配符并在查询字符串中使用*,这些都不如edgeNGram 方法。

因此,您必须先像这样创建索引,然后将数据重新索引到其中。

curl -XPUT http://localhost:9200/sample -d '{
    "settings": {
        "analysis": {
            "filter": {
                "prefixes": {
                    "type": "edgeNGram",
                    "min_gram": 1,
                    "max_gram": 15
                }
            },
            "analyzer": {
                "my_analyzer": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "filter": ["lowercase", "prefixes"]
                }
            }
        }
    },
    "mappings": {
        "your_type": {
            "properties": {
                "first_name": {
                    "type": "string",
                    "analyzer": "my_analyzer",
                    "search_analyzer": "standard"
                }
            }
        }
    }
}'

那么在索引first_name: JUSTIN时,你会得到以下索引标记:jjujusjustjustijustin,基本上都是JUSTIN的前缀。

然后,您将能够使用第二个查询进行搜索,并实际找到您期望的内容。

search_response = es.search(index = 'sample', body = {'query':{'match':{'first_name':'JUST'}}})

【讨论】:

  • 这有帮助吗?您需要更多信息吗?
猜你喜欢
  • 1970-01-01
  • 2018-11-01
  • 1970-01-01
  • 2018-03-17
  • 2015-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-09
相关资源
最近更新 更多