ElasticSearch (12) Bool Query JAVA API

Source: Internet
Author: User

Bool query is used to combine default queries for multiple leaf or compound query clauses

Must equivalent to & =

Must not equal to ~! =

should equivalent to or | Or

Filter filters

Boolquery ()        .  Must(termquery ("Content", "test1"))               
        . must (Termquery ("Content", "test4"))                 
        . Mustnot (Termquery ("Content", "test2"))              
        . should (Termquery ("Content", "test3"))               
        . Filter (Termquery ("Content", "test5"));


Private SearchRequest Getsearchrequest (RequestType request) {

searchrequest sr = new SearchRequest ();

Specifies the index name, if unspecified, to query all
Sr.indices (Elasticsearchconst.xxx_index);
Sr.types (Elasticsearchconst.xxx_type);

Searchsourcebuilder Source = new Searchsourcebuilder ();
Boolquerybuilder BQ = Querybuilders.boolquery ();

Getquerybuilder (request, BQ);

From 0 onwards, including front and back boundaries is actually <= >=

Source.from (Request.getrowstart ());
Source.size (Request.getrowsize ());
Source.query (BQ);

Setsort (request, source);
Sr.source (source);
return SR;
}

private void Getquerybuilder (RequestType request, Boolquerybuilder Bq) {

Getorderquerybuilder (request, BQ);

Addpurchaseorderquerybuilder (request, BQ);
}

/**
* Query conditions
*
* @param request
* @param BQ
*/
private void Getorderquerybuilder (RequestType request, Boolquerybuilder Bq) {

Termquery exact match, no participle equals =

Type.childtype Multilayer type nesting, specific index structure, reference mapping

GET yourindexname/_mapping

Mapping:

{
"Yourindexname": {
"Mappings": {
"Type1": {
"Properties": {
"Childtype": {
"Properties": {
"Cedstatus": {
"Type": "Text",
"Fields": {
"keyword": {
"Type": "Keyword",
"Ignore_above": 256
}
}
},

Bq. must (querybuilders.termquery("type.childtype", Request.getorderid ()));
Rangequery range query, maximum range not exceeding 1w

 Bq.must (querybuilders.rangequery("type.childtype. Departuredate")
    . GTE (Dateutil.tosqltimestamp (Request.getdeparturedatestart (). GetTime ()). GetTime ())
   . LTE (Dateutil.tosqltimestamp (Request.getdeparturedateend (). GetTime ()). GetTime ());

querybuilders.matchphrasequery Matching query, no participle, equivalent to contains

Matchphrasequery is directly in the form of a phrase query, the order is not bad, and connected together

Pobq.must (querybuilders.matchphrasequery(
String.Format ("%s%s%s", PreFix, I, ". Vacationvbkpurchaseorderbaseinfodto.contactname"),
Request.getcontactname ()));

ElasticSearch (12) Bool Query JAVA API

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.