微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Elasticsearch 搜索精确标签

如何解决Elasticsearch 搜索精确标签

假设我有以下文件

doc1: "blue water"
doc2: "extra blue water"
doc3: "blue waters"

我正在寻找一种方法来处理以下情况

如果用户搜索“blue water”,我希望他收到 doc1 和 doc3(这意味着它会忽略 doc2,并且还会有一个分析器,可以像 doc3 一样提取标记)。

例如,如果我使用 query_string,我将收到 doc2 以及 doc1 和 doc3。

解决方法

您可以将 stemmerpercolate query 一起使用

添加一个包含索引数据、映射、搜索查询和搜索结果的工作示例

索引映射:

{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "whitespace","filter": [
            "stemmer"
          ]
        }
      }
    }
  },"mappings": {
    "properties": {
      "tags": {
        "type": "text","analyzer": "my_analyzer"
      },"query": {
        "type": "percolator"
      }
    }
  }
}

索引数据:

{
  "query": {
    "match_phrase": {
      "tags": {
        "query": "blue waters","analyzer": "my_analyzer"
      }
    }
  }
}
{
  "query": {
    "match_phrase": {
      "tags": {
        "query": "extra blue water","analyzer": "my_analyzer"
      }
    }
  }
}
{
  "query": {
    "match_phrase": {
      "tags": {
        "query": "blue water","analyzer": "my_analyzer"
      }
    }
  }
}

搜索查询:

{
  "query": {
    "percolate": {
      "field": "query","document": {
        "tags": "blue water"
      }
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "67671916","_type": "_doc","_id": "3","_score": 0.26152915,"_source": {
          "query": {
            "match_phrase": {
              "tags": {
                "query": "blue waters","analyzer": "my_analyzer"
              }
            }
          }
        },"fields": {
          "_percolator_document_slot": [
            0
          ]
        }
      },{
        "_index": "67671916","_id": "1","_source": {
          "query": {
            "match_phrase": {
              "tags": {
                "query": "blue water","fields": {
          "_percolator_document_slot": [
            0
          ]
        }
      }
    ]
,

在这种情况下,您可以使用前缀搜索。如果您查找 blue water,则根据前缀搜索,它将给出 doc1 和 doc3。

对于前缀搜索:

{
    "query": {
        "prefix":{
            "doc": word
        }
     }
}

这里的词 = 蓝水

你可以看看this link

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。