Elasticsearch Java api-Multi-condition query (MUST)

Source: Internet
Author: User

Multi-condition settings
Multi-condition settings Matchphrasequerybuilder mpq1 = Querybuilders                . Matchphrasequery ("Pointid", "W3". unit1.10lbg01cp301 "); Matchphrasequerybuilder mpq2 = Querybuilders                . Matchphrasequery ("Inputtime", "2016-07-21 00:00:01"); QueryBuilder QB2 = Querybuilders.boolquery (). Must (MPQ1). must (MPQ2);

Demo:

Package com.zx.znyd.es; Import Java.io.ioexception;import java.util.date;import Java.util.hashmap;import Java.util.map;import Org.apache.commons.lang.stringutils;import Org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest ; Import Org.elasticsearch.action.get.getresponse;import Org.elasticsearch.action.index.indexrequest;import Org.elasticsearch.action.search.searchresponse;import Org.elasticsearch.action.search.searchtype;import Org.elasticsearch.client.client;import Org.elasticsearch.client.requests;import Org.elasticsearch.client.transport.transportclient;import Org.elasticsearch.common.mustache.mustache;import Org.elasticsearch.common.settings.immutablesettings;import Org.elasticsearch.common.settings.settings;import Org.elasticsearch.common.transport.inetsockettransportaddress;import Org.elasticsearch.common.transport.transportaddress;import Org.elasticsearch.common.xcontent.XContentBuilder; Import Org.elasticsearch.common.xcontent.xcontentfactory;import ORG.ELASTICSEArch.index.query.matchquerybuilder.operator;import Org.elasticsearch.index.query.querybuilders;import Org.elasticsearch.search.searchhit;import Org.elasticsearch.search.searchhits;import Org.elasticsearch.search.sort.sortorder;import Org.junit.test;import Org.junit.runner.runwith;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.beans.factory.annotation.qualifier;import Org.springframework.test.context.contextconfiguration;import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner; Import Com.alibaba.fastjson.jsonobject;import Com.zx.znyd.common.md5;import com.zx.znyd.common.SpringContextUtil; Import Com.zx.znyd.dao.autoreplydao;import Com.zx.znyd.data.model.autoreply;import Com.zx.znyd.data.model.logindex;import Com.zx.znyd.service.AutoReplyDeployService; @RunWith (Springjunit4classrunner.class) @ContextConfiguration (locations = "Classpath:applicationContext.xml") public class TestES {@Autowired @Qualifier ("esclient") Private CLient client;        @Test public void Testsearch () {String index= "index_log_daily";        String type= "Uq_log";        String uquestion = "139";        String province = "Jiangsu"; SearchResponse SearchResponse = Client.preparesearch (index). Settypes (Type). Setquery (Quer Ybuilders.matchallquery ())//query all. Setquery (Querybuilders.boolquery (). Must (querybuilders.ma Tchquery ("Uquestion", uquestion)//query uquestion for 139. Must (Querybuilders.matchquery ("province", province))/                 /Query province for Jiangsu Setsearchtype (Searchtype.query_then_fetch). Setfrom (0). SetSize (10)//Paging                    . AddSort ("Age", sortorder.desc)//Sort. get ();         Searchhits hits = Searchresponse.gethits ();         Long total = Hits.gettotalhits ();         SYSTEM.OUT.PRINTLN (total);         searchhit[] searchhits = Hits.hits (); for (Searchhit s:searchhits) {             System.out.println (S.getsourceasstring ());                     String []logindex=s.getsourceasstring (). Split (","); }     } }

Paste My Code:

       /** * ERROR Log Statistics */@Overridepublic list<tomcatmodel> errortomcatrequest (Page page, String index,string StartDate, String endDate) {transportclient client = Getclient (); if (index.isempty () = = true) {return null;} DateInterval DateInterval = regexutil.getdateinterval (new DateInterval (startdate,enddate), "YyyyMMdd"); list<tomcatmodel> list = new arraylist<tomcatmodel> (); try {searchresponse response = Client.preparesearch ( Index). Settypes (Getindextype ()). Setfrom (Page.getstart ()). SetSize (Page.getlength ()). Setquery ( Querybuilders.boolquery (). Must (Querybuilders.regexpquery ("Response", "[4-6][0-9][0-9]")). Must ( Querybuilders.rangequery ("@timestamp"). Format ("YyyyMMdd"). From (Dateinterval.getstartdate ()). to ( Dateinterval.getenddate ())). AddSort ("@timestamp", SORTORDER.ASC). Execute (). Actionget (); Searchhits myhits = response.gethits ();p age.setrecordsfiltered ((int) myhits.gettotalhits ());p Age.setrecordstotal (( int) myhits.gettotalhits ()); int i = 1;for (Searchhit hit:myhits.getHits ()){Tomcatmodel tomcat = new Tomcatmodel (); map<string, object> map = Hit.getsource (); Tomcat.setclientip ((String) map.get ("ClientIP"); tomcat.setresponse (string) map.get ("response"); Tomcat.setmessage ((String) map.get ("message"); Tomcat.settype ((String) Map.get (" Verb ") tomcat.settimestamp (String) map.get (" timestamp "); Tomcat.setrowid (i); List.add (tomcat); i++;}} catch (Indexnotfoundexception e) {System.err.println ("This index does not exist!");} catch (Exception e) {e.printstacktrace ();} return list;}

  

Elasticsearch Java api-Multi-condition query (MUST)

Related Article

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.