添加依赖
< dependency>
< groupId> org.elasticsearch.client</ groupId>
< artifactId> elasticsearch-rest-high-level-client</ artifactId>
< version> 7.2.0</ version>
</ dependency>
配置类
import org. apache. http. HttpHost ;
import org. elasticsearch. client. RestClient ;
import org. elasticsearch. client. RestHighLevelClient ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. beans. factory. annotation. Value ;
import org. springframework. boot. context. properties. ConfigurationProperties ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
@Configuration
public class ElasticSearchClientConfig {
@Value ( "${es.hostname}" )
private String hostname;
@Value ( "${es.port}" )
private Integer port;
@Value ( "${es.scheme}" )
private String scheme;
@Bean
public RestHighLevelClient restHighLevelClient ( ) {
RestHighLevelClient client = new RestHighLevelClient ( RestClient . builder ( new HttpHost ( hostname, port, scheme) ) ) ;
return client;
}
}
测试类
import java. io. IOException ;
import java. util. ArrayList ;
import java. util. HashMap ;
import java. util. List ;
import java. util. concurrent. TimeUnit ;
import java. util. stream. Collectors ;
import com. baomidou. mybatisplus. extension. plugins. pagination. Page ;
import com. reptile. dao. AnnouncementDao ;
import com. reptile. dao. BidItemDao ;
import com. reptile. dto. AnnouncementAndItemDto ;
import com. reptile. dto. AnnouncementDto ;
import com. reptile. po. AnnouncementPo ;
import com. reptile. po. BidItemPo ;
import com. reptile. vo. AnnouncementReq ;
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. RestHighLevelClient ;
import org. elasticsearch. client. indices. CreateIndexRequest ;
import org. elasticsearch. client. indices. CreateIndexResponse ;
import org. elasticsearch. client. indices. GetIndexRequest ;
import org. elasticsearch. common. settings. Settings ;
import org. elasticsearch. common. unit. TimeValue ;
import org. elasticsearch. common. xcontent. XContentType ;
import org. elasticsearch. index. query. QueryBuilders ;
import org. elasticsearch. index. query. TermQueryBuilder ;
import org. elasticsearch. search. SearchHit ;
import org. elasticsearch. search. builder. SearchSourceBuilder ;
import org. elasticsearch. search. fetch. subphase. FetchSourceContext ;
import org. junit. jupiter. api. Test ;
import org. junit. runner. RunWith ;
import org. springframework. beans. BeanUtils ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. beans. factory. annotation. Qualifier ;
import org. springframework. boot. test. context. SpringBootTest ;
import com. alibaba. fastjson. JSON;
import org. springframework. test. context. junit4. SpringRunner ;
@RunWith ( SpringRunner . class )
@SpringBootTest
public class ElasticsearchJdApplicationTests {
@Autowired
private RestHighLevelClient client;
@Autowired
private AnnouncementDao announcementDao;
@Autowired
private BidItemDao bidItemDao;
@Test
void testCreateIndex ( ) throws IOException {
CreateIndexRequest request = new CreateIndexRequest ( "biding" ) ;
request. settings ( Settings . builder ( )
. put ( "index.number_of_shards" , 1 )
. put ( "index.number_of_replicas" , 0 )
) ;
request. source ( "{\n" +
" \"mappings\": {\n" +
" \"properties\": {\n" +
" \"addressId\": {\n" +
" \"type\": \"long\"\n" +
" },\n" +
" \"announcementName\": {\n" +
" \"type\": \"text\",\n" +
" \"fields\": {\n" +
" \"keyword\": {\n" +
" \"type\": \"keyword\"\n" +
" }\n" +
" },\n" +
" \"boost\": 10\n" +
" },\n" +
" \"bidItems\": {\n" +
" \"type\": \"nested\",\n" +
" \"properties\": {\n" +
" \"announcementNumber\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"bidContent\": {\n" +
" \"type\": \"text\"\n" +
" },\n" +
" \"startDate\": {\n" +
" \"type\": \"date\",\n" +
" \"format\": \"yyyy-MM-dd\"\n" +
" },\n" +
" \"endDate\": {\n" +
" \"type\": \"date\",\n" +
" \"format\": \"yyyy-MM-dd\"\n" +
" },\n" +
" \"bidItemNumber\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"id\": {\n" +
" \"type\": \"long\"\n" +
" },\n" +
" \"packetNumber\": {\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"itemName\": {\n" +
" \"type\": \"text\",\n" +
" \"fields\": {\n" +
" \"keyword\": {\n" +
" \"type\": \"keyword\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"performanceRequirements\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"projectConstructionUnit\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"qualificationRequirement\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"smallClassName\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"projectCategory\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"biddingScope\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"licensingRequirements\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"bidLimitPrice\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"bidBond\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"contactInformation\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"demandUnit\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"materialName\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"materialDesc\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"location\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"remark\": {\n" +
" \"type\": \"text\"\n" +
" },\n" +
" \"projectDetail\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"fileAcquireEndTime\": {\n" +
" \"type\": \"date\",\n" +
" \"format\": \"yyyy-MM-dd||yyyy-MM-dd HH:mm:ss\"\n" +
" },\n" +
" \"fileName\": {\n" +
" \"type\": \"text\"\n" +
" },\n" +
" \"flag\": {\n" +
" \"type\": \"byte\",\n" +
" \"index\": false\n" +
" },\n" +
" \"id\": {\n" +
" \"type\": \"long\"\n" +
" },\n" +
" \"insertTime\": {\n" +
" \"type\": \"date\",\n" +
" \"format\": \"yyyy-MM-dd||yyyy-MM-dd HH:mm:ss\"\n" +
" },\n" +
" \"itemNumber\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"labels\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"noticeUrl\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"openTime\": {\n" +
" \"type\": \"date\",\n" +
" \"format\": \"yyyy-MM-dd||yyyy-MM-dd HH:mm:ss\"\n" +
" },\n" +
" \"publishTime\": {\n" +
" \"type\": \"date\",\n" +
" \"format\": \"yyyy-MM-dd||yyyy-MM-dd HH:mm:ss\"\n" +
" },\n" +
" \"city\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"resultType\": {\n" +
" \"type\": \"byte\"\n" +
" },\n" +
" \"type\": {\n" +
" \"type\": \"keyword\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}" , XContentType . JSON) ;
CreateIndexResponse createIndexResponse = client. indices ( ) . create ( request, RequestOptions . DEFAULT) ;
System . out. println ( createIndexResponse) ;
}
@Test
void testExistIndex ( ) throws IOException {
GetIndexRequest request = new GetIndexRequest ( "biding" ) ;
boolean exists = client. indices ( ) . exists ( request, RequestOptions . DEFAULT) ;
System . out. println ( exists) ;
}
@Test
void testDeleteIndex ( ) throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest ( "biding" ) ;
AcknowledgedResponse delete = client. indices ( ) . delete ( request, RequestOptions . DEFAULT) ;
System . out. println ( delete. isAcknowledged ( ) ) ;
}
@Test
void testAddDocument ( ) throws IOException {
AnnouncementReq announcementReq = new AnnouncementReq ( ) ;
announcementReq. setPageNo ( 1 ) ;
announcementReq. setAddressId ( 1 ) ;
announcementReq. setPageSize ( 3 ) ;
Page < AnnouncementPo > announcementPoPage = announcementDao. pageAnnouncement ( announcementReq) ;
if ( announcementPoPage. getRecords ( ) == null || announcementPoPage. getRecords ( ) . size ( ) <= 0 ) return ;
List < AnnouncementAndItemDto > announcementAndItemDtos = announcementPoPage. getRecords ( ) . stream ( ) . map ( item -> {
AnnouncementAndItemDto announcementAndItemDto = new AnnouncementAndItemDto ( ) ;
BeanUtils . copyProperties ( item, announcementAndItemDto) ;
return announcementAndItemDto;
} ) . collect ( Collectors . toList ( ) ) ;
List < String > announcementNumbers = announcementAndItemDtos. stream ( ) . map ( AnnouncementAndItemDto :: getItemNumber ) . collect ( Collectors . toList ( ) ) ;
HashMap < String , List < BidItemPo > > announcementNumberBidItemsMap = new HashMap < > ( ) ;
bidItemDao. lambdaQuery ( ) . in ( BidItemPo :: getAnnouncementNumber , announcementNumbers) . list ( ) . forEach ( item -> {
List < BidItemPo > orDefault = announcementNumberBidItemsMap. getOrDefault ( item. getAnnouncementNumber ( ) , new ArrayList < > ( ) ) ;
orDefault. add ( item) ;
announcementNumberBidItemsMap. put ( item. getAnnouncementNumber ( ) , orDefault) ;
} ) ;
announcementAndItemDtos. forEach ( item -> {
item. setBidItems ( announcementNumberBidItemsMap. get ( item. getItemNumber ( ) ) ) ;
} ) ;
BulkRequest bulkRequest = new BulkRequest ( ) ;
bulkRequest. timeout ( "30s" ) ;
for ( int i = 0 ; i < announcementAndItemDtos. size ( ) ; i++ ) {
bulkRequest. add ( new IndexRequest ( "biding" ) . id ( "" + ( i + 1 ) )
. source ( JSON. toJSONString ( announcementAndItemDtos. get ( i) ) , XContentType . JSON) ) ;
}
BulkResponse bulkResponse = client. bulk ( bulkRequest, RequestOptions . DEFAULT) ;
System . out. println ( bulkResponse. hasFailures ( ) ) ;
System . out. println ( bulkResponse. status ( ) ) ;
System . out. println ( bulkResponse. toString ( ) ) ;
}
@Test
void testIsExists ( ) throws IOException {
GetRequest getRequest = new GetRequest ( "kuang_index" , "1" ) ;
getRequest. fetchSourceContext ( new FetchSourceContext ( false ) ) ;
getRequest. storedFields ( "_none_" ) ;
boolean exists = client. exists ( getRequest, RequestOptions . DEFAULT) ;
System . out. println ( exists) ;
}
@Test
void testGetDocument ( ) throws IOException {
GetRequest getRequest = new GetRequest ( "kuang_index" , "1" ) ;
GetResponse getResponse = client. get ( getRequest, RequestOptions . DEFAULT) ;
System . out. println ( getResponse. getSourceAsString ( ) ) ;
System . out. println ( getResponse) ;
}
@Test
void testUpdateRequest ( ) throws IOException {
UpdateRequest updateRequest = new UpdateRequest ( "kuang_index" , "1" ) ;
updateRequest. timeout ( "1s" ) ;
UpdateResponse updateResponse = client. update ( updateRequest, RequestOptions . DEFAULT) ;
System . out. println ( updateResponse. status ( ) ) ;
}
@Test
void testDeleteRequest ( ) throws IOException {
DeleteRequest request = new DeleteRequest ( "kuang_index" , "1" ) ;
request. timeout ( "1s" ) ;
DeleteResponse deleteResponse = client. delete ( request, RequestOptions . DEFAULT) ;
System . out. println ( deleteResponse. status ( ) ) ;
}
@Test
void testBulkRequest ( ) throws IOException {
BulkRequest bulkRequest = new BulkRequest ( ) ;
bulkRequest. timeout ( "10s" ) ;
BulkResponse bulkResponse = client. bulk ( bulkRequest, RequestOptions . DEFAULT) ;
System . out. println ( bulkResponse. hasFailures ( ) ) ;
}
@Test
void testSearch ( ) throws IOException {
SearchRequest searchRequest = new SearchRequest ( "kuang_index" ) ;
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder ( ) ;
sourceBuilder. highlighter ( ) ;
TermQueryBuilder termQueryBuilder = QueryBuilders . termQuery ( "name" , "qinjiang1" ) ;
QueryBuilders . matchAllQuery ( ) ;
sourceBuilder. query ( termQueryBuilder) ;
sourceBuilder. timeout ( new TimeValue ( 60 , TimeUnit . SECONDS) ) ;
searchRequest. source ( sourceBuilder) ;
SearchResponse searchResponse = client. search ( searchRequest, RequestOptions . DEFAULT) ;
System . out. println ( JSON. toJSONString ( searchResponse. getHits ( ) ) ) ;
System . out. println ( "=================================" ) ;
for ( SearchHit documentFields : searchResponse. getHits ( ) . getHits ( ) ) {
System . out. println ( documentFields. getSourceAsMap ( ) ) ;
}
}
}