如何通过弹性搜索从文本中找到相似的标签

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

我尝试使用Elastic Search从文本中找到最相似的标签。

例如,我创建test_index并插入两个文档:

POST test_index/_doc/17
{
  "id": 17,
  "tags": ["it", "devops", "server"]
}

POST test_index/_doc/20
{
  "id": 20,
  "tags": ["software", "hardware"]
}

所以,我希望从“ 我正在使用某些软件和应用程序”文本中找到“ 软件”标签(文本或ID)。

我希望有人可以提供一个示例,说明如何执行此操作,或者至少将我指出正确的方向。

谢谢。

java elasticsearch lucene text-mining
1个回答
0
投票

您正在寻找的不过是一个称为Stemming的概念。您将需要创建一个Stemming并使用Custom Analyzer

请找到以下映射,样本文档,查询和响应:

映射:

Stemmer Token Filter

从注释看来,您正在使用版本<7。为此,您可能必须在其中添加PUT my_stem_index { "settings": { "analysis" : { "analyzer" : { "my_analyzer" : { "tokenizer" : "standard", "filter" : ["lowercase", "my_stemmer"] } }, "filter" : { "my_stemmer" : { "type" : "stemmer", "name" : "english" } } } }, "mappings": { "properties": { "id":{ "type": "keyword" }, "tags":{ "type": "text", "analyzer": "my_analyzer", "fields": { "keyword":{ "type": "keyword" } } } } } }

type

样本文件:

PUT my_stem_index
{
   "settings":{
      "analysis":{
         "analyzer":{
            "my_analyzer":{
               "tokenizer":"standard",
               "filter":[
                  "lowercase",
                  "my_stemmer"
               ]
            }
         },
         "filter":{
            "my_stemmer":{
               "type":"stemmer",
               "name":"english"
            }
         }
      }
   },
   "mappings":{
      "_doc":{
         "properties":{
            "id":{
               "type":"keyword"
            },
            "tags":{
               "type":"text",
               "analyzer":"my_analyzer",
               "fields":{
                  "keyword":{
                     "type":"keyword"
                  }
               }
            }
         }
      }
   }
}

请求查询:

POST my_stem_index/_doc/17
{
  "id": 17,
  "tags": ["it", "devops", "server"]
}

POST my_stem_index/_doc/20
{
  "id": 20,
  "tags": ["software", "hardware"]
}

POST my_stem_index/_doc/21
{
  "id": 21,
  "tags": ["softwares and applications", "hardwares and storage devices"]
}

响应:

POST my_stem_index/_search
{
  "query": {
    "match": {
      "tags": "software"
    }
  }
}

[有关具有{ "took" : 3, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 0.5908618, "hits" : [ { "_index" : "my_stem_index", "_type" : "_doc", "_id" : "20", "_score" : 0.5908618, "_source" : { "id" : 20, "tags" : [ "software", "hardware" ] } }, { "_index" : "my_stem_index", "_type" : "_doc", "_id" : "21", "_score" : 0.35965496, "_source" : { "id" : 21, "tags" : [ "softwares and applications", <--- Note this has how `softwares` also was searchable. "hardwares and storage devices" ] } } ] } } _id 20的文档如何出现的提示。

附加说明:

如果您是Elasticsearch的新手,建议您花些时间来了解21的概念以及Elasticsearch如何使用Analysis来实现相同的概念。

这将帮助您了解仅查询Analyzers时,如何返回带有Analyzers的文档,反之亦然。

希望这会有所帮助!

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