我需要使用NativeSearchQueryBuilder进行搜索,但是当我添加过滤器时,它无法正常工作
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(queryString(query))
.withFilter(FilterBuilders.termFilter("domain", "TEST"))
.build();
Page<Asset> asset =
elasticsearchTemplate.queryForPage(searchQuery,Asset.class);
return asset.getContent();
实体Asset.java
@Entity
@Table(name = "ASSET")
@Document(indexName = "asset")
public class Asset implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
@Size(max = 50)
@Column(name = "name", length = 50, nullable = false)
private String name;
@Column(name = "domain")
private String domain;
//getter and setter
}
测试数据
id;name;domain
1;name1;TEST
2;name2;TEST
3;name3;DEMO
4;name4;DEMO
5;name5;TEST
任何人都可以帮我找错误吗?
答案 0 :(得分:0)
我认为您的查询必须是:
Json版本:
{
"query": {
"filtered": {
"query": {
"query_string" : {
"default_field" : "content",
"query" : "this AND that OR thus"
}
},
"filter": {
"term": {
"domain": "TEST"
}
}
}
}
}
NativeSearchQueryBuilder
版本:
QueryBuilder boolQueryBuilder = queryStringQuery(query);
FilterBuilder filterBuilder = termFilter("domain", "TEST");
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(QueryBuilders.filteredQuery(boolQueryBuilder, filterBuilder);
Page<Asset> asset = elasticsearchTemplate.queryForPage(searchQuery, Asset.class);
return asset.getContent();