首页 > 代码库 > Elasticsearch的javaAPI之Query DSL-filters
Elasticsearch的javaAPI之Query DSL-filters
Elasticsearch的javaAPI之Query DSL-filters
和REST query dsl一样,elasticsearch提供了一个完整的Java query dsl。 工厂过滤器建造者是
如何构建查询?使用
import org.elasticsearch.index.query.FilterBuilders.*;
请注意,在
下面选取了几个常用的filer:
andfilter
FilterBuilders.andFilter(
FilterBuilders.rangeFilter("postDate").from("2010-03-01").to("2010-04-01"),
FilterBuilders.prefixFilter("name.second", "ba")
);
注意,您可以缓存结果,用
bool filter
FilterBuilders.boolFilter()
.must(FilterBuilders.termFilter("tag", "wow"))
.mustNot(FilterBuilders.rangeFilter("age").from("10").to("20"))
.should(FilterBuilders.termFilter("tag", "sometag"))
.should(FilterBuilders.termFilter("tag", "sometagtag"));
用
exists filter
FilterBuilders.existsFilter("user");
matchall filter
FilterBuilders.matchAllFilter();
notfilter
FilterBuilders.notFilter(
FilterBuilders.rangeFilter("price").from("1").to("2"));
or filter
FilterBuilders.orFilter(
FilterBuilders.termFilter("name.second", "banon"),
FilterBuilders.termFilter("name.nick", "kimchy")
);
query filter
FilterBuilders.queryFilter(
QueryBuilders.queryString("this AND that OR thus")
);
range filter
FilterBuilders.rangeFilter("age")
.from("10")
.to("20")
.includeLower(true)
.includeUpper(false);
// A simplified form using gte, gt, lt or lte
FilterBuilders.rangeFilter("age")
.gte("10")
.lt("20");
t
term filter
FilterBuilders.termFilter("user", "kimchy");
t
terms filter
FilterBuilders.termsFilter("user", "kimchy", "elasticsearch")
.execution("plain"); // Optional, can be also "bool", "and" or "or"
// or "bool_nocache", "and_nocache" or "or_nocache"
cache
默认情况下,一些过滤器缓存,一些不缓存缓存。 你可以有一个好的控制使用cache(boolean)
FilterBuilder filter=FilterBuilders.andFilter(
FilterBuilders.rangeFilter("postDate").from("2010-03-01").to("2010-04-01"),
FilterBuilders.prefixFilter("name.second", "ba")
)
.cache(true);
和REST query dsl一样,elasticsearch提供了一个完整的Java query dsl。 工厂过滤器建造者是
FilterBuilders
一旦准备好您的查询,就可以使用query api。如何构建查询?使用
FilterBuilders,
导入它们在你的类:import org.elasticsearch.index.query.FilterBuilders.*;
请注意,在
FilterBuilder
对象上可以轻松地打印(又名调试) 查询生成的JSON,用 toString()
方法。 下面选取了几个常用的filer:
andfilter
FilterBuilders.andFilter(
FilterBuilders.rangeFilter("postDate").from("2010-03-01").to("2010-04-01"),
FilterBuilders.prefixFilter("name.second", "ba")
);
注意,您可以缓存结果,用
AndFilterBuilder#cache(boolean)
method方法。bool filter
FilterBuilders.boolFilter()
.must(FilterBuilders.termFilter("tag", "wow"))
.mustNot(FilterBuilders.rangeFilter("age").from("10").to("20"))
.should(FilterBuilders.termFilter("tag", "sometag"))
.should(FilterBuilders.termFilter("tag", "sometagtag"));
用
BoolFilterBuilder#cache(boolean)
method缓存结果exists filter
FilterBuilders.existsFilter("user");
matchall filter
FilterBuilders.matchAllFilter();
notfilter
FilterBuilders.notFilter(
FilterBuilders.rangeFilter("price").from("1").to("2"));
or filter
FilterBuilders.orFilter(
FilterBuilders.termFilter("name.second", "banon"),
FilterBuilders.termFilter("name.nick", "kimchy")
);
用
OrFilterBuilder#cache(boolean)
method缓存结果query filter
FilterBuilders.queryFilter(
QueryBuilders.queryString("this AND that OR thus")
);
用
QueryFilterBuilder#cache(boolean)
method缓存结果range filter
FilterBuilders.rangeFilter("age")
.from("10")
.to("20")
.includeLower(true)
.includeUpper(false);
// A simplified form using gte, gt, lt or lte
FilterBuilders.rangeFilter("age")
.gte("10")
.lt("20");
用
RangeFilterBuilder#cache(boolean)
method 缓存结果t
term filter
FilterBuilders.termFilter("user", "kimchy");
你可以使用
TermFilterBuilder#cache(boolean)
method来不适用缓存.t
terms filter
FilterBuilders.termsFilter("user", "kimchy", "elasticsearch")
.execution("plain"); // Optional, can be also "bool", "and" or "or"
// or "bool_nocache", "and_nocache" or "or_nocache"
你可以不使用缓存,用
TermsFilterBuilder#cache(boolean)
methodcache
默认情况下,一些过滤器缓存,一些不缓存缓存。 你可以有一个好的控制使用cache(boolean)
方法时存在。 例如:FilterBuilder filter=FilterBuilders.andFilter(
FilterBuilders.rangeFilter("postDate").from("2010-03-01").to("2010-04-01"),
FilterBuilders.prefixFilter("name.second", "ba")
)
.cache(true);
Elasticsearch的javaAPI之Query DSL-filters
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。