【问题标题】:ElasticSearch Keyword usage with a prefix search带前缀搜索的 ElasticSearch 关键字用法
【发布时间】:2020-09-15 14:17:12
【问题描述】:

我要求能够搜索完整或带前缀的句子。我正在使用的 UI 库(反应式搜索)以这种方式生成查询:

"simple_query_string": {
  "query": "\"Louis George Maurice Adolphe\"",
  "fields": [
    "field1",
    "field2",    
    "field3"
  ],
  "default_operator": "or"
}

我期待它返回结果,例如。 Louis George Maurice Adolphe (Roche) 但不仅仅是包含部分术语的记录,如 LouisGeorge

目前,我有这样的代码,但是如果我使用完整的单词 Louis George Maurice Adolphe (Roche) 而不是前缀 Louis George Maurice Adolphe 进行搜索,它只会带来记录。

{
  "settings": {
    "analysis": {
      "char_filter": {
        "space_remover": {
          "type": "mapping",
          "mappings": [
            "\\u0020=>"
          ]
        }
      },
      "normalizer": {
        "lower_case_normalizer": {
          "type": "custom",
          "char_filter": [
            "space_remover"
          ],
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  },
  "mappings": {
    "_doc": {
      "properties": {
        "field3": {
          "type": "keyword",
          "normalizer": "lower_case_normalizer"
        }
      }
    }
  }
}

感谢您对上述内容的任何指导。谢谢。

【问题讨论】:

    标签: elasticsearch reactivesearch


    【解决方案1】:

    您没有使用prefix query,因此没有得到前缀搜索词的结果,我使用了相同的映射和示例文档,但更改了搜索查询,从而得到了预期的结果

    索引映射

    {
        "settings": {
            "analysis": {
                "char_filter": {
                    "space_remover": {
                        "type": "mapping",
                        "mappings": [
                            "\\u0020=>"
                        ]
                    }
                },
                "normalizer": {
                    "lower_case_normalizer": {
                        "type": "custom",
                        "char_filter": [
                            "space_remover"
                        ],
                        "filter": [
                            "lowercase"
                        ]
                    }
                }
            }
        },
        "mappings": {
            "properties": {
                "field3": {
                    "type": "keyword",
                    "normalizer": "lower_case_normalizer"
                }
            }
        }
    }
    

    索引示例文档

    {
       "field3" : "Louis George Maurice Adolphe (Roche)"
    }
    
    

    搜索查询

    {
      "query": {
        "prefix": {
         "field3": {
            "value": "Louis George Maurice Adolphe"
          }
        }
      }
    }
    
    

    搜索结果

    "hits": [
                {
                    "_index": "normal",
                    "_type": "_doc",
                    "_id": "1",
                    "_score": 1.0,
                    "_source": {
                        "field3": "Louis George Maurice Adolphe (Roche)"
                    }
                }
            ]
    
    

    【讨论】:

    • 感谢您的回复。这里的问题是用户通过搜索框查询它并且库生成simple_query_string。用户的搜索词可以匹配任何字段 field1、field2、field3 或 field4。如何通过仍然保持此功能来更改查询?并且所有字段都可以链接到不同的分析器。
    • 在我的搜索查询中添加多个字段并不是什么大问题,而是定义。您必须更改库中的逻辑才能创建前缀查询而不是 simple_q_string
    • @sunny 运气好吗?如果有帮助,请不要忘记投票并接受答案
    • @sunny 有什么运气和更新吗?如果您有后续问题,请告诉我
    【解决方案2】:

    根本问题源于您正在应用空白删除器这一事实。这实际上意味着当您提取文档时:

    GET your_index_name/_analyze
    {
      "text": "Louis George Maurice Adolphe (Roche)",
      "field": "field3"
    }
    

    它们被索引为

    {
      "tokens" : [
        {
          "token" : "louisgeorgemauriceadolphe(roche)",
          "start_offset" : 0,
          "end_offset" : 36,
          "type" : "word",
          "position" : 0
        }
      ]
    }
    

    因此,如果您打算使用 simple_string,您可能需要重新考虑您的规范化器。

    @Ninja 搜索George Maurice Adolphe 时回答失败,即没有前缀交集。

    【讨论】:

    • 是的,我找到了他的令牌,因为他提到他需要前缀搜索,所以添加了该解决方案,如果他需要更多我们不知道 atm 的组合,显然它需要更多的微调
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 2023-03-23
    • 2020-01-28
    • 1970-01-01
    相关资源
    最近更新 更多