Spring elasticsearch过滤条件使用列表

时间:2017-07-04 12:19:27

标签: elasticsearch spring-boot spring-data-elasticsearch

我有以下索引和查询

@Document(indexName="idx", type="worker")
public class Worker {
    @Id
    @Field(type = FieldType.Long)
    private Long id;
    @Field(type = FieldType.String)
    private String firstName;
    @Field(type = FieldType.String)
    private String lastName;
    --getter--setter
}


public interface WRepo extends ElasticsearchRepository<Worker, Long> {

    @Query("{
        "query": {"match": {"firstName": "?0"}},
        "filter": {"terms" : {"id" : ***ids***}}
    }")
    Page<Worker> searchWorker(String firstName, List<Long> ids, Pageable pageable);
}

当添加静态像

时,id的数量是可变的

{“id”:[101,102,103]}

它按预期工作,我的问题是如何从方法签名中传递id列表,如 firstName 字段

{“firstName”:“?0”}

1 个答案:

答案 0 :(得分:0)

我改变了下面的方法体

@Query("{
    "query": {"match": {"firstName": "?0"}},
    "filter": {"terms" : {"id" : [?1]}}
}")
Page<Worker> searchWorker(String firstName, String ids, Pageable pageable);

并将我的请求作为:

repository.searchProduct("firstName", "101,102", pageable)