首页 > 解决方案 > 在elasticsearch中按嵌套字段的大小获取文档?

问题描述

我有带有嵌套字段的弹性文档这是一个示例文档

"Not nestedKey": "Some value"
"sentencesSentiments": [
            {
              "sentence": "Good",
              "sentenceKeyword": "Good",
              "score": 0.8,
              "magnitude": 0.8
            }
          ]
"Another not nested key" :"Another value"

如您所见,sentencesSentiments是嵌套键,这里是它的映射

 "sentencesSentiments": {
            "type": "nested",
            "properties": {
              "magnitude": {
                "type": "double",
                "store": true
              },
              "score": {
                "type": "double",
                "store": true
              },
              "sentence": {
                "type": "text",
                "store": true
              },
              "sentenceKeyword": {
                "type": "keyword",
                "store": true
              }
            }
          }

现在我需要获取那些“sentencesSentiments”为空的文档以及其他一些布尔过滤器。我尝试了不同的查询,但没有一个有效,这里有一些查询

GET indexName/_search
{
    "query": {
        "nested": {
            "path": "sentencesSentiments",
            "query": {
                "bool": {
                    "must": [
                        {
                            "exists": {
                                "field": "sentencesSentiments.sentenceKeyword"
                            }
                        }
                    ]
                }
            }
        }
    }
}


GET indexName/_search
{
  "query": {
    "bool": {
      "filter": {
        "nested": {
        "path": "sentencesSentiments",
        "filter": {
            "script": {
                "script" : {
                        "inline": "doc['sentencesSentiments'].values.length  > 0",
                        "lang": "painless"
                     }
            }
        }
    }
      }
    }
  }
}


GET indexname/_search
{
    "query": {
        "bool" : {
            "must" : {
                "script" : {
                    "script" : {
                        "inline": "doc['sentencesSentiments'].values.length  > 0",
                        "lang": "painless"
                     }
                }
            }
        }
    }
}

我也尝试过“values.size()”。无法弄清楚这个的确切语法。 注意:我使用的是弹性版本 5.5

标签: elasticsearchelastic-stack

解决方案


推荐阅读