【问题标题】:Elasticsearch query to return part of words searched forElasticsearch 查询返回部分搜索词
【发布时间】:2021-01-12 11:18:58
【问题描述】:

如果我搜索“thank”,我想知道如何返回“thanks”或“thanking” 目前我有一个多重匹配查询,它只返回出现的“谢谢”,比如“谢谢”,而不是“感恩节”或“谢谢”。我正在使用 ElasticSearch 7.9.1

query: {
                bool: {
                    must: [
                        {match: {accountId}},
                        {
                            multi_match: {
                                query: "thank",
                                type: "most_fields",
                                fields: ["text", "address", "description", "notes", "name"],
                            }
                        }
                    ],
                    filter: {match: {type: "personaldetails"}}
                }
            },

还可以将多重匹配查询与其中一个字段上的 queryString 结合起来(比如描述,我只会在描述上进行查询字符串搜索,在其他字段上进行短语匹配)

    {   "query": {
        "query_string": {
          "query": "(new york city) OR (big apple)",
          "default_field": "content"
        }   
       } 
     }

感谢任何输入。 谢谢

【问题讨论】:

    标签: elasticsearch search


    【解决方案1】:

    您可以使用 edge_ngrma tokenizer 将文本首先分解为 每当遇到指定字符列表中的一个时, 然后它发出每个单词的 N-gram,其中 N-gram 的开头是 锚定到单词的开头。

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

    索引映射:

        {
      "settings": {
        "analysis": {
          "analyzer": {
            "my_analyzer": {
              "tokenizer": "my_tokenizer"
            }
          },
          "tokenizer": {
            "my_tokenizer": {
              "type": "edge_ngram",
              "min_gram": 5,
              "max_gram": 20,
              "token_chars": [
                "letter",
                "digit"
              ]
            }
          }
        },
        "max_ngram_diff": 50
      },
      "mappings": {
        "properties": {
          "notes": {
            "type": "text",
            "analyzer": "my_analyzer",
            "search_analyzer": "standard"    // note this
          }
        }
      }
    }
    

    索引数据:

    {
        "notes":"thank"
    }
    {
        "notes":"thank you"
    }
    {
        "notes":"thanks"
    }
    {
        "notes":"thanksgiving"
    }
    

    搜索查询:

    {
      "query": {
        "multi_match" : {
          "query":    "thank", 
          "fields": [ "notes", "name" ] 
        }
      }
    }
    

    搜索结果:

    "hits": [
          {
            "_index": "65511630",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.1448707,
            "_source": {
              "notes": "thank"
            }
          },
          {
            "_index": "65511630",
            "_type": "_doc",
            "_id": "3",
            "_score": 0.1448707,
            "_source": {
              "notes": "thank you"
            }
          },
          {
            "_index": "65511630",
            "_type": "_doc",
            "_id": "2",
            "_score": 0.12199639,
            "_source": {
              "notes": "thanks"
            }
          },
          {
            "_index": "65511630",
            "_type": "_doc",
            "_id": "4",
            "_score": 0.06264679,
            "_source": {
              "notes": "thanksgiving"
            }
          }
        ]
    

    要将多重匹配查询与查询字符串结合,请使用以下查询:

    {
      "query": {
        "bool": {
          "must": {
            "multi_match": {
              "query": "thank",
              "fields": [
                "notes",
                "name"
              ]
            }
          },
          "should": {
            "query_string": {
              "query": "(new york city) OR (big apple)",
              "default_field": "content"
            }
          }
        }
      }
    }
    

    【讨论】:

    • @user14262559 你有没有机会通过答案,期待得到你的反馈?
    • 抱歉耽搁了。生病了,到目前为止无法尝试。今天会试试这个。
    • @user14262559 别担心?快点好起来!
    • 抱歉延迟回复。这可以找到部分单词,但我看到的问题是区分大小写。例如,输入“new”不再得到“New York”,我发现了一个 stackoverflowing 帖子,其中详细说明了添加客户映射器会导致不区分大小写的问题。如果这里有解决方法,请告诉我。
    • 我通常在寻找不区分大小写的内容,除非在引号内
    猜你喜欢
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多