全文elasticsearch填充嵌套对象并按字段分数排序命中

发布于 2025-01-11 21:33:35 字数 2517 浏览 2 评论 0原文

我有 2 个文档,ShopDocument 和 ProductDocument。我需要的是过滤掉所有字段 hiddenremoved 等于 true 的商店,从留下的商店中过滤掉具有 < code>removed 和 hidden 字段等于 true,然后从剩下的内容中,我想过滤掉与某些字符串不匹配的商店和产品(匹配需要位于某些字段中)并根据分数对结果进行排序我将给每个字段(就像在lucene中)。这是我的文档:

@Document(indexName = Indices.SHOP_INDEX)
@Setting(settingPath = Constants.esSettingsPath)
@Data
public class ShopDocument {

@Id
@Field(type = FieldType.Keyword)
private Long id;

@Field(type = FieldType.Keyword)
private Long companyId;

@Field(type = FieldType.Boolean)
private Boolean companyHidden;

@Field(type = FieldType.Keyword)
private String slug;

@Field(type = FieldType.Keyword)
private String postalCode;

@Field(type = FieldType.Keyword)
private String addressLine;

@Field(type = FieldType.Boolean)
private boolean hidden;

@Field(type = FieldType.Boolean)
private boolean removed;

@Field(type = FieldType.Text)
private String name;

@Field(type = FieldType.Nested)
private List<ProductDocument> products = new ArrayList<>();

@Field(type = FieldType.Double)
private Double latitude;

@Field(type = FieldType.Double)
private Double longitude;

@Field(type = FieldType.Integer)
private Integer maxDeliveryDistance;
}



@Document(indexName = Indices.Product_INDEX)
@Setting(settingPath = Constants.esSettingsPath)
@Data
public class ProductDocument {

@Id
@Field(type = FieldType.Keyword)
private String id;

@Field(type = FieldType.Text)
private String name;

@Field(type = FieldType.Text)
private String description;

@Field(type = FieldType.Boolean)
private boolean hidden;

@Field(type = FieldType.Boolean)
private boolean removed;

@Field(type = FieldType.Long)
private Long oldPrice;

@Field(type = FieldType.Long)
private Long price;

我尝试了以下操作:

    BoolQueryBuilder builder = boolQuery();

    builder.must(termQuery("hidden", false))
            .must(termQuery("removed", false))
            .must(nestedQuery("products", termQuery("products.hidden", false), ScoreMode.None))
            .must(nestedQuery("products", termQuery("products.removed", false), ScoreMode.None));

            SearchHits<ShopDocument> searchHits = operations.search(qb.build(), ShopDocument.class, IndexCoordinates.of(Indices.SHOP_INDEX));

此查询应返回所有 hiddenremoved 等于 false 的商店code> 以及商店中具有 hiddenremoved 的所有产品等于 false,但它只会过滤掉商店并保留产品原样。提前致谢!

I have 2 documents, ShopDocument and ProductDocument. What i need is to filter out all shops that have field hidden and removed equals to true, from shops that left filter out products that have removed and hidden fields equal to true, then from whats left, i want to filter out shops and products that doesnt match certain string (the matching needs to be in certain fields) and sort the results according to score that i will give to each field (like in lucene). This is my documents:

@Document(indexName = Indices.SHOP_INDEX)
@Setting(settingPath = Constants.esSettingsPath)
@Data
public class ShopDocument {

@Id
@Field(type = FieldType.Keyword)
private Long id;

@Field(type = FieldType.Keyword)
private Long companyId;

@Field(type = FieldType.Boolean)
private Boolean companyHidden;

@Field(type = FieldType.Keyword)
private String slug;

@Field(type = FieldType.Keyword)
private String postalCode;

@Field(type = FieldType.Keyword)
private String addressLine;

@Field(type = FieldType.Boolean)
private boolean hidden;

@Field(type = FieldType.Boolean)
private boolean removed;

@Field(type = FieldType.Text)
private String name;

@Field(type = FieldType.Nested)
private List<ProductDocument> products = new ArrayList<>();

@Field(type = FieldType.Double)
private Double latitude;

@Field(type = FieldType.Double)
private Double longitude;

@Field(type = FieldType.Integer)
private Integer maxDeliveryDistance;
}



@Document(indexName = Indices.Product_INDEX)
@Setting(settingPath = Constants.esSettingsPath)
@Data
public class ProductDocument {

@Id
@Field(type = FieldType.Keyword)
private String id;

@Field(type = FieldType.Text)
private String name;

@Field(type = FieldType.Text)
private String description;

@Field(type = FieldType.Boolean)
private boolean hidden;

@Field(type = FieldType.Boolean)
private boolean removed;

@Field(type = FieldType.Long)
private Long oldPrice;

@Field(type = FieldType.Long)
private Long price;

I had tried the following:

    BoolQueryBuilder builder = boolQuery();

    builder.must(termQuery("hidden", false))
            .must(termQuery("removed", false))
            .must(nestedQuery("products", termQuery("products.hidden", false), ScoreMode.None))
            .must(nestedQuery("products", termQuery("products.removed", false), ScoreMode.None));

            SearchHits<ShopDocument> searchHits = operations.search(qb.build(), ShopDocument.class, IndexCoordinates.of(Indices.SHOP_INDEX));

This query should return all shops with hidden and removed equals to false and all products in shops that have hidden and removed equals to false but it only filters out shops and leaves the products as is. Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人生百味 2025-01-18 21:33:36

您必须调用NestedQueryBuilder 类的innerHits 方法。
此方法仅检索匹配的嵌套对象。
因此你的代码变成:

BoolQueryBuilder builder = boolQuery();
builder.must(termQuery("hidden", false))
       .must(termQuery("removed", false))
       .must(nestedQuery("products", termQuery("products.hidden", false), ScoreMode.None).innerHits(new InnerHitBuilder()));
       .must(nestedQuery("products", termQuery("products.removed", false), ScoreMode.None).innerHits(new InnerHitBuilder()));

SearchHits<ShopDocument> searchHits = operations.search(qb.build(), ShopDocument.class, IndexCoordinates.of(Indices.SHOP_INDEX));

You have to call the method innerHits of class NestedQueryBuilder.
This method retrieves just the nested objects that match.
Therefore your code becomes:

BoolQueryBuilder builder = boolQuery();
builder.must(termQuery("hidden", false))
       .must(termQuery("removed", false))
       .must(nestedQuery("products", termQuery("products.hidden", false), ScoreMode.None).innerHits(new InnerHitBuilder()));
       .must(nestedQuery("products", termQuery("products.removed", false), ScoreMode.None).innerHits(new InnerHitBuilder()));

SearchHits<ShopDocument> searchHits = operations.search(qb.build(), ShopDocument.class, IndexCoordinates.of(Indices.SHOP_INDEX));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文