Elasticsearch,java API, Transport Client, query when the index library can use wildcards * and delete interfaces can not be used

Source: Internet
Author: User

1, Get transport Client Connection instance
  /**     * Get the connection client instance of ES;     *     * @return transportclient Object     *    /private static transportclient gettransportclient (String esIp) throws IOException {        Settings Settings = Settings.builder ()                . Put (cluster_name, "xxx")                . Put (ELASTIC. Client_transport_sniff, True)                . Build ();        Transportclient transportclient = Transportclient.builder (). settings (Settings). build ();        String[] IPs =esip.split (",");        for (String ip:ips) {            try {                inetsockettransportaddress ist = new Inetsockettransportaddress ( Inetaddress.getbyname (IP), elastic.es_transportclient_port);                Transportclient.addtransportaddress (IST);            } catch (Exception e) {            }        }        return transportclient;    }

  

2, encapsulates the query object, can be used with the * Wildcard index library, and set the maximum number of returns
  @Test public void Querysearch () throws IOException {long L = system.currenttimemillis ();        Transportclient transportclient = Factory.gettransportclient (); Encapsulates the query condition for aggregations rangequerybuilder Rangequerybuilder = Querybuilders.rangequery (Time_field). from (StartTime). to (EndTime)        ;       Matchquerybuilder Matchquerybuilder = querybuilders.matchquery ("xx", "xxx");        QueryBuilder QueryBuilder = Querybuilders.matchallquery (); Boolquerybuilder Boolquerybuilder = new Boolquerybuilder (). Must (Rangequerybuilder). must (Matchquerybuilder);//using wildcard characters        ' * ' to prefix all index libraries SearchResponse response=transportclient.preparesearch ("index*") that match the beginning of index. Settypes ("TYPE") Set query type//1.searchtype.dfs_query_then_fetch = exact query//2.searchtype.scan = Scan query, unordered//3.SearchTy Pe. COUNT = Not set, this is the default value, and some of them to try it. Setsearchtype (Searchtype.dfs_query_then_fetch)//Set query keywords. setquery (b Oolquerybuilder)//Set the location of the query data, paging with. sEtfrom (0)//sets the maximum number of bars for the query result set. SetSize (1000)//sets whether to sort by query matching. Setexplain (TRUE)///finally return to the search ring        should be information. Execute (). Actionget (); Searchhits searchhits=response.gethits ();//System.out.println ("-----------------in [" +term+ "] Search for keywords [" +querystring        +"]---------------------");        System.out.println (System.currenttimemillis ()-L);        System.out.println ("co-match to:" +searchhits.gettotalhits () + "strip record!");        Searchhit[]hits=searchhits.gethits ();            for (Searchhit searchhit:hits) {map<string, object> sourceasmap=searchhit.sourceasmap ();            Set<string> Keyset=sourceasmap.keyset (); The for (String string:keyset) {//key value values correspond to System.out.println (string+ ":" +sourceasmap.            Get (string));        } System.out.println ();  }    }
3, delete index document interface according to ID, single and batch, delete interface index library cannot match with wildcard ' * '

1, single ID delete index document

Index: Indexed library name (cannot be used with wildcards), type: Index library type, ID index library ID  deleteresponse deleteresponse = Client.preparedelete ("index", " TYPE "," ID "). get ();       Successfully removed if (Deleteresponse.isfound ()) {    successes.add (rowkey);} else {    failedes.add (rowkey);  }

2. Bulk Delete index document according to IDs

@Test
public void Removedatafromes () throws IOException { arraylist<string> xxx= new arraylist<> (); Xxx.add ("201801001"); Xxx.add ("201801001"); Transportclient client = Factory.gettransportclient (); Bulkrequestbuilder Builder=client.preparebulk (); for (String id:xxx) { System.out.println (ID); Builder.add (Client.preparedelete ("INDEX", "TYPE", id). request ());
}}bulkresponse bulkresponse = Builder.get ();

2,elasticseach performance is not good when searching for data based on the time of the index.

Elasticsearch,java API, Transport Client, query when the index library can use wildcards * and delete interfaces can not be used

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.