【问题标题】:Spring Data Elastic Search Nested Field and NativeSearchQueryBuilder.withFieldsSpring Data 弹性搜索嵌套字段和 NativeSearchQueryBuilder.withFields
【发布时间】:2016-04-06 05:44:48
【问题描述】:

当我使用 NativeSearchQueryBuilder.withFields(...) 方法时,我似乎无法返回嵌套字段。

这是我的父对象:

@Document(indexName = "inventory")
public class Inventory
{
  @Id
  private String id;
  @Field(type=FieldType.String)
  private String name;
  @Field(type=FieldType.Nested, index=FieldIndex.not_analyzed, store=true)
  private List<Model> models;
}

这是嵌套对象:

public class Model
{
   @Field(type=FieldType.String, index=FieldIndex.not_analyzed, store=true)
   private String model;
   @Field(type=FieldType.String, index=FieldIndex.not_analyzed, store=true)
   private Set<String> series;
}

还有查询

NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
nativeSearchQueryBuilder.withQuery(QueryBuilders.matchAllQuery());
nativeSearchQueryBuilder.withFields("models.series");
NativeSearchQuery nativeSearchQuery = nativeSearchQueryBuilder.build();
FacetedPage<Inventory> results = inventoryRepository.search(nativeSearchQuery);

结果 TotalElements = 529 但是 Content 中的每个 Object 看起来像这样(JSON 格式):

{
   "id":"d5f82880-15bc-45ed-8abb-ff97d0e45da9",
   "name": null,
   "models": null
}

如果我删除 withFields(...) 设置,我会返回:

{
   "id":"d5f82880-15bc-45ed-8abb-ff97d0e45da9",
   "name": "Cool Beans",
   "models": [
     {
       "model" : "foo",
       "series" : ["bar"]
     }
   ]
}

我尝试过模型、models.model、models.series、model、series。我无法让 withFields 与 NestedFields 一起使用。

有什么想法吗?

【问题讨论】:

  • 当您在查询中指定fields 时,它不会在响应中返回source。只有fields 出现在命中以获取您的记录值。我认为您正在查看值的来源

标签: java elasticsearch spring-data-elasticsearch


【解决方案1】:

我对弹性搜索字段的理​​解不正确。 rahulroc 告诉了我。

withFields 与源过滤不同。

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-fields.html

所以我实际上是在告诉 Spring Data ES 这样做:

curl localhost:9200/inventory/_search?pretty=true -d '
{
  "fields" : ["models.series"],
  "query" : {
     "match" : {"name" : "cool"}
  }
}'

当这就是我想要的时候

curl localhost:9200/inventory/_search?pretty=true -d '
{
  "_source" : ["models.series"],
  "query" : {
     "match" : {"name" : "cool"}
  }
}'

在我开始添加 NestedFields 之前,withFields 方法一直适用于我正在做的事情。而我目前使用的 Spring Data ES 的实现不支持源过滤。

源过滤最近刚刚添加到 Spring Data ES 2.0.0.RC1

【讨论】:

    猜你喜欢
    • 2023-03-02
    • 2020-06-14
    • 2018-06-08
    • 1970-01-01
    • 2021-01-21
    • 2020-02-25
    • 2020-10-12
    • 1970-01-01
    • 2016-01-02
    相关资源
    最近更新 更多