将ngram与elasticsearch一起使用时,带回所有相关结果

问题描述 投票:0回答:1

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

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

 {
    "_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"
    }
  }
elasticsearch prefix n-gram
1个回答
0
投票

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

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

希望这会有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.