(20) according to specified conditions ElasticSearch java projects delete documents and query all documents

  1, delete the index1, title documents that contain plant

@Test
     public  void testQueryDelete () throws IOException, InterruptedException, ExecutionException {
         // specify the cluster 
        Settings Settings = Settings.builder () PUT ( "cluster.name", "My-the Application." ) .Build (); 
         // create a client end 
        TransportClient Client = new new PreBuiltTransportClient (Settings) 
                                .addTransportAddress ( new new TransportAddress (InetAddress.getByName ( "192.168.43.151"), 9300 ));
         // delete index1 in, title of the document that contains the factory 
        BulkByScrollResponse the Response = DeleteByQueryAction.INSTANCE
                                        .newRequestBuilder (Client)
                                        .filter (QueryBuilders.matchQuery ( "title", "factory" )) 
                                        .source ( "index1" ) 
                                        .get (); 
        // returns the number of deleted 
        Long COUNT = response.getDeleted (); 
        System.out.println ( COUNT); 
        client.close (); 
   }

  2, query all documents under index1, show only three

@Test
     public  void testMatchAll () throws IOException, InterruptedException, ExecutionException {
         // specify the cluster 
        Settings Settings = Settings.builder () PUT ( "cluster.name", "My-the Application." ) .Build (); 
         // create a client end 
        TransportClient Client = new new PreBuiltTransportClient (Settings) 
                                .addTransportAddress ( new new TransportAddress (InetAddress.getByName ( "192.168.43.151"), 9300 ));
         // create a query object 
        QueryBuilder qb = QueryBuilders.matchAllQuery ();
         // execute the query
        SR = client.prepareSearch SearchResponse ( "index1" ) 
                                  .setQuery (qb) 
                                  .setSize ( 3) // check out the three 
                                  .get ();
         // get the result 
        SearchHits Hits = sr.getHits ();
         for (SearchHit HIT: Hits) {
             // output json 
            System.out.println (hit.getSourceAsString ());
             // output json with the key value 
            the Map <String, Object> Map = hit.getSourceAsMap ();
             for (String key: map.keySet ()) { 
                System.out.println (Key+"="+map.get(key));
            }
        }
        client.close();
   }

Guess you like

Origin www.cnblogs.com/javasl/p/12070422.html