Filterbuilders Build Filter Query

Source: Internet
Author: User

Filterbuilders Build Filter Query

Package Com.elasticsearch;import Org.elasticsearch.action.actionlistener;import Org.elasticsearch.action.search.searchresponse;import Org.elasticsearch.common.geo.geodistance;import Org.elasticsearch.common.unit.distanceunit;import org.elasticsearch.index.query.*;/** * Created by LW on 14-7-16. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Elasticsearch to provide a complete Java query DSL for the rest of the query DSL. * Filterbuilders Factory Build * API: * <a>http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/ query-dsl-filters.html</a> * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */public class Es_ FILTERBUILDERS_DSL {/** * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * and filter * A filter matches a document     With the Boolean combination of other filters. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */protected static Filterbuilder Andfilter () {RET Urn Filterbuilders.andfilter (Filterbuilders.rangefilter ("age"). from (1) to (+), FilterbuiLders.prefixfilter ("name", "Gourd 1493"));     }/** * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * BOOL Filter * A filter matching the document matches the Boolean combination of other filters. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */protected static Filterbuilder Boolfilter () {RE Turn Filterbuilders.boolfilter (). Must (Filterbuilders.termfilter ("name", "Gourd 1493")). Mustno T (Filterbuilders.rangefilter ("age"). from (+) to (+)). Should (Filterbuilders.termfilter ("Home", Taiyuan City, Shanxi province 7    077 streets "));     }/** * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * exists filter * A filter to filter the fields where a unique field exists.        * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */protected static Filterbuilder Existsfilter () {    Return Filterbuilders.existsfilter ("Home");     }/** * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * IDs Filter * Creates a new ID filter that provides the doc/mapping type. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */protected static Filterbuilder Idsfilter () {return Filterbuilders.idsfilter (es_utils.index _demo_01_mapping, "type2"). Addids ("Snt0kdnbrdkmxjvaxfnxea", "Udkto4o9tgmdhit4bk_oww", "JKAZOHE9RWYJXYONBCT        DRW ");    Type is optional//filterbuilders.idsfilter (). Addids ("1", "4", "100"); }/** * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Limit filter * A filter to limit the limit values provided by the result (each shard     * 2). * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */protected static Filterbuilder Limitfilter () {R Eturn Filterbuilders.limitfilter (2);//Return fragment SHARD*2 results}/** * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ ~ ~ * Type Filter * Filter Type * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * */protected Stati    C Filterbuilder TypeFilter () {return filterbuilders.typefilter (es_utils.index_demo_01_mapping); }/** * TODO notsolved * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Geo bounding box filter * Defines a filter to filter positions/minutes based on the upper-left and lower-right corners of the bounding box * ~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */protected static Filterbuilder Geoboundingboxfilter () {return Filterbuilders.geoboundingboxfilter ("Pin.location"). TopLeft (40.73, -74.1). BottomRight (40.7    17,-73.99); }/** * TODO notsolved * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * geodistance filter *     A filter to filter based on a specific distance from a specific location/point.        * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */protected static Filterbuilder Geodistancefilter () { Return Filterbuilders.geodistancefilter ("Pin.location"). Point ( -70). Distance (200,                distanceunit.kilometers). Optimizebbox ("Memory")//Can be also "indexed" or "none"            . Geodistance (GEODISTANCE.ARC); Or Geodistance.plane}/** *TODO notsolved * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Geo Distance range filter * A filter to filter     Based on a specific range from a specific location/point.  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */protected static Filterbuilder Geodistancerangefilter () {return Filterbuilders.geodistancerangefilter ("Pin.location"). Point (at -70). From                ("200km"). to ("400km"). Includelower (True). Includeupper (False) . Optimizebbox ("Memory")//Can be also "indexed" or "none". Geodistance (geodistance.ar            C);     Or Geodistance.plane}/** * TODO notsolved * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     * Geo Polygon Filter * A filter to filter based on polygons defined as a set of positions/points.        * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */protected static Filterbuilder Geopolygonfilter () { Return Filterbuilders.geopolygonfilter ("PIN.LOcation "). Addpoint ( -70). Addpoint ( -80). Addpoint (20,-90); }/** * TODO notsolved * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * has Child/has parent fi     Lters * Constructs a sub-filter, sub-types and queries with documents, filtered results are parent * * documents.        * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */protected static Filterbuilder Haschildfilter () { Have child filterbuilders.haschildfilter ("Blog_tag", Querybuilders.termquery ("tag", "Something")        ); Has Parent return filterbuilders.hasparentfilter ("blog", Querybuilders.termquery ("tag", "Somethin    G "));     }/** * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Match all filter * A filter matches all files.        * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */protected static Filterbuilder Matchallfilter () {    return Filterbuilders.matchallfilter (); }    /**     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Missing Filter * A filter to filter fields a unique file does not exist.        * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */protected static Filterbuilder Missingfilter () {    Return Filterbuilders.missingfilter ("name"). Existence (True). Nullvalue (True); }/** * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * not filter * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                ~~~~~~~~~~~~~~~~~~~~~~~~~ */protected static Filterbuilder Notfilter () {return Filterbuilders.notfilter (    Filterbuilders.rangefilter ("Age"). from (+) to (2000)); }/** * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * or filter * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                ~~~~~~~~~~~~~~~~~~~~~~~~ */protected static Filterbuilder Orfilter () {return Filterbuilders.orfilter (        Filterbuilders.termfilter ("name", "Gourd 1493 Baby"), Filterbuilders.termfilter ("name", "Gourd 5083 Doll")   ); }/** * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Prefix filter * ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */protected static Filterbuilder Prefixfilter () {return filterbuilders.prefi    XFilter ("name", "Gourd 5083");     }/** * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Query Filter * A filter that wraps only one query. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */protected static Filterbuilder QueryFilter () {R    Eturn Filterbuilders.queryfilter (querybuilders.querystring ("name"));     }/** * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Range filter Filter to limit search result values within a given range. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */protected static Filterbuilder Rangefilter () {F                Ilterbuilders.rangefilter ("Age"). from (+) to (+). Includelower (True) . Includeupper (False);                A simplified form using GTE, GT, LT or LTE return Filterbuilders.rangefilter ("age"). GTE (1000)    . LT (2000);     }/** * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Script Filter * Filter is based on the construction of scripts.        * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */protected static Filterbuilder Scriptfilter () {    Return Filterbuilders.scriptfilter ("doc[' age '].value > param1"). AddParam ("param1", 1000); }/** * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Term filter * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~ */protected static Filterbuilder Termfilter () {return filterbuilders.termfilter    ("Name", "Gourd 5083 baby"); }/** * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Terms filter * ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ * The execution option now has the following options: * <p/> * Plain * the default. Works as today. Iterates over all the terms, building a bit set matching it, and filtering.     The total filter is cached.     * <p/> * Fielddata * Generates a terms filters that uses the Fielddata cache to compare terms. * This execution mode was great to use when filtering on a field that's already loaded into the Fielddata cache from facet     ing, sorting, or index warmers.     * When filtering on a large number of terms, this execution can is considerably faster than the other modes.     * The total filter isn't cached unless explicitly configured to doing so.     * <p/> * BOOL * Generates a term filter (which are cached) for each term, and wraps those in a bool filter.     * The bool filter itself is not cached as it can operate very quickly in the cached term filters.     * <p/> * and * Generates a term filter (which are cached) for each term, and wraps those in an and filter. * The and filter itself is Not cached.     * <p/> * or * Generates a term filter (which are cached) for each term, and wraps those in an or filter. * The or filter itself is not cached.     Generally, the bool execution mode should be preferred. */protected static Filterbuilder Termsfilter () {return filterbuilders.termsfilter ("name", "Gourd 5083 Baby", "Gourd 3582 Baby")     ). Execution ("plain"); Execution mode Cane be either "plain", "bool" "and".    Defaults to "plain". }/** * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Nested filter * ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */protected static Filterbuilder Nestedfilter () {return filterbuilders.nested  Filter ("Obj1", Querybuilders.boolquery (). Must (Querybuilders.matchquery ("Obj1.name",    "Blue"). Must (Querybuilders.rangequery ("Obj1.count"). GT (5))); }    /**     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ ~ * Caching * Cache filter * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */protected static Fi Lterbuilder cache () {return Filterbuilders.andfilter (Filterbuilders.rangefilter ("age"). from (1000).     to (9000), Filterbuilders.prefixfilter ("name", "Gourd 3582")). Cache (true);//Default False}        public static void Main (string[] args) {es_utils.startupclient ();        try {searchtest (cache ());        } catch (Exception e) {e.printstacktrace ();        } finally {es_utils.shutdownclient (); }} private static void Searchtest (Filterbuilder filterbuilder) {//pre-prepared to perform search Es_Utils.client.prepareSea RCH (es_utils.index_demo_01). Settypes (es_utils.index_demo_01_mapping). Setpostfilter (FilterB                Uilder). Setfrom (0). SetSize (. Setexplain (True). Execute ()//Register listener Events . adDlistener (New actionlistener<searchresponse> () {@Override public void Onrespo                    NSE (SearchResponse searchresponse) {es_utils.writesearchresponse (searchresponse);                } @Override public void OnFailure (Throwable e) {}    }); }}

Filterbuilders Build Filter Query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.