elasticsearch java index CRUD

1.创建索引并插入数据
Map<String, Object> json = new HashMap<String, Object>();
json.put("user", "kimchy5");
json.put("postDate", new Date());
json.put("message", "trying out Elasticsearch");
//参数设置
//Settings settings = Settings.settingsBuilder().put("cluster.name", "elasticsearch").build();
//TransportClient client = TransportClient.builder().settings(settings).build();
Client client = TransportClient.builder().build()
        .addTransportAddress(newInetSocketTransportAddress (InetAddress.getByName ( "127.0.0.1"), 9300 ));
 // first parameter: the name index; second parameter: index type; third parameter: Review index data ID (the same id, The default for the random string) 
indexResponse indexResponse = client.prepareIndex ( "Twitter", "JSON", ". 1" ) .setSource (JSON) .get (); 
System.out.println (indexResponse); 
// do not specify the id generating a random
 // . client.prepareIndex ( "Twitter", "Tweet") setSource (JSON); 
client.close ();

 2 . bulk index data is inserted (insertion high efficiency)
 // bulk insert data, delete, and modify also can be, for example without the 
BulkRequestBuilder bulkRequest = client.prepareBulk (); 
IndexRequest Request = client.prepareIndex ( "Twitter", "Tweet", ". 1" ) .setSource (JSON).request();
IndexRequest request2 = client.prepareIndex("twitter", "tweet", "2").setSource(json2).request();
bulkRequest.add(request);
bulkRequest.add(request2);
bulkRequest.execute().actionGet();

3.判断索引是否存在
IndicesExistsRequest inExistsRequest = new IndicesExistsRequest("twitter");
IndicesExistsResponse inExistsResponse = client.admin().indices().exists(inExistsRequest).actionGet();
System.out.println("索引twitter是否存在:"+inExistsResponse.isExists());

4.删除索引
//删除索引twitter
client.admin().indices().prepareDelete("twitter").execute().actionGet();
//A document (a data) delete the index 
client.prepareDelete ( "twitter", "Tweet", "1" );

 5 . Query index database 
Client Client = TransportClient.builder () Build (). 
        .AddTransportAddress ( new new InetSocketTransportAddress (InetAddress.getByName ( "127.0.0.1"), 9300 )); 
 
// the first argument: what to look for second, three parameters: to find the field 
QueryBuilder qb = QueryBuilders.multiMatchQuery ( "noisy", " Content "," the Message " );
 // twitter: index name tweet: index type 
SearchResponse the Response = client.prepareSearch (" twitter. ") setTypes (" Tweet " ) .setQuery (qb)
 // set query types SearchType.QUERY_AND_FETCH: all acquisition
                .setSearchType (SearchType.DFS_QUERY_AND_FETCH) .execute () actionGet ();. 
 
SearchHits Hits = response.getHits ();
 IF (hits.totalHits ()> 0 ) {
     for (SearchHit HIT: Hits) { 
        System.out.println ( "Score:" hit.getScore + () + ": \ T" + hit.getSource ()); // .get ( "title") 
    } 
} the else { 
    System.out.println ( "0 to search results for" ); 
} 
client.close ();

 . 6 . index refresh
 // refresh all 
client.admin () indices () prepareRefresh () GET ();...
 // refresh one 
client.admin () indices ().. prepareRefresh ( "twitter") .get ();
 // refresh a type 
client.admin () indices () prepareRefresh ( "twitter", "Tweet.". ) .get ();

 7 . to update the index data
 // update the index (the index, type, ID) 
UpdateRequest = updateRequest to new new UpdateRequest (); 
updateRequest.index ( "Twitter" ); 
updateRequest.type ( "Tweet" ); 
updateRequest.id ( ". 1" ); 
updateRequest.doc (jsonBuilder (). StartObject () .field ( "Content", "If I did not love me if" ) .endObject ()); 
client.update (updateRequest) .get (); 
or 
updateRequest updateRequest = new new updateRequest ( "twitter","tweet", "1")
        .doc (.. jsonBuilder () StartObject () Field, ( "Content", "If I did not love me if" ) .endObject ()); 
client.update (updateRequest) .get ();

 8 . Insert Update
 // update insert (id does not exist if the content is content IndexRequest insert 3, if there is content UpdateRequest content is updated) 
IndexRequest indexRequest = new new IndexRequest ( "Twitter", "Tweet", "3" ) 
        .source (jsonBuilder () .startObject () 
            .field ( "Content", "whenever I get lost in the dark" ) 
            .endObject ()); 
UpdateRequest updateRequest2 = new new UpdateRequest ( "twitter", "Tweet", "3" ) 
        .doc ( jsonBuilder ().startObject()
                .field("content", "please illuminate me forward" ) 
                .endObject ()) 
        .upsert (indexRequest); 
client.update (updateRequest2) .get (); 
client.close ();

 

Guess you like

Origin www.cnblogs.com/gavinYang/p/11199643.html