elasticsearch 动态字段的弹性DSL查询

doinxwow  于 2023-03-29  发布在  ElasticSearch
关注(0)|答案(1)|浏览(163)

我需要帮助来构建弹性查询,跳过channelMessage.channelMessageType等于RESUME_EXECUTIONreplyMessages数组为空或第一个元素channelMessageType等于INTENT_FINISHED的记录。
在夏天像:

(channelMessage.channelMessageType equal 'RESUME_EXECUTION' AND (replyMessages is empty or first element .'channelMessageType' equal 'INTENT_FINISHED'))

查询字段Map信息

"channelMessage": {
                    "dynamic": "true",
                    "properties": {
                        "channelMessageType": {
                            "type": "text",
                            "store": true,
                            "fielddata": true
                        },...

     "replyMessages": {
                    "type": "nested",
                    "dynamic": "true",
                    "properties": {
                        "channelMessageType": {
                            "type": "text",
                            "store": true,
                            "fielddata": true
                        },...

到目前为止,我尝试了:

{
  "bool" : {
    "must_not" : [
      {
        "bool" : {
          "must" : [
            {
              "term" : {
                "channelMessage.channelMessageType" : {
                  "value" : "RESUME_EXECUTION",
                  "boost" : 1.0
                }
              }
            }
          ],
          "must_not" : [
            {
              "exists" : {
                "field" : "replyMessages.channelMessageType",
                "boost" : 1.0
              }
            }
          ],
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      },
      {
        "bool" : {
          "must" : [
            {
              "term" : {
                "channelMessage.channelMessageType" : {
                  "value" : "RESUME_EXECUTION",
                  "boost" : 1.0
                }
              }
            },
            {
              "script" : {
                "script" : {
                  "source" : "doc['replyMessages.channelMessageType'].length == 1",
                  "lang" : "painless"
                },
                "boost" : 1.0
              }
            },
            {
              "term" : {
                "replyMessages.channelMessageType" : {
                  "value" : "INTENT_FINISHED",
                  "boost" : 1.0
                }
              }
            }
          ],
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}

任何帮助都将不胜感激!

bmvo0sr5

bmvo0sr51#

在阅读更多的弹性文档时,我发现了以下warning

Avoid using the term query for text fields.
By default, Elasticsearch changes the values of text fields as part of analysis. This can make finding exact matches for text field values difficult.
To search text field values, use the match query instead.

通过检查索引Map类型,channelMessage.channelMessageTypetext,这是查询不起作用的原因。通过将所有term替换为match运算符,我的问题解决了!

相关问题