Elasticsearch's Javaapi facet,count,delete by query

Source: Internet
Author: User

Count of the Javaapi of Elasticsearch

The Count API allows you to easily execute a query that gets the number of matches that can span one or more index, one or more type.

Import static Org.elasticsearch.index.query.xcontent.filterbuilders.*;import static org.elasticsearch.index.query.xcontent.querybuilders.*;        Countresponse response = Client.preparecount ("Test"). Setquery (Termquery ("_type", "Type1"). Execute () . Actionget ();

For more information about calculation operations, see Rest Count Docs.

Threading Operations

The search API allows you to set up threads to perform operations so that the actual ginger execution API executes on the same node (the API executes on a shard that is assigned on the same server).

There are three kinds of threading modes, the NO_THREADS mode means that the query operation will be performed on the calling thread. The SINGLE_THREAD pattern means that the search operation will be performed on the local shards, which is the single different thread. THREAD_PER_SHARDmeans that the search operation will be performed on a different thread, for each local shard.

The default mode is SINGLE_THREAD .


Elasticsearch's Javaapi Delete by query

The Delete by Query API allows you to delete documents based on a query, from one or more based on index and one or more type. Here is an example:

Import static Org.elasticsearch.index.query.filterbuilders.*;import static org.elasticsearch.index.query.querybuilders.*;D Eletebyqueryresponse response = client.preparedeletebyquery ("Test") ). Setquery (Termquery ("_type", "Type1"). Execute (). Actionget ();

The facets of the javaapi of Elasticsearch

For Facets,elasticsearch provides a complete Java API, you can see facets Guide.

Use the factory built for facets builders ( FacetBuilders) to build, add something you want to calculate at the time of the query, and add it to your search request:

SearchResponse sr = node.client (). Preparesearch (). Setquery (/* Your query */). ADDFACET (/* Add a facet */ ). Execute (). Actionget ();

Note that you can add multiple facets. You can see the search Java app for more information.

Build facets requests, use FacetBuilders helpers, and simply import them:

Import org.elasticsearch.search.facet.facetbuilders.*;

Facets
Terms Facet

Here's how to use the term facet (Java API).

Prepare the FACET request

Here's how to create a facet request:

Facetbuilders.termsfacet ("F"). Field ("brand"). Size (10);
Using facet response
Import Facet Class
Import org.elasticsearch.search.facet.terms.*;
SR is here your searchresponse objecttermsfacet f = (Termsfacet) sr.getfacets (). Facetsasmap (). Get ("F");      F.gettotalcount ();      Total Terms Doc countf.getothercount ();    Not shown terms Doc countf.getmissingcount ();    Without term doc count//for each entryfor (Termsfacet.entry entry:f) {entry.getterm ();   term entry.getcount (); Doc Count}
Range facet

Prepare the FACET request

Here's how to create an example (facet request):

Facetbuilders.rangefacet ("F"). Field ("Price")//field to compute on. Addunboundedfrom (3)//From-infini     Ty to 3 (excluded). AddRange (3, 6)//from 3 to 6 (excluded). Addunboundedto (6); From 6 to +infinity

Use facet response

Import Facet Related classes:

Import org.elasticsearch.search.facet.range.*;
SR is here your searchresponse objectrangefacet f = (Rangefacet) sr.getfacets (). Facetsasmap (). Get ("F");//For each ENTR    Yfor (Rangefacet.entry entry:f) {entry.getfrom ();      Range from requested Entry.getto ();   Range to requested Entry.getcount ();     Doc Count Entry.getmin ();     Min value Entry.getmax ();    Max value Entry.getmean ();   Mean entry.gettotal (); Sum of Values}
Histogram facet

Here's how to use the histogram facet (Java API).

Prepare the FACET request

Here's how to create a facet request:

Histogramfacetbuilder facet = Facetbuilders.histogramfacet ("F"). Field ("Price"). Interval (1);
Using facet response

Import Facet related classes:

Import org.elasticsearch.search.facet.histogram.*;
SR is here your searchresponse objecthistogramfacet f = (Histogramfacet) sr.getfacets (). Facetsasmap (). Get ("F");//For E     Ach entryfor (histogramfacet.entry entry:f) {entry.getkey ();   Key (x-axis) entry.getcount (); Doc count (Y-axis)}
Date histogram facet

Here's how to use the date histogram facet (Java API).

Prepare the FACET request

Here is an example of how to create a facet request:

Facetbuilders.datehistogramfacet ("F"). Field ("date")//Your date field. Interval ("year"); can also use ' quarter ', ' Month ', ' Week ', ' Day ',//' hour ' and ' minute ' or ' notation like ' 1.5 H "or" 2w "
Using facet response

Import Facet related classes:

Import org.elasticsearch.search.facet.datehistogram.*;
SR is here your searchresponse objectdatehistogramfacet f = (Datehistogramfacet) sr.getfacets (). Facetsasmap (). Get ("F"    );//For each entryfor (Datehistogramfacet.entry entry:f) {entry.gettime ();   Date in MS since epoch (x-axis) entry.getcount (); Doc count (Y-axis)}
Filter (not facet filter)

Here's how to use the Facet filter (Java API).

If you are looking at how to apply a filter to facets, look at the facet filter in the Java API.

Prepare facet response

Here is an example of how to create a facet request:

Facetbuilders.filterfacet ("F", Filterbuilders.termfilter ("brand", "Heineken")); Your Filter Here

See filters to learn how to use build filters.

Using facet response

To import related classes:

Import org.elasticsearch.search.facet.filter.*;
SR is here your searchresponse objectfilterfacet f = (Filterfacet) sr.getfacets (). Facetsasmap (). Get ("F"); F.getcount ()   ; Number of docs that matched

Query facet

Here's how to use the query facet (Java API).

Prepare the FACET request

Here is an example of how to create a facet request:

Facetbuilders.queryfacet ("F", Querybuilders.matchquery ("brand", "Heineken"));
Import org.elasticsearch.search.facet.query.*;
SR is here your searchresponse objectqueryfacet f = (Queryfacet) sr.getfacets (). Facetsasmap (). Get ("F"); F.getcount (); Number of docs that matched

Statistical

Here is how to use the statistical facet (Java API).

Prepare the FACET request

Here is an example of how to create:

Facetbuilders.statisticalfacet ("F"). Field ("Price");
Using facet response
Import org.elasticsearch.search.facet.statistical.*;
SR is here your searchresponse objectstatisticalfacet f = (Statisticalfacet) sr.getfacets (). Facetsasmap (). Get ("F");           F.getcount ();             Doc countf.getmin ();             Min Valuef.getmax ();            Max Valuef.getmean ();           Meanf.gettotal ();    Sum of Valuesf.getstddeviation ();    Standard Deviationf.getsumofsquares ();        Sum of Squaresf.getvariance (); Variance

Terms stats Facet

Here is how to use the Terms stats facet (Java API).

Here is an example of how to create:

Facetbuilders.termsstatsfacet ("F"). Keyfield ("brand"). Valuefield ("price");
Using facet response
Importing related classes
Import org.elasticsearch.search.facet.termsstats.*;
SR is here your searchresponse objecttermsstatsfacet f = (Termsstatsfacet) sr.getfacets (). Facetsasmap (). Get ("F");      F.gettotalcount ();      Total Terms Doc countf.getothercount ();    Not shown terms Doc countf.getmissingcount ();            Without term doc count//for each entryfor (Termsstatsfacet.entry entry:f) {entry.getterm ();           term entry.getcount ();             Doc Count Entry.getmin ();             Min value Entry.getmax ();            Max value Entry.getmean ();           Mean entry.gettotal (); Sum of Values}

Geo distance Facet

Here's how to use the distance facet (Java API).

Here is an example of how to create:

Facetbuilders.geodistancefacet ("F"). Field ("Pin.location")//field containing coordinates we want to comp  is with. Point ( -70)//point from where we start (0). Addunboundedfrom (10)//                  0 to ten km (excluded). AddRange (Ten)//(excluded). AddRange (20, 100)    From $ to (excluded). Addunboundedto (+)//From: To infinity (and beyond;-))     . Unit (distanceunit.kilometers); All distances is in kilometers. Can be MILES
Using facet response
Import org.elasticsearch.search.facet.geodistance.*;
SR is here your searchresponse objectgeodistancefacet f = (Geodistancefacet) sr.getfacets (). Facetsasmap (). Get ("F");//            For each entryfor (Geodistancefacet.entry entry:f) {entry.getfrom ();              Distance from requested Entry.getto ();           Distance to requested Entry.getcount ();             Doc Count Entry.getmin ();             Min value Entry.getmax ();           Max value entry.gettotal ();            Sum of Values Entry.getmean (); Mean}
Facet Filter (not filter facet)

By default, facets are applied to the query result set regardless of whether the filter exists.

If you need to calculate facets in the same filter or other filters, you can add facets to any filter, using the AbstractFacetBuilder #facetFilter(FilterBuilder) method:

Facetbuilders. Termsfacet ("F"). Field ("brand")//Your facet. Facetfilter (//Your filter here filterbuilders. Termfilter ("colour", "pale"));

For example, you can reuse the same filter to create your query:

A Common Filterfilterbuilder filter = Filterbuilders.termfilter ("colour", "pale");                           Termsfacetbuilder facet = Facetbuilders.termsfacet ("F"). Field ("brand"). Facetfilter (filter); We apply it to the Facetsearchresponse sr = Node.client (). Preparesearch (). Setquery (Querybuilders.matchallquery ()). SetFilter (filter)//We apply it to the query. Addfacet (FACET). Execute (). actio Nget ();

Scope

By default, facets are evaluated in the query result set. But you can also calculate facets for the entire index, regardless of what the query is, using the global parameter:

Termsfacetbuilder facet = Facetbuilders.termsfacet ("F"). Field ("Brand"). Global (true);
Original:

Http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/java-facets.html#_use_facet_response_2

Http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/delete-by-query.html

Http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/count.html

I hope that the translation is not misleading.

Elasticsearch's Javaapi facet,count,delete by 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.