简体   繁体   English

查询Spring Data Elasticsearch的嵌套属性

[英]Querying Spring Data Elasticsearch for nested properties

I am trying to query spring data elasticsearch repositories for nested properties. 我正在尝试查询spring数据elasticsearch存储库中的嵌套属性。 My Repository looks like this: 我的存储库如下所示:

public interface PersonRepository extends
        ElasticsearchRepository<Person, Long> {

    List<Person> findByAddressZipCode(String zipCode);
}

The domain objects Person and Address (without getters/setters) are defined as follows: 域对象Person和Address(没有getter / setter)的定义如下:

@Document(indexName="person")
public class Person {
  @Id
  private Long id;

  private String name;

  @Field(type=FieldType.Nested, store=true, index = FieldIndex.analyzed)
  private Address address;
}

public class Address {
  private String zipCode;
}

My test saves one Person document and tries to read it with the repository method. 我的测试保存了一个Person文档,并尝试使用存储库方法读取它。 But no results are returned. 但是没有结果返回。 Here is the test method: 这是测试方法:

@Test
public void testPersonRepo() throws Exception {
    Person person = new Person();
    person.setName("Rene");
    Address address = new Address();
    address.setZipCode("30880");
    person.setAddress(address);
    personRepository.save(person);
    elasticsearchTemplate.refresh(Person.class,true);
    assertThat(personRepository.findByAddressZipCodeContaining("30880"), hasSize(1));
}

Does spring data elasticsearch support the default spring data query generation? spring数据elasticsearch是否支持默认的spring数据查询生成?

Elasticsearch indexes the new document asynchronously... near real-time . Elasticsearch异步索引新文档... 接近实时 The default refresh is typically 1s I think. 我认为默认刷新通常为1s。 So you must explicitly request a refresh (to force a flush and the document available for search) if you are wanting the document immediately searchable as with a unit test. 因此,如果您希望像单元测试一样可以立即搜索文档,则必须明确请求刷新(以强制刷新并提供文档以供搜索)。 So your unit test needs to include the ElasticsearchTemplate bean so that you can explicitly call refresh . 因此,您的单元测试需要包括ElasticsearchTemplate bean,以便可以显式调用refresh Make sure you set waitForOperation to true to force a synchronous refresh. 确保将waitForOperation设置为true以强制执行同步刷新。 See this related answer . 请参阅此相关答案 Kinda like this: 有点像这样:

elasticsearchTemplate.refresh("myindex",true);

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

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