Elasticsearch's Javaapi query dsl-filters
and theLike the REST query DSL, Elasticsearch provides a complete Java query DSL. The Factory filter Builder is
FilterBuilders
Once you have your query ready, you can use the query API.
How do I build a query? Use
FilterBuilders,
import them in your class:
Import org.elasticsearch.index.query.filterbuilders.*;
Please note that the
FilterBuilder
Object can easily print (aka Debug) The JSON generated by the query, using the
toString()
Method.
Here are a few commonFiler
Andfilter
Filterbuilders.andfilter (
filterbuilders.rangefilter ("Postdate"). From ("2010-03-01"). to ("2010-04-01"),
filterbuilders.prefixfilter ("Name.second", "BA")
);
Note, you can cache the results, using the
AndFilterBuilder#cache(boolean)
Method.
BOOL Filter
Filterbuilders.boolfilter ()
. Must (Filterbuilders.termfilter ("tag", "Wow"))
. Mustnot ("Filterbuilders.rangefilter"). From ("Ten") . to (" a"))
. Should (Filterbuilders.termfilter ("tag", "Sometag"))
. Should (Filterbuilders.termfilter ("tag", "Sometagtag"));
Use
BoolFilterBuilder#cache(boolean)
Method Cache Results
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 Cache Results
Query filter
Filterbuilders.queryfilter (
querybuilders.querystring ("This and that OR thus")
);
用
QueryFilterBuilder#cache(boolean)
Method Cache Results
Range Filter
filterbuilders.rangefilter ("Age")
. From ("Ten")
. to (" a")
. Includelower (True)
. Includeupper (false);
//A simplified form using GTE, GT, LT or LTE
filterbuilders.rangefilter ("Age")
. GTE ("ten")
. LT ("a");
用
RangeFilterBuilder#cache(boolean)
Method Cache Results
T
Term filter
Filterbuilders.termfilter ("User", "Kimchy");
你可以使用
TermFilterBuilder#cache(boolean)
method does not apply to caching.
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)
Method
Cache
By default, some filters are cached, some do not cache. You can have a good control of usingCache (Boolean)
method is present. For example:
FilterbuilderFilter=filterbuilders.Andfilter(
filterbuilders.rangefilter ("Postdate"). From ("2010-03-01"). to ("2010-04-01"),
filterbuilders.prefixfilter ("Name.second", "BA")
)
. Cache (TRUE);
Elasticsearch's Javaapi query dsl-filters