1. 程式人生 > 其它 >js字串操作方法集合

js字串操作方法集合

SpringBoot整合

1、建立工程

目錄結構

2、匯入依賴

注意依賴版本和安裝的版本一致

  1. <properties>
  2. <java.version>1.8</java.version>
  3. <!-- 統一版本 -->
  4. <elasticsearch.version>7.6.1</elasticsearch.version>
  5. </properties>

匯入elasticsearch

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  4. </dependency>

提前匯入fastjson、lombok

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>fastjson</artifactId>
  4. <version>1.2.70</version>
  5. </dependency>
  6. <!-- lombok需要安裝外掛 -->
  7. <dependency>
  8. <groupId>org.projectlombok</groupId>
  9. <artifactId>lombok</artifactId>
  10. <optional>true</optional>
  11. </dependency>

3、建立並編寫配置類

  1. @Configuration
  2. public class ElasticSearchConfig {
  3. // 註冊 rest高階客戶端
  4. @Bean
  5. public RestHighLevelClient restHighLevelClient(){
  6. RestHighLevelClient client = new RestHighLevelClient(
  7. RestClient.builder(
  8. new HttpHost("127.0.0.1",9200,"http")
  9. )
  10. );
  11. return client;
  12. }
  13. }

4、建立並編寫實體類

  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. public class User implements Serializable {
  5. private static final long serialVersionUID = -3843548915035470817L;
  6. private String name;
  7. private Integer age;
  8. }

5、測試

所有測試均在 SpringbootElasticsearchApplicationTests中編寫

注入 RestHighLevelClient

  1. @Autowired
  2. public RestHighLevelClient restHighLevelClient;

索引的操作

1、索引的建立
  1. // 測試索引的建立, Request PUT liuyou_index
  2. @Test
  3. public void testCreateIndex() throws IOException {
  4. CreateIndexRequest request = new CreateIndexRequest("liuyou_index");
  5. CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
  6. System.out.println(response.isAcknowledged());// 檢視是否建立成功
  7. System.out.println(response);// 檢視返回物件
  8. restHighLevelClient.close();
  9. }
2、索引的獲取,並判斷其是否存在
  1. // 測試獲取索引,並判斷其是否存在
  2. @Test
  3. public void testIndexIsExists() throws IOException {
  4. GetIndexRequest request = new GetIndexRequest("index");
  5. boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
  6. System.out.println(exists);// 索引是否存在
  7. restHighLevelClient.close();
  8. }
3、索引的刪除
  1. // 測試索引刪除
  2. @Test
  3. public void testDeleteIndex() throws IOException {
  4. DeleteIndexRequest request = new DeleteIndexRequest("liuyou_index");
  5. AcknowledgedResponse response = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
  6. System.out.println(response.isAcknowledged());// 是否刪除成功
  7. restHighLevelClient.close();
  8. }

文件的操作

1、文件的新增
  1. // 測試新增文件(先建立一個User實體類,新增fastjson依賴)
  2. @Test
  3. public void testAddDocument() throws IOException {
  4. // 建立一個User物件
  5. User liuyou = new User("liuyou", 18);
  6. // 建立請求
  7. IndexRequest request = new IndexRequest("liuyou_index");
  8. // 制定規則 PUT /liuyou_index/_doc/1
  9. request.id("1");// 設定文件ID
  10. request.timeout(TimeValue.timeValueMillis(1000));// request.timeout("1s")
  11. // 將我們的資料放入請求中
  12. request.source(JSON.toJSONString(liuyou), XContentType.JSON);
  13. // 客戶端傳送請求,獲取響應的結果
  14. IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
  15. System.out.println(response.status());// 獲取建立索引的狀態資訊 CREATED
  16. 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}]
  17. }
2、文件資訊的獲取
  1. // 測試獲得文件資訊
  2. @Test
  3. public void testGetDocument() throws IOException {
  4. GetRequest request = new GetRequest("liuyou_index","1");
  5. GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
  6. System.out.println(response.getSourceAsString());// 列印文件內容
  7. System.out.println(request);// 返回的全部內容和命令是一樣的
  8. restHighLevelClient.close();
  9. }
3、文件的獲取,並判斷其是否存在
  1. // 獲取文件,判斷是否存在 get /liuyou_index/_doc/1
  2. @Test
  3. public void testDocumentIsExists() throws IOException {
  4. GetRequest request = new GetRequest("liuyou_index", "1");
  5. // 不獲取返回的 _source的上下文了
  6. request.fetchSourceContext(new FetchSourceContext(false));
  7. request.storedFields("_none_");
  8. boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
  9. System.out.println(exists);
  10. }
4、文件的更新
  1. // 測試更新文件內容
  2. @Test
  3. public void testUpdateDocument() throws IOException {
  4. UpdateRequest request = new UpdateRequest("liuyou_index", "1");
  5. User user = new User("lmk",11);
  6. request.doc(JSON.toJSONString(user),XContentType.JSON);
  7. UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT);
  8. System.out.println(response.status()); // OK
  9. restHighLevelClient.close();
  10. }
5、文件的刪除
  1. // 測試刪除文件
  2. @Test
  3. public void testDeleteDocument() throws IOException {
  4. DeleteRequest request = new DeleteRequest("liuyou_index", "1");
  5. request.timeout("1s");
  6. DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
  7. System.out.println(response.status());// OK
  8. }
6、文件的查詢
  1. // 查詢
  2. // SearchRequest 搜尋請求
  3. // SearchSourceBuilder 條件構造
  4. // HighlightBuilder 高亮
  5. // TermQueryBuilder 精確查詢
  6. // MatchAllQueryBuilder
  7. // xxxQueryBuilder ...
  8. @Test
  9. public void testSearch() throws IOException {
  10. // 1.建立查詢請求物件
  11. SearchRequest searchRequest = new SearchRequest();
  12. // 2.構建搜尋條件
  13. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  14. // (1)查詢條件 使用QueryBuilders工具類建立
  15. // 精確查詢
  16. TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "liuyou");
  17. // // 匹配查詢
  18. // MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
  19. // (2)其他<可有可無>:(可以參考 SearchSourceBuilder 的欄位部分)
  20. // 設定高亮
  21. searchSourceBuilder.highlighter(new HighlightBuilder());
  22. // // 分頁
  23. // searchSourceBuilder.from();
  24. // searchSourceBuilder.size();
  25. searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
  26. // (3)條件投入
  27. searchSourceBuilder.query(termQueryBuilder);
  28. // 3.新增條件到請求
  29. searchRequest.source(searchSourceBuilder);
  30. // 4.客戶端查詢請求
  31. SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  32. // 5.檢視返回結果
  33. SearchHits hits = search.getHits();
  34. System.out.println(JSON.toJSONString(hits));
  35. System.out.println("=======================");
  36. for (SearchHit documentFields : hits.getHits()) {
  37. System.out.println(documentFields.getSourceAsMap());
  38. }
  39. }
前面的操作都無法批量新增資料
  1. // 上面的這些api無法批量增加資料(只會保留最後一個source)
  2. @Test
  3. public void test() throws IOException {
  4. IndexRequest request = new IndexRequest("bulk");// 沒有id會自動生成一個隨機ID
  5. request.source(JSON.toJSONString(new User("liu",1)),XContentType.JSON);
  6. request.source(JSON.toJSONString(new User("min",2)),XContentType.JSON);
  7. request.source(JSON.toJSONString(new User("kai",3)),XContentType.JSON);
  8. IndexResponse index = restHighLevelClient.index(request, RequestOptions.DEFAULT);
  9. System.out.println(index.status());// created
  10. }
7、批量新增資料
  1. // 特殊的,真的專案一般會 批量插入資料
  2. @Test
  3. public void testBulk() throws IOException {
  4. BulkRequest bulkRequest = new BulkRequest();
  5. bulkRequest.timeout("10s");
  6. ArrayList<User> users = new ArrayList<>();
  7. users.add(new User("liuyou-1",1));
  8. users.add(new User("liuyou-2",2));
  9. users.add(new User("liuyou-3",3));
  10. users.add(new User("liuyou-4",4));
  11. users.add(new User("liuyou-5",5));
  12. users.add(new User("liuyou-6",6));
  13. // 批量請求處理
  14. for (int i = 0; i < users.size(); i++) {
  15. bulkRequest.add(
  16. // 這裡是資料資訊
  17. new IndexRequest("bulk")
  18. .id(""+(i + 1)) // 沒有設定id 會自定生成一個隨機id
  19. .source(JSON.toJSONString(users.get(i)),XContentType.JSON)
  20. );
  21. }
  22. BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
  23. System.out.println(bulk.status());// ok
  24. }