查询嵌套属性的Spring Data Elasticsearch

时间:2015-07-14 10:24:59

标签: java elasticsearch spring-data spring-data-elasticsearch

我正在尝试查询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 data elasticsearch是否支持默认的spring数据查询生成?

1 个答案:

答案 0 :(得分:0)

Elasticsearch异步索引新文档... near real-time。我认为默认刷新通常为1秒。因此,如果您希望文档可以像单元测试一样立即搜索,则必须明确请求刷新(强制刷新并且文档可用于搜索)。因此,您的单元测试需要包含ElasticsearchTemplate bean,以便您可以显式调用refresh。确保将waitForOperation设置为true以强制执行同步刷新。见related answer。有点像这样:

INTEGER :: ierr, myid, istatus(MPI_STATUS_SIZE), num, i, n
INTEGER,parameter :: seed = 86456, numbers=200 
INTEGER :: req1(numbers), req2(numbers)
LOGICAL :: flag

CALL MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierr)
IF (myid==0) THEN

    DO n=1, numbers

        req1(n)=0
        req2(n)=0
        num=IRAND()

        CALL MPI_ISEND(num,1,MPI_INTEGER,1,1,MPI_COMM_WORLD,req1(n),ierr)
        CALL MPI_IRECV(best_prime,1,MPI_INTEGER,1,0,MPI_COMM_WORLD,req2(n),ierr)

    END DO

ELSE IF (myid==1) THEN

    DO i=1, numbers
        CALL MPI_TEST(req2(i),flag,istatus,ierr)
        IF (flag .eqv. .false.) THEN
            WRITE(*,*)'RECV',i,'non-blocking FAIL'
        ELSE IF (flag .eqv. .true.) THEN
            WRITE(*,*)'RECV',i,'non-blocking SUCCESS'
        END IF
    END DO

END IF