【问题标题】:Elasticsearch filter group by using nest c#使用嵌套 C# 的 Elasticsearch 过滤器组
【发布时间】:2021-07-02 14:19:11
【问题描述】:

我正在使用弹性搜索来获取按类别分组的产品并对结果进行聚合...... 如果我使用 categoryid(numeric) 作为字段,它的给出结果但是当我尝试给出类别名称时,它给出 Unsuccessful(400)

请看打击码sn-p

我正在获取文档计数。我可以从同一个请求中获取文档数据吗?

ISearchResponse<Products> results;

        results = _client.Search<Products>(s => s
                    //.Size(int.MaxValue)
                    .Query(q => q
                        .Bool(b => b
                            .Should(
                                bs => bs.Prefix(p => p.cat_name, "heli"),
                                bs => bs.Prefix(p => p.pr_name, "heli")
                                    )
                            )
                        )
                    .Aggregations(a => a
                        .Terms("catname", t => t
                            .Field(f => f.categoryid)
                            .Size(int.MaxValue)
                            .Aggregations(agg => agg
                               .Max("maxprice", av => av.Field(f2 => f2.price))
                               .Average("avgprice", av => av.Field(f3 => f3.price))
                               .Max("maxweidht", av => av.Field(f2 => f2.weight))
                               .Average("avgweight", av => av.Field(f3 => f3.weight))
                               )
                            )
                        )
                    );

映射模型:

{
  "product_catalog": {
     "mappings": {
  "properties": {
    "@timestamp": {
      "type": "date"
    },
    "@version": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "cat_name": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "categoryid": {
      "type": "long"
    },
    "createdon": {
      "type": "date"
    },
    "fulldescription": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "height": {
      "type": "float"
    },
    "length": {
      "type": "float"
    },
    "pr_name": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "price": {
      "type": "long"
    },
    "productid": {
      "type": "long"
    },
    "shortdescription": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "sku": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "updatedon": {
      "type": "date"
    },
    "weight": {
      "type": "float"
    },
    "width": {
      "type": "float"
        }
      }
    }
  }
}

谁能指导如何使用类别名称进行分组。

【问题讨论】:

  • 你能分享索引映射吗?在您的情况下,字段 catname 可能是文本类型,默认情况下,您不能在此类字段上执行术语 aggs,但要确保很高兴看到您的映射。
  • 谢谢@Rob,我已经更新了相关映射。

标签: c# elasticsearch nest


【解决方案1】:

catname 字段的类型为 text,因此您不能在聚合中默认使用它,因为 fielddata is disabled 出于性能原因。

根据您的映射,我看到您还为keyword 以及catname 编制索引,因此您可以使用此字段。 只需将您的术语聚合.Field(f =&gt; f.categoryid) 的这一部分更改为 .Field(f =&gt; f.cat_name.Suffix("keyword")) 你应该很好。

【讨论】:

    猜你喜欢
    • 2017-07-27
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    相关资源
    最近更新 更多