Spring数据ElasticSearch中的自连接方案

cgvd09ve  于 2022-12-26  发布在  ElasticSearch
关注(0)|答案(1)|浏览(142)

如何在ElasticSearch中搜索自引用文档?

public class ProductDocument {
@Id
private String id;
private String title;
private List<String> tags;
//private List<ProductDocument> relatedProducts -- Doesn't work
private List<String> relatedProducts;
}

因此,在搜索时,我希望执行以下操作
x一个一个一个一个x一个一个二个x
怎么才能做到呢?我到处找了找,到目前为止什么也没找到。任何帮助都是非常感谢的

o0lyfsai

o0lyfsai1#

需要使用嵌套类型

public class ProductDocument {
  @Id
  private String id;
  private String title;
  private List<String> tags;

  // Use the nested datatype for the related products field
  @Field(type = FieldType.Nested)
  private List<ProductDocument> relatedProducts;
}

然后,在执行搜索时,可以使用嵌套查询在相关产品字段中搜索匹配项。以下是如何执行此操作的示例:

{
  "query": {
    "nested": {
      "path": "relatedProducts",
      "query": {
        "multi_match" : {
          "query": "cloths", 
          "fields": ["title", "tags"]
        }
      }
    }
  }
}

相关问题