简体   繁体   English

Spring Data弹性搜索嵌套字段和NativeSearchQueryBuilder.withFields

[英]Spring Data Elastic Search Nested Field and NativeSearchQueryBuilder.withFields

I can't seem to get Nested Fields to return when I use the NativeSearchQueryBuilder.withFields(...) method. 使用NativeSearchQueryBuilder.withFields(...)方法时,似乎无法返回嵌套字段。

Here is my parent Object: 这是我的父对象:

@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;
}

And here is the nested Object: 这是嵌套的对象:

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;
}

And the Query 和查询

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

Resulting TotalElements = 529 But each Object in the Content Looks like this (in JSON format): 结果TotalElements = 529,但是内容中的每个对象看起来都像这样(JSON格式):

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

If I remove the withFields(...) setting, I get back: 如果删除withFields(...)设置,则会返回:

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

I've tried models, models.model, models.series, model, series. 我已经尝试过models,models.model,models.series,model,series。 I can't get withFields working with NestedFields. 我无法使用withNestedFields的withFields。

Any thoughts? 有什么想法吗?

My understanding of elastic search fields was incorrect. 我对弹性搜索字段的理​​解不正确。 rahulroc tipped me off. 拉乌洛洛克向我提示。

withFields is not the same as source filtering. withFields与源过滤不同。

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

So i'm effectively telling Spring Data ES to do this: 所以我有效地告诉Spring Data ES做到这一点:

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

When this is what I want 当这就是我想要的

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

The withFields approach worked for what I was doing up till I started adding NestedFields. 在我开始添加NestedFields之前,withFields方法一直有效。 And the current implementation of Spring Data ES that I'm using does not support source filtering. 我正在使用的Spring Data ES的当前实现不支持源过滤。

Source Filtering was just recently added to Spring Data ES 2.0.0.RC1 源过滤是最近才添加到Spring Data ES 2.0.0.RC1的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM