以下弹性搜索查询中的错误结果

时间:2017-05-17 08:52:23

标签: elasticsearch nest

我是弹性搜索的新手,我第一次使用它来索引我的数据并对其进行搜索。

我编写此代码以查找索引中的术语计数:

{
  {
    "size": 0
  },
  "aggs": {
    "BookID": {
      "terms": {
        "field": "bookID",
        "size": 100000
      },
      "aggs": {
        "total_Chapter": {
          "nested": {
            "path": "chapterData"
          },
          "aggs": {
            "termsInChapter": {
              "filters": {
                "filters": {
                  "king": {
                    "query_string": {
                      "query": "king",
                      "default_field": "chapter.data"
                    }
                  },
                  "queen": {
                    "query_string": {
                      "query": "queen",
                      "default_field": "chapter.data"
                    }
                  },
                  "apple": {
                    "query_string": {
                      "query": "apple",
                      "default_field": "chapter.data"
                    }
                  }
                }
              }
            },
            "termsInChapter_count": {
              "value_count": {
                "field": "termsInChapter"
              }
            }
          }
        }
      }
    }
  }
}

但不知何故结果返回0.我不知道在这个查询中我做错了什么。

以下是此查询输出的一部分:

"aggregations": {
    "BookID": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 1,
          "doc_count": 1,
          "total_Chapter": {
            "doc_count": 23,
            "termsInChapter": {
              "buckets": {
                "apple": {
                  "doc_count": 2
                },
                "king": {
                  "doc_count": 1
                },
                "queen": {
                  "doc_count": 1
                }
              }
            },
            "termsInChapter_count": {
              "value": 0              <=========This should return 3
            }
          }
        },
        {
          "key": 2,
          "doc_count": 1,
          "total_Chapter": {
            "doc_count": 23,
            "termsInChapter": {
              "buckets": {
                "apple": {
                  "doc_count": 0
                },
                "king": {
                  "doc_count": 3
                },
                "queen": {
                  "doc_count": 2
                }
              }
            },
            "termsInChapter_count": {
              "value": 0
            }
          }
        },

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您的termsInChapter_count聚合需要引用字段,而不是聚合名称,这就是您的解决方案不起作用的原因。

要实现这一点,您需要sum_bucket类型的聚合:

"termsInChapter_count": {
  "sum_bucket": {
    "buckets_path": "termsInChapter._count"
  }
}

稍后编辑

  "aggs": {
    "termsInChapter": {
      "filters": {
        "filters": {
          "king": {
            "query_string": {
              "query": "king",
              "default_field": "chapter.data"
            }
          },
          "queen": {
            "query_string": {
              "query": "queen",
              "default_field": "chapter.data"
            }
          },
          "apple": {
            "query_string": {
              "query": "apple",
              "default_field": "chapter.data"
            }
          }
        }
      },
      "aggs": {
        "filter_zero_matches_buckets": {
          "bucket_selector": {
            "buckets_path": {
              "hits": "_count"
            },
            "script": "params.hits > 0"
          }
        }
      }
    },
    "termsInChapter_count": {
      "bucket_script": {
        "buckets_path": {
          "bucket_count": "termsInChapter._bucket_count"
        },
        "script": "params.bucket_count"
      }
    }
  }

您在结果中寻找的是termsInChapter_count值。

答案 1 :(得分:0)

首先感谢Andrei Stefan寻求帮助。

我直接回答了这个问题,这样任何人都可以直接回答而无需通过评论等等。

通过this回答,我发现bucket_script应放在更高的级别。所以我稍微改变了你的查询从上一个答案,它的确有效。

以下是最终的工作查询:

"aggs": {
  "total_Chapter": {
    "nested": {
      "path": "chapterData"
    },
    "aggs": {
      "termsInChapter": {
        "filters": {
          "filters": {
            "king": {
              "query_string": {
                "query": "king",
                "default_field": "chapter.data"
              }
            },
            "queen": {
              "query_string": {
                "query": "queen",
                "default_field": "chapter.data"
              }
            },
            "apple": {
              "query_string": {
                "query": "apple",
                "default_field": "chapter.data"
              }
            }
          }
        },
        "aggs": {
          "filter_zero_matches_buckets": {
            "bucket_selector": {
              "buckets_path": {
                "hits": "_count"
              },
              "script": "params.hits > 0"
            }
          }
        }
      }
    }
  },
  "termsInChapter_count": {
    "bucket_script": {
      "buckets_path": {
        "bucket_count": "total_Chapter>termsInChapter._bucket_count"
      },
      "script": "params.bucket_count"
    }
  }
}

再次感谢Andrei。 :)