过滤条件查询不起作用-Elasticseach和Java API [英] Filter terms query don't work - Elasticseach and the java api
问题描述
我是stackoverflow的新手
I'm new to stackoverflow
这是我的问题
我想在ES中发出一个请求,该请求首先执行必须,然后执行过滤器.在sql中,其外观应与此类似
I want to make a request in ES that in first perform a must then a filter. In sql it should lookalike to this below
SELECT * FROM table WHERE name = "the_name" AND type IN ("type1", "type2")
这是我对这种情况的实施方式
Here is my implementation of this case
@Override
public List<Example> findByName(String pattern, String... types) {
// Prepare query
QueryBuilder queryBuilder = QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("name", pattern))
.filter(QueryBuilders.termsQuery("type", types));
// Execute query
return new ArrayList<>(this.elasticsearchQuery.findAgainstFields(Example.class, queryBuilder));
}
接下来,我对方法 findAgainstFields
@SuppressWarnings("unchecked")
@Override
public <T> List<T> findAgainstFields(Class<?> t, QueryBuilder query) {
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
NativeSearchQuery nativeSearchQuery = nativeSearchQueryBuilder
.withIndices("my_indice")
.withTypes(t.getSimpleName())
.withSort(new ScoreSortBuilder().order(SortOrder.DESC))
.withQuery(query)
.withPageable(PageRequest.of(0, 5)).build();
// Execute query
return (List<T>) this.elasticsearchTemplate.queryForList(nativeSearchQuery, t);
}
当我运行项目时,此方法( findByName )返回一个空数组.但是,当我删除 .filter(QueryBuilders.termsQuery("type",types))
时,我得到了结果.那么,我在哪一部分上做错了?
And when I run the project this method (findByName) return an empty array. However, when I remove the .filter(QueryBuilders.termsQuery("type", types))
i got a result. So, on which part i'm wrong?
预先感谢
ps:我使用ES 5.5和Java 8 oracle
ps: I use ES 5.5 and Java 8 oracle
更新
ES查询是这样:
{
"bool" : {
"must" : [
{
"term" : {
"name" : {
"value" : "eso",
"boost" : 1.0
}
}
}
],
"filter" : [
{
"terms" : {
"type" : [
"Society",
"Institut"
],
"boost" : 1.0
}
}
],
"disable_coord" : false,
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
因此,在这里,我尝试查找具有类型且可能是 Society 的 eso 的具有名称的数据或研究所
So here, i try to find the data with name as eso with type who can be Society or Institut
推荐答案
问题出在映射文件中,而不是Java源代码上.因此我通过将属性 type 的类型更改为关键字(之前是 text )来更新文件,并且一切正常.
the problem was in the mapping file not on java source code. so i updated the file by changing the type of the property type to keyword (it was text before) and everything works fine.
这篇关于过滤条件查询不起作用-Elasticseach和Java API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!