Elastic Search 中的百分位聚合返回值 0,即使存在具有值的匹配文档

问题描述 投票:0回答:1

我正在尝试通过弹性搜索查询中的聚合计算定价分数属性的第 75 个百分位值。我的最后一个条件似乎返回 0 docCount 返回 0 百分位值。

这是我索引中属性的映射:

        "pricingbytrade" : {
            "type" : "nested",
            "properties" : {
              "name" : {
                "type" : "text",
                "fields" : {
                  "analyzed" : {
                    "type" : "text",
                    "analyzer" : "standard",
                    "search_analyzer" : "synonym_analyzer"
                  },
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256,
                    "normalizer" : "lowercase"
                  }
                }
              },
              "pricingbycbsa" : {
                "type" : "nested",
                "properties" : {
                  "cbsaid" : {
                    "type" : "integer"
                  },
                  "cbsaname" : {
                    "type" : "text"
                  },
                  "pricingscore" : {
                    "type" : "float"
                  }
                }
              }
            }
          } 

这是我的 AggregationContainerDescriptor 中的代码。 在这里,pricingbytrade.name 必须与给定的搜索词匹配,pricingbytrade.pricingbycbsa.cbsaid 必须与给定的 cbsaid 匹配才能匹配文档并计算pricingbytrade.pricingbycbsa.pricingscore 的第 75 个百分点:

 .Nested("pricingbytrade", n => n
                .Path("pricingbytrade")
                .Aggregations(nestedAggs => nestedAggs
                    .Nested("pricingbycbsa", nn => nn
                        .Path("pricingbytrade.pricingbycbsa")
                        .Aggregations(nestedNestedAggs => nestedNestedAggs
                            .Filter("filter_bycbsa", ff => ff
                                .Filter(filter => filter
                                    .Bool(b => b
                                        .Should(filterList => filterList
                                          .Term(t => t
                                                .Field("pricingbytrade.pricingbycbsa.cbsaid")
                                                .Value(_cbsaId)
                                            ),
                                            filterList => filterList
                                                .Term(t => t
                                                    .Field("pricingbytrade.name")
                                                    .Value(_searchTerm.ToLower())
                                                )
                                       )
                                    )
                                )
                                .Aggregations(subAggs => subAggs
                                    .Percentiles("price_score_percentiles", p => p
                                        .Field("pricingbytrade.pricingbycbsa.pricingscore")
                                        .Percents(75)
                                    )
                                )
                            )
                        )
                    )
                )
            );

我在 bool 中尝试了“必须”而不是“应该”,但没有区别。

我按如下方式访问我的百分位值,该值始终为 0 :

var priceScorePercentiles = response.Aggregations
                  .Nested("pricingbytrade")
                  .Nested("pricingbycbsa")
                  .Nested("filter_bycbsa")
                  .PercentilesBucket("price_score_percentiles")
                  .Items[0]
                  .Value;

虽然有匹配的文档,但我得到的价格_分数_百分位数存储桶的 docCount 为 0。代码中是否有任何错误,我必须正确计算百分位值,仅当两个条件匹配时才选择文档?

elasticsearch aggregation percentile
1个回答
0
投票

我想给你写评论,但我想我不能,因为 Stack Overflow 规则 -_-。无论如何,我做了和你一样的事情,但我得到了结果。我将分享我所做的;也许它对你有用。

首先使用映射创建索引:

PUT /new_stack_over_flow_question
{
  "mappings": {
    "properties": {
      "pricingbytrade": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "pricingbycbsa": {
            "type": "nested",
            "properties": {
              "cbsaid": {
                "type": "long"
              },
              "cbsaname": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "pricingscore": {
                "type": "float"
              }
            }
          }
        }
      }
    }
  }
}

第二次插入虚拟数据

POST /new_stack_over_flow_question/_doc
{
  "pricingbytrade": {
    "name": "dummy_name1",
    "pricingbycbsa": [
      {
        "cbsaid": 123,
        "cbsaname": "dummy_cbsa_name1",
        "pricingscore": 10.5
      },
      {
        "cbsaid": 456,
        "cbsaname": "dummy_cbsa_name2",
        "pricingscore": 15.2
      }
    ]
  }
}

POST /new_stack_over_flow_question/_doc
{
  "pricingbytrade": {
    "name": "dummy_name2",
    "pricingbycbsa": [
      {
        "cbsaid": 789,
        "cbsaname": "dummy_cbsa_name3",
        "pricingscore": 20.1
      },
      {
        "cbsaid": 101,
        "cbsaname": "dummy_cbsa_name4",
        "pricingscore": 25.7
      }
    ]
  }
}

这里可以看到所有数据

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "new_stack_over_flow_question",
        "_id": "wFMHqY4BH07gFgTpzL-g",
        "_score": 1,
        "_source": {
          "pricingbytrade": {
            "name": "dummy_name1",
            "pricingbycbsa": [
              {
                "cbsaid": 123,
                "cbsaname": "dummy_cbsa_name1",
                "pricingscore": 10.5
              },
              {
                "cbsaid": 456,
                "cbsaname": "dummy_cbsa_name2",
                "pricingscore": 15.2
              }
            ]
          }
        }
      },
      {
        "_index": "new_stack_over_flow_question",
        "_id": "wVMIqY4BH07gFgTpar81",
        "_score": 1,
        "_source": {
          "pricingbytrade": {
            "name": "dummy_name2",
            "pricingbycbsa": [
              {
                "cbsaid": 789,
                "cbsaname": "dummy_cbsa_name3",
                "pricingscore": 20.1
              },
              {
                "cbsaid": 101,
                "cbsaname": "dummy_cbsa_name4",
                "pricingscore": 25.7
              }
            ]
          }
        }
      },
      {
        "_index": "new_stack_over_flow_question",
        "_id": "wlMLqY4BH07gFgTpA78p",
        "_score": 1,
        "_source": {
          "pricingbytrade": {
            "name": "dummy_name2",
            "pricingbycbsa": [
              {
                "cbsaid": 789,
                "cbsaname": "dummy_cbsa_name3",
                "pricingscore": 20.1
              },
              {
                "cbsaid": 101,
                "cbsaname": "dummy_cbsa_name4",
                "pricingscore": 25.7
              }
            ]
          }
        }
      }
    ]
  }
}

POST /new_stack_over_flow_question/_doc
    {
      "pricingbytrade": {
        "name": "dummy_name2",
        "pricingbycbsa": [
          {
            "cbsaid": 789,
            "cbsaname": "dummy_cbsa_name3",
            "pricingscore": 20.1
          },
          {
            "cbsaid": 101,
            "cbsaname": "dummy_cbsa_name4",
            "pricingscore": 25.7
          }
        ]
      }
    }

然后进行搜索

POST /new_stack_over_flow_question/_search
{
  "size": 0,
  "aggs": {
    "pricingbytrade_nested": {
      "nested": { "path": "pricingbytrade"},
      "aggs": {
        "pricingbycbsa_nested": {
          "nested": {"path": "pricingbytrade.pricingbycbsa"},
          "aggs": {
            "filter_bycbsa": {
              "filter": {
                "bool": {
                  "should": [
                    {
                      "term": {
                        "pricingbytrade.pricingbycbsa.cbsaid": {
                          "value": 123
                        }
                      }
                    },
                    {
                      "term": {
                        "pricingbytrade.name": {
                          "value": "Product 1"
                        }
                      }
                    }
                    ]
                }
              },
              "aggs": {
                "price_score_percentiles": {
                  "percentiles": {
                    "field": "pricingbytrade.pricingbycbsa.pricingscore",
                    "percents": [75]
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

这是回复:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "pricingbytrade_nested": {
      "doc_count": 3,
      "pricingbycbsa_nested": {
        "doc_count": 6,
        "filter_bycbsa": {
          "doc_count": 1,
          "price_score_percentiles": {
            "values": {
              "75.0": 10.5
            }
          }
        }
      }
    }
  }
}

您可以将我与您分享的内容与(响应)进行比较,或者将调用(sourceBuilder)与搜索查询请求进行比较。使用调试工具打开C#(sourceBuilder)数据;它应该看起来像我使用的相同搜索查询。

如果您无法修复它,只需共享调试器中的(响应)数据和(sourceBuilder)数据即可。

© www.soinside.com 2019 - 2024. All rights reserved.