弹性搜索中的通配符搜索的ngram [英] ngram for wildcard search in Elastic Search

查看:178
本文介绍了弹性搜索中的通配符搜索的ngram的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向最终用户提供搜索类型,因为它们更像是sqlserver。我能够为给定的sql方案实现ES查询:

I am trying to provide the search to end user with type as they go which is is more like sqlserver. I was able to implement ES query for the given sql scenario:

 select * from table where name like '%peter tom%' and type != 'xyz 

在ES中,我使用ngram tokenizer以达到所需的结果: / p>

In ES i used ngram tokenizer in order to achieve the desired results :

PUT sample
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_ngram_analyzer": {
          "tokenizer": "my_ngram_tokenizer"
        }
      },
      "tokenizer": {
        "my_ngram_tokenizer": {
          "type": "nGram",
          "min_gram": "2",
          "max_gram": "15"
        }
      }
    }
  },
  "mappings": {
    "typename": {
      "properties": {
        "name": {
          "type": "string",
          "fields": {
            "search": {
              "type": "string",
              "analyzer": "my_ngram_analyzer"
            }
          }
        },
        "type": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  }
}

{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "name.search": "peter tom"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "type": "xyz"
          }
        },
        {
          "match": {
            "type": "abc"
          }
        }
      ]
    }
  }
}

所以如果我的文档行类似于

So if my document rows are like

name                              type
peter tomson                      efg
Peter tomson robert simson        efg

以上查询只显示两个文件,但是当我尝试输入Peter sims或Pete r simson它不返回第二个文件,除非我输入彼得tomson罗伯特sims或彼得tomson罗伯特simson。基本上我必须在彼得之后和simson之前输入所有以下单词以获得第二个文档。有没有办法获得部分匹配的第二个文档。我可以使用查询匹配和AND操作,但仍然是完全匹配的单词。我正在寻找部分匹配,如彼得sims应该给我第二行的文件。
谢谢

The above query only shows be both the documents but when i try to type in Peter sims or Peter simson it doesnt return the second document unless i type in Peter tomson robert sims or Peter tomson robert simson .So basically i have to type all the following words after Peter and before simson to get to the second document . Is there any way to get the second document with partial matching .I can use the query match with and "AND" operation but that is still on exact match of the word.I am looking for partial match like Peter sims should give me second row of the documents . Thanks

推荐答案

我发现查询的答案本人发布解决方案供其他用户进一步参考: p

I found the answer to the query myself posting the solution for further reference for other users :

{
    "settings": {
        "analysis": {
            "analyzer": {
                "autocomplete": {
                    "tokenizer": "whitespace",
                    "filter": [
                        "lowercase",
                        "autocomplete"
                    ]
                },
                "autocomplete_search": {
                    "tokenizer": "whitespace",
                    "filter": [
                        "lowercase"
                    ]
                }
            },
            "filter": {
                "autocomplete": {
                    "type": "nGram",
                    "min_gram": 2,
                    "max_gram": 40
                }
            }
        }
    },
    "mappings": {
        "doc": {
            "properties": {
                "title": {
                    "type": "string",
                    "analyzer": "autocomplete",
                    "search_analyzer": "autocomplete_search"
                }
            }
        }
    }
}

PUT my_index/doc/1
{
  "title": "peter tomson" 
}

PUT my_index/doc/2
{
  "title": "Peter tomson robert simson" 
}


GET my_index/doc/_search
    {
      "query": {
        "match": {
          "title": {
            "query": "Pete sim", 
            "operator": "and"
          }
        }
      }
    }

这篇关于弹性搜索中的通配符搜索的ngram的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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