Elasticsearch:查找字符串匹配 [英] Elasticsearch: Find substring match

查看:1131
本文介绍了Elasticsearch:查找字符串匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要执行这两个词精确匹配和部分字/子字符串匹配。例如,如果我搜索男人的剃须刀那么我应该能够找到男人的剃须刀的结果。但万一情况下,我搜索连接的剃须刀,那么也应该是我能找到男人的剃须刀的结果。
我使用以下设置和映射:

I want to perform both exact word match and partial word/substring match. For example if I search for "men's shaver" then I should be able to find "men's shaver" in the result. But in case case I search for "en's shaver" then also I should be able to find "men's shaver" in the result. I using following settings and mappings:

指数设置:

PUT /my_index
{
    "settings": {
        "number_of_shards": 1, 
        "analysis": {
            "filter": {
                "autocomplete_filter": { 
                    "type":     "edge_ngram",
                    "min_gram": 1,
                    "max_gram": 20
                }
            },
            "analyzer": {
                "autocomplete": {
                    "type":      "custom",
                    "tokenizer": "standard",
                    "filter": [
                        "lowercase",
                        "autocomplete_filter" 
                    ]
                }
            }
        }
    }
}

映射:

PUT /my_index/my_type/_mapping
{
    "my_type": {
        "properties": {
            "name": {
                "type":            "string",
                "index_analyzer":  "autocomplete", 
                "search_analyzer": "standard" 
            }
        }
    }
}

插入记录:

POST /my_index/my_type/_bulk
{ "index": { "_id": 1            }}
{ "name": "men's shaver" }
{ "index": { "_id": 2            }}
{ "name": "women's shaver" }

查询:

1。要通过精确匹配搜索 - >男人的

POST /my_index/my_type/_search
{
    "query": {
        "match": {
            "name": "men's"
        }
    }
}

上面的查询返回男人的剃须刀的返回结果。

Above query returns "men's shaver" in the return result.

2。要通过局部字匹配搜索 - >连接的

POST /my_index/my_type/_search
{
    "query": {
        "match": {
            "name": "en's"
        }
    }
}

上面的查询不返回任何东西。

Above query DOES NOT return anything.

我也尝试下面​​的查询

I have also tried following query

POST /my_index/my_type/_search
{
    "query": {
        "wildcard": {
           "name": {
              "value": "%en's%"
           }
        }
    }
}

仍然没有得到任何东西。
我想这是因为指数edge_ngram式过滤器,它是不是能够找到部分字/ sbustring匹配的。
我试着正克型过滤器,以及,但它明显放缓搜索了很多东西。

Still not getting anything. I figured it is because of "edge_ngram" type filter on Index which is not able to find "partial word/sbustring match". I tried "n-gram" type filter as well but it is slowing down the search alot.

请建议我如何实现这两个词组excact匹配和部分匹配词组用同一指标的设置。

Please suggest me how to achieve both excact phrase match and partial phrase match using same index setting.

推荐答案

要搜索部分现场和详细match.You更好地定义字段不analyzed.And然后使用通配符查询。[没有分析会降低CPU使用率在索引]

TO search for partial field and exact match.You better define the fields as not analyzed.And then use wildcard query.[not analyzed will reduce cpu usage During indexing.]

要提到的一些字段不分析<一href=\"http://stackoverflow.com/questions/23238972/how-to-do-mapping-while-indexing-in-elasticsearch/23241214#23241214\">refer这

To mentions some field as not analyzed refer this

要使用通配符查询,追加*上串的两端要搜索

To use wildcard query append * on both ends of string you are searching for

POST /my_index/my_type/_search
{
"query": {
    "wildcard": {
       "name": {
          "value": "*en's*"
       }
    }
}
}

要与使用区分大小写后,使用自定义分析仪具有的小写的过滤器和关键字标记生成器

To use with case insensitivity, Use a custom analyzer with a lowercase filter and keyword tokenizer.

自定义分析:

"custom_analyzer": {
            "tokenizer": "keyword",
            "filter": ["lowercase"]
        }

小写的搜索字符串

如果你得到的搜索字符串的 ASD ..变化的 * ASD *

If you get search string as AsD.. change to *asd*

希望它可以帮助..!

这篇关于Elasticsearch:查找字符串匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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