全文elasticsearch填充嵌套对象并按字段分数排序命中
我有 2 个文档,ShopDocument 和 ProductDocument。我需要的是过滤掉所有字段 hidden
和 removed
等于 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));
此查询应返回所有 hidden
和 removed
等于 false
的商店code> 以及商店中具有 hidden
和 removed
的所有产品等于 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须调用
NestedQueryBuilder
类的innerHits
方法。此方法仅检索匹配的嵌套对象。
因此你的代码变成:
You have to call the method
innerHits
of classNestedQueryBuilder
.This method retrieves just the nested objects that match.
Therefore your code becomes: