js字串操作方法集合
阿新 • • 發佈:2022-03-15
SpringBoot整合
1、建立工程
略
目錄結構
2、匯入依賴
注意依賴版本和安裝的版本一致
<properties>
<java.version>1.8</java.version>
<!-- 統一版本 -->
<elasticsearch.version>7.6.1</elasticsearch.version>
</properties>
匯入elasticsearch
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
提前匯入fastjson、lombok
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
<!-- lombok需要安裝外掛 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
3、建立並編寫配置類
@Configuration
public class ElasticSearchConfig {
// 註冊 rest高階客戶端
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1",9200,"http")
)
);
return client;
}
}
4、建立並編寫實體類
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
private static final long serialVersionUID = -3843548915035470817L;
private String name;
private Integer age;
}
5、測試
所有測試均在
SpringbootElasticsearchApplicationTests
中編寫
注入 RestHighLevelClient
@Autowired
public RestHighLevelClient restHighLevelClient;
索引的操作
1、索引的建立
// 測試索引的建立, Request PUT liuyou_index
@Test
public void testCreateIndex() throws IOException {
CreateIndexRequest request = new CreateIndexRequest("liuyou_index");
CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());// 檢視是否建立成功
System.out.println(response);// 檢視返回物件
restHighLevelClient.close();
}
2、索引的獲取,並判斷其是否存在
// 測試獲取索引,並判斷其是否存在
@Test
public void testIndexIsExists() throws IOException {
GetIndexRequest request = new GetIndexRequest("index");
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);// 索引是否存在
restHighLevelClient.close();
}
3、索引的刪除
// 測試索引刪除
@Test
public void testDeleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("liuyou_index");
AcknowledgedResponse response = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());// 是否刪除成功
restHighLevelClient.close();
}
文件的操作
1、文件的新增
// 測試新增文件(先建立一個User實體類,新增fastjson依賴)
@Test
public void testAddDocument() throws IOException {
// 建立一個User物件
User liuyou = new User("liuyou", 18);
// 建立請求
IndexRequest request = new IndexRequest("liuyou_index");
// 制定規則 PUT /liuyou_index/_doc/1
request.id("1");// 設定文件ID
request.timeout(TimeValue.timeValueMillis(1000));// request.timeout("1s")
// 將我們的資料放入請求中
request.source(JSON.toJSONString(liuyou), XContentType.JSON);
// 客戶端傳送請求,獲取響應的結果
IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println(response.status());// 獲取建立索引的狀態資訊 CREATED
System.out.println(response);// 檢視返回內容 IndexResponse[index=liuyou_index,type=_doc,id=1,version=1,result=created,seqNo=0,primaryTerm=1,shards={"total":2,"successful":1,"failed":0}]
}
2、文件資訊的獲取
// 測試獲得文件資訊
@Test
public void testGetDocument() throws IOException {
GetRequest request = new GetRequest("liuyou_index","1");
GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());// 列印文件內容
System.out.println(request);// 返回的全部內容和命令是一樣的
restHighLevelClient.close();
}
3、文件的獲取,並判斷其是否存在
// 獲取文件,判斷是否存在 get /liuyou_index/_doc/1
@Test
public void testDocumentIsExists() throws IOException {
GetRequest request = new GetRequest("liuyou_index", "1");
// 不獲取返回的 _source的上下文了
request.fetchSourceContext(new FetchSourceContext(false));
request.storedFields("_none_");
boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
4、文件的更新
// 測試更新文件內容
@Test
public void testUpdateDocument() throws IOException {
UpdateRequest request = new UpdateRequest("liuyou_index", "1");
User user = new User("lmk",11);
request.doc(JSON.toJSONString(user),XContentType.JSON);
UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT);
System.out.println(response.status()); // OK
restHighLevelClient.close();
}
5、文件的刪除
// 測試刪除文件
@Test
public void testDeleteDocument() throws IOException {
DeleteRequest request = new DeleteRequest("liuyou_index", "1");
request.timeout("1s");
DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
System.out.println(response.status());// OK
}
6、文件的查詢
// 查詢
// SearchRequest 搜尋請求
// SearchSourceBuilder 條件構造
// HighlightBuilder 高亮
// TermQueryBuilder 精確查詢
// MatchAllQueryBuilder
// xxxQueryBuilder ...
@Test
public void testSearch() throws IOException {
// 1.建立查詢請求物件
SearchRequest searchRequest = new SearchRequest();
// 2.構建搜尋條件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// (1)查詢條件 使用QueryBuilders工具類建立
// 精確查詢
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "liuyou");
// // 匹配查詢
// MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
// (2)其他<可有可無>:(可以參考 SearchSourceBuilder 的欄位部分)
// 設定高亮
searchSourceBuilder.highlighter(new HighlightBuilder());
// // 分頁
// searchSourceBuilder.from();
// searchSourceBuilder.size();
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
// (3)條件投入
searchSourceBuilder.query(termQueryBuilder);
// 3.新增條件到請求
searchRequest.source(searchSourceBuilder);
// 4.客戶端查詢請求
SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
// 5.檢視返回結果
SearchHits hits = search.getHits();
System.out.println(JSON.toJSONString(hits));
System.out.println("=======================");
for (SearchHit documentFields : hits.getHits()) {
System.out.println(documentFields.getSourceAsMap());
}
}
前面的操作都無法批量新增資料
// 上面的這些api無法批量增加資料(只會保留最後一個source)
@Test
public void test() throws IOException {
IndexRequest request = new IndexRequest("bulk");// 沒有id會自動生成一個隨機ID
request.source(JSON.toJSONString(new User("liu",1)),XContentType.JSON);
request.source(JSON.toJSONString(new User("min",2)),XContentType.JSON);
request.source(JSON.toJSONString(new User("kai",3)),XContentType.JSON);
IndexResponse index = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println(index.status());// created
}
7、批量新增資料
// 特殊的,真的專案一般會 批量插入資料
@Test
public void testBulk() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("10s");
ArrayList<User> users = new ArrayList<>();
users.add(new User("liuyou-1",1));
users.add(new User("liuyou-2",2));
users.add(new User("liuyou-3",3));
users.add(new User("liuyou-4",4));
users.add(new User("liuyou-5",5));
users.add(new User("liuyou-6",6));
// 批量請求處理
for (int i = 0; i < users.size(); i++) {
bulkRequest.add(
// 這裡是資料資訊
new IndexRequest("bulk")
.id(""+(i + 1)) // 沒有設定id 會自定生成一個隨機id
.source(JSON.toJSONString(users.get(i)),XContentType.JSON)
);
}
BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulk.status());// ok
}