elasticsearch - 在 ElasticSearch 5.6.3 中获取嵌套文档的聚合导致 Lucene 异常

标签 elasticsearch lucene aggregation

我在 ElasticSearch 5.6.3 上聚合嵌套文档时遇到问题。

我的查询结构如下:

  query
  aggs
  |_filter
    |_nested
      |_term
        |_top-hits

如果我尝试在非嵌套字段上进行聚合(当然还删除了嵌套聚合),一切都会按预期进行。但是按照现在的结构,我收到了来自 Lucene 的异常: 子查询不能与父过滤器匹配相同的文档。将它们组合为 must 子句 (+) 以查找问题文档。 docId=2147483647,类 org.apache.lucene.search.ConstantScoreScorer

此异常不会在 ElasticSearch 2.4.6 上触发。

我尝试以不同的方式构建聚合,但我无法想出一个有效的组合并提供所需的结果。

映射是这样的:

"recording": {
  "dynamic": "strict",
  "_all" : {
    "enabled" : false
  },
  "properties": {
    "id": {
      "type": "integer"
    },
    "soloists": {
      "properties": {
        "type": "nested",
        "person": {
          "properties": {
            "id": {
             "type": "integer"
            },
            "name": {
              "type": "string",
              "index": "not_analyzed"
            }
         }
      }
    },
    "work": {
      "id": {
        "type": integer
      },
      "title": {
        "type": "string",
        "index": "not_analyzed"
      }
    }
}

查询本身:

{
  "query": {},
  "aggs": {
    "my_top_results": {
      "global": {},
      "aggs": {
        "my_filter_agg": {
          "filter": {
            "bool": {
              "must": [
                {
                  "bool": {
                    "should": [
                      {
                        "nested": {
                          "path": "soloists",
                          "query": {
                            "bool": {
                              "must": {
                                "match": {
                                  "soloists.person.id": 77957
                                }
                              }
                            }
                          }
                        }
                      }
                    ]
                  }
                }
              ]
            }
          },
          "aggs": {
            "my_nested_agg": {
              "nested": {
                "path": "soloists"
              },
              "aggs": {
                "my_terms_agg": {
                  "term": {
                    "field": "soloists.person.id",
                    "size": 10
                  }
                  "aggs": {
                    "my_top_hits_agg": {
                      "size": 1,
                      "_source": {
                        "include": [
                          "soloists.person.id",
                          "soloists.person.name"
                        ]
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

如有任何帮助,我们将不胜感激。

我在寻找解决方案时偶然发现的一些链接:

最佳答案

您的映射和查询中有一些拼写错误:

这里有一些固定的命令,在 Elasticsearch 5.6.3 的实例上使用时不会触发任何错误。

您可以在 Kibana 或 Linux 终端中复制和粘贴(在这种情况下您应该编辑第一行)并在您的 Elasticsearch 实例上测试它们。

HOST=10.225.0.2:9200

curl -XPUT "http://$HOST/an_index"

curl -XPUT "http://$HOST/an_index/recording/_mapping" -H 'Content-Type: application/json' -d'
{
  "dynamic": "strict",
  "_all": {
    "enabled": false
  },
  "properties": {
    "id": {
      "type": "integer"
    },
    "soloists": {
      "type": "nested",
      "properties": {
        "person": {
          "properties": {
            "id": {
              "type": "integer"
            },
            "name": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        }
      }
    },
    "work": {
      "properties": {
        "id": {
          "type": "integer"
        },
        "title": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  }
}'

curl -XPOST "http://$HOST/an_index/recording/1" -H 'Content-Type: application/json' -d'
{
  "id": 0,
  "soloists": [
    {
      "person": {
        "id": 77957,
        "name": "John doe"
      }
    },
    {
      "person": {
        "id": 1,
        "name": "Jane smith"
      }
    }
  ],
  "work": {
    "id": 0,
    "title": "Test"
  }
}'

curl -XGET "http://$HOST/an_index/recording/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "my_top_results": {
      "global": {},
      "aggs": {
        "my_filter_agg": {
          "filter": {
            "bool": {
              "must": [
                {
                  "bool": {
                    "should": [
                      {
                        "nested": {
                          "path": "soloists",
                          "query": {
                            "bool": {
                              "must": {
                                "match": {
                                  "soloists.person.id": 77957
                                }
                              }
                            }
                          }
                        }
                      }
                    ]
                  }
                }
              ]
            }
          },
          "aggs": {
            "my_nested_agg": {
              "nested": {
                "path": "soloists"
              },
              "aggs": {
                "my_terms_agg": {
                  "terms": {
                    "field": "soloists.person.id",
                    "size": 10
                  },
                  "aggs": {
                    "my_top_hits_agg": {
                      "top_hits": {
                        "size": 1,
                        "_source": {
                          "include": [
                            "soloists.person.id",
                            "soloists.person.name"
                          ]
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}'

如果这些查询有效但应用于您的索引时无效,请您使用 curl -XGET "http://$HOST/your_index_name" 的输出更新您的问题,以便我们检查您的索引的确切设置和映射?这种错误可能是由同一索引上的类型之间的冲突引起的。我会相应地更新我的答案。

关于elasticsearch - 在 ElasticSearch 5.6.3 中获取嵌套文档的聚合导致 Lucene 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47226236/

相关文章:

elasticsearch - 如何在Elasticsearch中过滤掉超过一击的东西?

search - Solr 术语跨多个字段的组件?

java - 如何使用查询语法在 Lucene 中跨多个字段进行搜索?

mysql - 如何在sequelize ORM中使用从时间戳中选择和提取日期到月份和年份?

javascript - 使用 node.js 连接和聚合 json 响应数据

elasticsearch - ElasticSearch 5找不到包含关键字(包括空格)的文档

facebook - Elasticsearch标准化数据

java - 解决 Java Lucene 忽略字段的问题

shell - sed 或 awk : group by 2 columns and get the last value of another column

elasticsearch客户端线程安全