【问题标题】:Elasticsearch searching for exact tagsElasticsearch 搜索精确标签
【发布时间】:2021-05-24 20:30:00
【问题描述】:

假设我有以下文件

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

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

如果用户搜索“蓝水”,我希望他接收 doc1 和 doc3(这意味着它将忽略 doc2,并且还将具有能够像 doc3 中那样提取令牌的分析器)。

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

【问题讨论】:

    标签: elasticsearch analyzer


    【解决方案1】:

    您可以将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",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.26152915,
            "_source": {
              "query": {
                "match_phrase": {
                  "tags": {
                    "query": "blue water",
                    "analyzer": "my_analyzer"
                  }
                }
              }
            },
            "fields": {
              "_percolator_document_slot": [
                0
              ]
            }
          }
        ]
    

    【讨论】:

    • @Lior Magen 您是否有机会查看答案,期待您的反馈:-)
    【解决方案2】:

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

    前缀搜索:

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

    这里的词=蓝色的水

    你可以看看this link

    【讨论】:

      猜你喜欢
      • 2021-03-27
      • 2016-03-14
      • 2013-08-13
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-22
      相关资源
      最近更新 更多