【问题标题】:ELK bool query with match and prefix带有匹配和前缀的 ELK bool 查询
【发布时间】:2023-01-30 22:40:49
【问题描述】:

我是 ELK 的新手。我对以下搜索查询有疑问:

curl --insecure -H "Authorization: ApiKey $ESAPIKEY" -X GET "https://localhost:9200/commsrch/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "should" : [
        {"match" : {"cn" : "franc"}},
        {"prefix" : {"srt" : "99889300200"}}
      ]
     
    }
  }
}
'

我需要找到满足条件的所有文档:OR 字段“cn”包含“franc”或字段“srt”以“99889300200”开头。

索引映射:

{
  "commsrch" : {
    "mappings" : {
      "properties" : {
        "addr" : {
          "type" : "text",
          "index" : false
        },
        "cn" : {
          "type" : "text",
          "analyzer" : "compname"
        },
        "srn" : {
          "type" : "text",
          "analyzer" : "srnsrt"
        },
        "srt" : {
          "type" : "text",
          "analyzer" : "srnsrt"
        }
      }
    }
  }
}

索引设置:

{
  "commsrch" : {
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "commsrch",
        "creation_date" : "1675079141160",
        "analysis" : {
          "filter" : {
            "ngram_filter" : {
              "type" : "ngram",
              "min_gram" : "3",
              "max_gram" : "4"
            }
          },
          "analyzer" : {
            "compname" : {
              "filter" : [
                "lowercase",
                "stop",
                "ngram_filter"
              ],
              "type" : "custom",
              "tokenizer" : "whitespace"
            },
            "srnsrt" : {
              "type" : "custom",
              "tokenizer" : "standard"
            }
          }
        },
        "number_of_replicas" : "1",
        "uuid" : "C15EXHnaTIq88JSYNt7GvA",
        "version" : {
          "created" : "8060099"
        }
      }
    }
  }
}

查询仅在一个条件下正常工作。如果查询只有“匹配”条件,则结果具有正确的文档计数。如果查询只有“前缀”条件,则结果具有正确的文档计数。

如果有两个条件“匹配”和“前缀”,我在结果文档中看到仅对应“前缀”条件。

在 ELK 文档中找不到关于混合“前缀”和“匹配”的任何限制,但我发现存在一些问题。请大家帮忙看看是哪里出了问题。

【问题讨论】:

  • 你好,你能分享文件吗?匹配只要字首和他们两个。
  • 真是个愚蠢的问题。我只看返回文件的列表。如果我看一下文件计数,一切都在推进!我忘记了将结果截断为 10 个文档。 @MusabDogan 谢谢!
  • 不客气,我将作为答案分享:)

标签: elasticsearch


【解决方案1】:

您可以使用_count API 来检查文档计数匹配字首查询。

下面是一个如何检查响应文档计数的示例。

GET /my-index-000001/_count
{
  "query" : {
    "term" : { "user.id" : "kimchy" }
  }
}

【讨论】:

    猜你喜欢
    • 2014-10-21
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 2017-12-05
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多