简体   繁体   English

ElasticSearch 关键字使用前缀搜索

[英]ElasticSearch Keyword usage with a prefix search

I have a requirement to be able to search a sentence as complete or with prefix.我需要能够搜索完整或带前缀的句子。 The UI library (reactive search) I am using is generating the query in this way:我正在使用的 UI 库(反应式搜索)以这种方式生成查询:

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

I am expecting it to returns results for eg.我期待它返回例如结果。 Louis George Maurice Adolphe (Roche) but NOT just records containing partial terms like Louis or George Louis George Maurice Adolphe (Roche)但不仅仅是包含像LouisGeorge这样的部分术语的记录

Currently, I have code like this but it only brings the record if I search with complete word Louis George Maurice Adolphe (Roche) but not a prefix Louis George Maurice Adolphe .目前,我有这样的代码,但如果我使用完整的单词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"
        }
      }
    }
  }
}

Any guidance on the above is appreciated.对上述任何指导表示赞赏。 Thanks.谢谢。

You are not using the prefix query hence not getting result for prefix search terms, I used same mapping and sample doc, but changed the search query which gives the expected results您没有使用前缀查询,因此没有获得前缀搜索词的结果,我使用了相同的映射和示例文档,但更改了提供预期结果的搜索查询

Index mapping索引映射

{
    "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"
            }
        }
    }
}

Indexed sample doc索引示例文档

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

Search query搜索查询

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

Search result搜索结果

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

The underlying issue stems from the fact that you're applying a whitespace remover.根本问题源于您正在应用空白去除剂这一事实。 What this practically means is that when you ingest your docs:这实际上意味着当您摄取文档时:

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

they're indexed as他们被索引为

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

So if you indend to use simple_string , you may want to rethink your normalizers.所以,如果你indend使用simple_string ,你可能要重新考虑你的正规化。

@Ninja's answer fails when you search for George Maurice Adolphe , ie no prefix intersection.当您搜索George Maurice Adolphe ,@Ninja 的回答失败,即没有前缀交集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM