将ngrams与elasticsearch一起使用时带回所有相关结果 [英] Bring back all relevant results when using ngrams with elasticsearch

查看:57
本文介绍了将ngrams与elasticsearch一起使用时带回所有相关结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用ngram索引了我的elasticsearch索引,从而可以进行模糊匹配和快速前缀搜索.我注意到,如果我在名称字段中搜索包含"Bob"的文档,则仅结果名称= Bob返回.我希望响应包含名称为Bob的文档,但还要包含名称为Bobbi,Bobbette等的 文档.Bob的结果应该有一个相对较高的分数.其他不完全匹配的结果仍应出现在结果集中,但得分较低.如何使用ngrams实现此目的?

I indexed my elasticsearch index with ngrams to make it possible to do fuzzy matching and prefix searches quickly. I notice that if I search for documents containing "Bob" in the name field, only results name = Bob return. I would like the response to include documents with name=Bob, but also documents with name = Bobbi, Bobbette, etcetera. The Bob results should have a relatively high score. The other results that don't match exactly, should still appear in the results set, but with lower scores. How can I achieve this with ngrams?

我正在使用一个非常小的简单索引进行测试.索引包含两个文档.

I am using a very small simple index to test. The index contains two documents.

 {
    "_index": "contacts_4",
    "_type": "_doc",
    "_id": "1",
    "_score": 1.0,
    "_source": {
      "full_name": "Bob Smith"
    }
  },
  {
    "_index": "contacts_4",
    "_type": "_doc",
    "_id": "2",
    "_score": 1.0,
    "_source": {
      "full_name": "Bobby Smith"
    }
  }

推荐答案

这是一个可行的示例(使用n-gram标记器):

Here is a working example (using n-gram tokenizer):

ngram-tokenizer

映射

  PUT my_index
  {
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "filter": [
            "lowercase"
          ],
          "type": "custom",
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "token_chars": [
            "letter",
            "digit"
          ],
          "min_gram": "3",
          "type": "ngram",
          "max_gram": "4"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "full_name": {
        "type": "text",
        "analyzer": "my_analyzer",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

索引文档

POST my_index/_doc/1
{
  "full_name":"Bob Smith"
}

POST my_index/_doc/2
{
  "full_name":"Bobby Smith"
}

POST my_index/_doc/3
{
  "full_name":"Bobbette Smith"
}

搜索查询

GET my_index/_search
{
  "query": {
    "match": {
      "full_name": "Bob"
    }
  }
}

结果

"hits" : [
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 0.1626403,
    "_source" : {
      "full_name" : "Bob Smith"
    }
  },
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "2",
    "_score" : 0.13703513,
    "_source" : {
      "full_name" : "Bobby Smith"
    }
  },
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "3",
    "_score" : 0.11085624,
    "_source" : {
      "full_name" : "Bobbette Smith"
    }
  }
]

希望这会有所帮助

这篇关于将ngrams与elasticsearch一起使用时带回所有相关结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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