索引
package com. chun. estest. demo ; import com. fasterxml. jackson. databind. ObjectMapper ;
import org. apache. http. HttpHost ;
import org. elasticsearch. action. admin. indices. delete. DeleteIndexRequest ;
import org. elasticsearch. action. delete. DeleteRequest ;
import org. elasticsearch. action. delete. DeleteResponse ;
import org. elasticsearch. action. get. GetRequest ;
import org. elasticsearch. action. get. GetResponse ;
import org. elasticsearch. action. index. IndexRequest ;
import org. elasticsearch. action. index. IndexResponse ;
import org. elasticsearch. action. support. master. AcknowledgedResponse ;
import org. elasticsearch. action. update. UpdateRequest ;
import org. elasticsearch. action. update. UpdateResponse ;
import org. elasticsearch. client. RequestOptions ;
import org. elasticsearch. client. RestClient ;
import org. elasticsearch. client. RestHighLevelClient ;
import org. elasticsearch. client. indices. CreateIndexRequest ;
import org. elasticsearch. client. indices. CreateIndexResponse ;
import org. elasticsearch. client. indices. GetIndexRequest ;
import org. elasticsearch. client. indices. GetIndexResponse ;
import org. elasticsearch. common. xcontent. XContent ;
import org. elasticsearch. common. xcontent. XContentFactory ;
import org. elasticsearch. common. xcontent. XContentType ;
import org. junit. After ;
import org. junit. Before ;
import org. junit. Test ; import java. io. IOException ;
public class EsTest_Client { RestHighLevelClient esClient = null ; @Before public void after ( ) { esClient = new RestHighLevelClient ( RestClient . builder ( new HttpHost ( "localhost" , 9200 , "http" ) ) ) ; } @Test public void index_create ( ) throws IOException { CreateIndexRequest request = new CreateIndexRequest ( "user" ) ; CreateIndexResponse response = esClient. indices ( ) . create ( request, RequestOptions . DEFAULT) ; boolean acknowledged = response. isAcknowledged ( ) ; System . out. println ( "索引操作:" + acknowledged) ; esClient. close ( ) ; } @Test public void index_search ( ) throws IOException { GetIndexRequest user = new GetIndexRequest ( "user" ) ; GetIndexResponse response = esClient. indices ( ) . get ( user, RequestOptions . DEFAULT) ; System . out. println ( response. getAliases ( ) ) ; System . out. println ( response. getMappings ( ) ) ; System . out. println ( "" + response. getSettings ( ) ) ; } @Test public void index_delete ( ) throws IOException { DeleteIndexRequest request = new DeleteIndexRequest ( "user" ) ; AcknowledgedResponse response = esClient. indices ( ) . delete ( request, RequestOptions . DEFAULT) ; System . out. println ( "是否删除成功:" + response. isAcknowledged ( ) ) ; } @Test public void doc_create ( ) throws IOException { IndexRequest request = new IndexRequest ( ) ; request. index ( "user" ) . id ( "1001" ) ; User user = new User ( "张三" , "男" , 30 ) ; ObjectMapper mapper = new ObjectMapper ( ) ; String userJson = mapper. writeValueAsString ( user) ; request. source ( userJson, XContentType . JSON) ; IndexResponse response = esClient. index ( request, RequestOptions . DEFAULT) ; System . out. println ( response. getResult ( ) ) ; } @Test public void doc_update ( ) throws IOException { UpdateRequest request = new UpdateRequest ( ) ; request. index ( "user" ) . id ( "1001" ) ; request. doc ( XContentType . JSON, "sex" , "女" ) ;
UpdateResponse response = esClient. update ( request, RequestOptions . DEFAULT) ; System . out. println ( response. getResult ( ) ) ; } @Test public void doc_search ( ) throws IOException { GetRequest request = new GetRequest ( ) ; request. index ( "user" ) . id ( "1001" ) ;
GetResponse response = esClient. get ( request, RequestOptions . DEFAULT) ; System . out. println ( response. getSourceAsString ( ) ) ; } @Test public void doc_delete ( ) throws IOException { DeleteRequest request = new DeleteRequest ( ) ; request. index ( "user" ) . id ( "1001" ) ;
DeleteResponse response = esClient. delete ( request, RequestOptions . DEFAULT) ; System . out. println ( response. getResult ( ) ) ; } @After public void After ( ) throws IOException { esClient. close ( ) ; } }
数据
package com. chun. estest. demo ; import com. fasterxml. jackson. databind. ObjectMapper ;
import org. apache. http. HttpHost ;
import org. elasticsearch. action. admin. indices. delete. DeleteIndexRequest ;
import org. elasticsearch. action. bulk. BulkRequest ;
import org. elasticsearch. action. bulk. BulkResponse ;
import org. elasticsearch. action. delete. DeleteRequest ;
import org. elasticsearch. action. delete. DeleteResponse ;
import org. elasticsearch. action. get. GetRequest ;
import org. elasticsearch. action. get. GetResponse ;
import org. elasticsearch. action. index. IndexRequest ;
import org. elasticsearch. action. index. IndexResponse ;
import org. elasticsearch. action. search. SearchRequest ;
import org. elasticsearch. action. search. SearchResponse ;
import org. elasticsearch. action. support. master. AcknowledgedResponse ;
import org. elasticsearch. action. update. UpdateRequest ;
import org. elasticsearch. action. update. UpdateResponse ;
import org. elasticsearch. client. RequestOptions ;
import org. elasticsearch. client. RestClient ;
import org. elasticsearch. client. RestHighLevelClient ;
import org. elasticsearch. client. indices. CreateIndexRequest ;
import org. elasticsearch. client. indices. CreateIndexResponse ;
import org. elasticsearch. client. indices. GetIndexRequest ;
import org. elasticsearch. client. indices. GetIndexResponse ;
import org. elasticsearch. common. unit. Fuzziness ;
import org. elasticsearch. common. xcontent. XContentType ;
import org. elasticsearch. index. query. BoolQueryBuilder ;
import org. elasticsearch. index. query. FuzzyQueryBuilder ;
import org. elasticsearch. index. query. QueryBuilder ;
import org. elasticsearch. index. query. QueryBuilders ;
import org. elasticsearch. index. query. RangeQueryBuilder ;
import org. elasticsearch. search. SearchHit ;
import org. elasticsearch. search. SearchHits ;
import org. elasticsearch. search. builder. SearchSourceBuilder ;
import org. elasticsearch. search. fetch. subphase. highlight. HighlightBuilder ;
import org. elasticsearch. search. sort. SortOrder ;
import org. junit. After ;
import org. junit. Before ;
import org. junit. Test ; import java. io. IOException ;
import java. util. Arrays ;
public class EsTest_Client_Batch { RestHighLevelClient esClient = null ; @Before public void after ( ) { esClient = new RestHighLevelClient ( RestClient . builder ( new HttpHost ( "localhost" , 9200 , "http" ) ) ) ; } @Test public void doc_Insert_Batch ( ) throws IOException { BulkRequest request = new BulkRequest ( ) ; request. add ( new IndexRequest ( ) . index ( "user" ) . id ( "1001" ) . source ( XContentType . JSON, "name" , "zhangsan" , "age" , 30 ) ) ; request. add ( new IndexRequest ( ) . index ( "user" ) . id ( "1002" ) . source ( XContentType . JSON, "name" , "lisi" , "age" , 40 ) ) ; request. add ( new IndexRequest ( ) . index ( "user" ) . id ( "1003" ) . source ( XContentType . JSON, "name" , "wangwu" , "age" , 20 ) ) ; BulkResponse response = esClient. bulk ( request, RequestOptions . DEFAULT) ; System . out. println ( response. getTook ( ) ) ; System . out. println ( Arrays . toString ( response. getItems ( ) ) ) ; } @Test public void doc_Delete_Batch ( ) throws IOException { BulkRequest request = new BulkRequest ( ) ; request. add ( new DeleteRequest ( ) . index ( "user" ) . id ( "1001" ) ) ; request. add ( new DeleteRequest ( ) . index ( "user" ) . id ( "1002" ) ) ; request. add ( new DeleteRequest ( ) . index ( "user" ) . id ( "1003" ) ) ; BulkResponse response = esClient. bulk ( request, RequestOptions . DEFAULT) ; System . out. println ( response. getTook ( ) ) ; System . out. println ( Arrays . toString ( response. getItems ( ) ) ) ; } @Test public void doc_Search_all_Batch ( ) throws IOException { SearchRequest request = new SearchRequest ( ) ; request. indices ( "user" ) ; request. source ( new SearchSourceBuilder ( ) . query ( QueryBuilders . matchAllQuery ( ) ) ) ; SearchResponse response = esClient. search ( request, RequestOptions . DEFAULT) ; SearchHits hits = response. getHits ( ) ; System . out. println ( hits. getTotalHits ( ) ) ; System . out. println ( response. getTook ( ) ) ; for ( SearchHit hit : hits) { System . out. println ( hit. getSourceAsString ( ) ) ; } } @Test public void doc_Search_Where_Batch ( ) throws IOException { SearchRequest request = new SearchRequest ( ) ; request. indices ( "user" ) ; request. source ( new SearchSourceBuilder ( ) . query ( QueryBuilders . termQuery ( "name" , "zhangsan" ) ) ) ; SearchResponse response = esClient. search ( request, RequestOptions . DEFAULT) ; SearchHits hits = response. getHits ( ) ; System . out. println ( hits. getTotalHits ( ) ) ; System . out. println ( response. getTook ( ) ) ; for ( SearchHit hit : hits) { System . out. println ( hit. getSourceAsString ( ) ) ; } } @Test public void doc_Search_Page_Batch ( ) throws IOException { SearchRequest request = new SearchRequest ( ) ; request. indices ( "user" ) ; SearchSourceBuilder builder = new SearchSourceBuilder ( ) . query ( QueryBuilders . matchAllQuery ( ) ) ; builder. from ( 2 ) . size ( 2 ) ; request. source ( builder) ; SearchResponse response = esClient. search ( request, RequestOptions . DEFAULT) ; SearchHits hits = response. getHits ( ) ; System . out. println ( hits. getTotalHits ( ) ) ; System . out. println ( response. getTook ( ) ) ; for ( SearchHit hit : hits) { System . out. println ( hit. getSourceAsString ( ) ) ; } } @Test public void doc_Search_OrderBy_Batch ( ) throws IOException { SearchRequest request = new SearchRequest ( ) ; request. indices ( "user" ) ; SearchSourceBuilder builder = new SearchSourceBuilder ( ) . query ( QueryBuilders . matchAllQuery ( ) ) ; builder. sort ( "age" , SortOrder . ASC) ;
request. source ( builder) ; SearchResponse response = esClient. search ( request, RequestOptions . DEFAULT) ; SearchHits hits = response. getHits ( ) ; System . out. println ( hits. getTotalHits ( ) ) ; System . out. println ( response. getTook ( ) ) ; for ( SearchHit hit : hits) { System . out. println ( hit. getSourceAsString ( ) ) ; } } @Test public void doc_Search_Filter_Batch ( ) throws IOException { SearchRequest request = new SearchRequest ( ) ; request. indices ( "user" ) ; SearchSourceBuilder builder = new SearchSourceBuilder ( ) . query ( QueryBuilders . matchAllQuery ( ) ) ; String [ ] excludes = { "age" } ; String [ ] includes = { } ; builder. fetchSource ( includes, excludes) ;
request. source ( builder) ; SearchResponse response = esClient. search ( request, RequestOptions . DEFAULT) ; SearchHits hits = response. getHits ( ) ; System . out. println ( hits. getTotalHits ( ) ) ; System . out. println ( response. getTook ( ) ) ; for ( SearchHit hit : hits) { System . out. println ( hit. getSourceAsString ( ) ) ; } } @Test public void doc_Search_Bool_Batch ( ) throws IOException { SearchRequest request = new SearchRequest ( ) ; request. indices ( "user" ) ; SearchSourceBuilder builder = new SearchSourceBuilder ( ) ; BoolQueryBuilder boolQueryBuilder = QueryBuilders . boolQuery ( ) ;
builder. query ( boolQueryBuilder) ; request. source ( builder) ; SearchResponse response = esClient. search ( request, RequestOptions . DEFAULT) ; SearchHits hits = response. getHits ( ) ; System . out. println ( hits. getTotalHits ( ) ) ; System . out. println ( response. getTook ( ) ) ; for ( SearchHit hit : hits) { System . out. println ( hit. getSourceAsString ( ) ) ; } } @Test public void doc_Search_Range_Batch ( ) throws IOException { SearchRequest request = new SearchRequest ( ) ; request. indices ( "user" ) ; SearchSourceBuilder builder = new SearchSourceBuilder ( ) ; RangeQueryBuilder rangeQueryBuilder = QueryBuilders . rangeQuery ( "age" ) ; rangeQueryBuilder. gte ( 30 ) ;
rangeQueryBuilder. lt ( 40 ) ; builder. query ( rangeQueryBuilder) ; request. source ( builder) ; SearchResponse response = esClient. search ( request, RequestOptions . DEFAULT) ; SearchHits hits = response. getHits ( ) ; System . out. println ( hits. getTotalHits ( ) ) ; System . out. println ( response. getTook ( ) ) ; for ( SearchHit hit : hits) { System . out. println ( hit. getSourceAsString ( ) ) ; } } @Test public void doc_Search_Fuzzy_Batch ( ) throws IOException { SearchRequest request = new SearchRequest ( ) ; request. indices ( "user" ) ; SearchSourceBuilder builder = new SearchSourceBuilder ( ) ; FuzzyQueryBuilder fuzziness = QueryBuilders . fuzzyQuery ( "name" , "li1si" ) . fuzziness ( Fuzziness . ONE) ; builder. query ( fuzziness) ; request. source ( builder) ; SearchResponse response = esClient. search ( request, RequestOptions . DEFAULT) ; SearchHits hits = response. getHits ( ) ; System . out. println ( hits. getTotalHits ( ) ) ; System . out. println ( response. getTook ( ) ) ; for ( SearchHit hit : hits) { System . out. println ( hit. getSourceAsString ( ) ) ; } } @Test public void doc_Search_Highlight_Batch ( ) throws IOException { SearchRequest request = new SearchRequest ( ) ; request. indices ( "user" ) ; SearchSourceBuilder builder = new SearchSourceBuilder ( ) ; FuzzyQueryBuilder fuzziness = QueryBuilders . fuzzyQuery ( "name" , "li1si" ) . fuzziness ( Fuzziness . ONE) ; HighlightBuilder highlightBuilder = new HighlightBuilder ( ) ; highlightBuilder. preTags ( "<font color='red'>" ) ; highlightBuilder. postTags ( "</font>" ) ; highlightBuilder. field ( "name" ) ; builder. highlighter ( highlightBuilder) ; builder. query ( fuzziness) ; request. source ( builder) ; SearchResponse response = esClient. search ( request, RequestOptions . DEFAULT) ; SearchHits hits = response. getHits ( ) ; System . out. println ( hits. getTotalHits ( ) ) ; System . out. println ( response. getTook ( ) ) ; for ( SearchHit hit : hits) { System . out. println ( hit. getSourceAsString ( ) ) ; } } @Test public void doc_Search_Group_Batch ( ) throws IOException { SearchRequest request = new SearchRequest ( ) ; request. indices ( "user" ) ; SearchSourceBuilder builder = new SearchSourceBuilder ( ) ; TermsAggregationBuilder aggregationBuilder = AggregationBuilders . terms ( "ageGroup" ) . field ( "age" ) ; builder. aggregation ( aggregationBuilder) ; request. source ( builder) ; SearchResponse response = esClient. search ( request, RequestOptions . DEFAULT) ; SearchHits hits = response. getHits ( ) ; System . out. println ( hits. getTotalHits ( ) ) ; System . out. println ( response. getTook ( ) ) ; for ( SearchHit hit : hits) { System . out. println ( hit. getSourceAsString ( ) ) ; } } @After public void After ( ) throws IOException { esClient. close ( ) ; } }