(23) ElasticSearch java projects in all the fields in the query and filter documents --queryStringQuery simpleQueryStringQuery

  1, all the fields in the query contains changge document does not contain hejiu

@Test
     public  void testQueryStringQuery () 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, all fields in the query contains changge document does not contain hejiu 
        QueryBuilder qb QueryBuilders.queryStringQuery = ( "+ Changge -hejiu" );
        // perform queries 
        the SearchResponse client.prepareSearch SR = ( "lib3" ) 
                                  .setQuery (QB) 
                                  .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 ()) { 
                the System.out .println (Key + "=" + as map.get (Key)); 
            } 
        }
        client.close();
   }

  2, all the fields in the query contains or does not contain hejiu changge documents

@Test
     public  void testSimpleQueryStringQuery () 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, all fields in the query contains or does not contain hejiu changge documents 
        QueryBuilder qb = QueryBuilders.simpleQueryStringQuery ( "+ changge -hejiu");
         // execute the query 
        SearchResponse SR = client.prepareSearch ( "lib3" ) 
                                  .setQuery (qb) 
                                  .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 ()) { 
                the System .out.println (Key + "=" +  as map.get (Key));
            }
        }
        client.close();
   }

Guess you like

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