在 Elasticsearch 中搜索包含 "not" 关键字的字符串



我在AWS(7.9版本(上使用ElasticSearch,我正在尝试区分两个字符串。

我的主要目标是在";找到";并且在";未找到";。

一般的问题是如何搜索";而不是";关键字。

您可以在下面看到两条示例消息。

"CachingServiceOne:Found in cache - Retrieve."
"CachingServiceThree:Not found in cache - Create new."

您可以使用ngram标记器在"title"字段上搜索"not"

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

索引映射:

{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "ngram",
"min_gram": 3,
"max_gram": 5,
"token_chars": [
"letter",
"digit"
]
}
}
},
"max_ngram_diff": 10
},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}

指数数据:

{
"title":"CachingServiceThree:Not found in cache - Create new."
}
{
"title":"CachingServiceOne:Found in cache - Retrieve."
}

搜索查询:

{
"query":{
"match":{
"title":"Not"
}
}
}

搜索结果:

"hits": [
{
"_index": "67093372",
"_type": "_doc",
"_id": "2",
"_score": 0.6720003,
"_source": {
"title": "CachingServiceThree:Not found in cache - Create new."
}
}
]

问题似乎确实是默认分析器的工作方式,而不是我无法搜索not单词。这就是我接受答案的原因。但我想补充一点。为了简单起见。

  1. 默认分析器不拆分:上的单词。

  2. 这意味着,我们必须搜索title:CachingServiceThree:Not

  3. 其中title是字段名,:必须转义为:

关键在于使用KQL语法的title:*:Nottitle:*:Found

使用通配符可以获取所有内容。我想知道使用所有实际值的数组是否会更快。

通过Inspect面板转换为:

{
"query": {
"bool": {
"filter": [
{
"bool": {
"should": [
{
"query_string": {
"fields": [
"title"
],
"query": "*\:Not"
}
}
],
"minimum_should_match": 1
}
}
]
}
}
}

最新更新