簡體   English   中英

查詢Spring Data Elasticsearch的嵌套屬性

[英]Querying Spring Data Elasticsearch for nested properties

我正在嘗試查詢spring數據elasticsearch存儲庫中的嵌套屬性。 我的存儲庫如下所示:

public interface PersonRepository extends
        ElasticsearchRepository<Person, Long> {

    List<Person> findByAddressZipCode(String zipCode);
}

域對象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;
}

我的測試保存了一個Person文檔,並嘗試使用存儲庫方法讀取它。 但是沒有結果返回。 這是測試方法:

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

spring數據elasticsearch是否支持默認的spring數據查詢生成?

Elasticsearch異步索引新文檔... 接近實時 我認為默認刷新通常為1s。 因此,如果您希望像單元測試一樣可以立即搜索文檔,則必須明確請求刷新(以強制刷新並提供文檔以供搜索)。 因此,您的單元測試需要包括ElasticsearchTemplate bean,以便可以顯式調用refresh 確保將waitForOperation設置為true以強制執行同步刷新。 請參閱此相關答案 有點像這樣:

elasticsearchTemplate.refresh("myindex",true);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM