日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

分布式搜索 Elasticsearch —— 删除索引

發布時間:2023/12/10 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 分布式搜索 Elasticsearch —— 删除索引 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么80%的碼農都做不了架構師?>>> ??

刪除索引的方式很多,這里列舉三種。

  • 指定 index 、type、id 執行刪除
  • package com.gsoft.gsearch.util;import org.elasticsearch.action.get.GetResponse; import org.junit.Test;import com.gsoft.gsearch.BaseTest; import com.gsoft.gsearch.entity.Person;public class DeleteTest extends BaseTest {@Testpublic void delete() {try {String id = "1234567890";Person p = new Person();p.setId(id);p.setAge(20);p.setIsStudent(false);p.setSex("男");p.setName("張三的車");String source = ElasticSearchUtil.BeanToJson(p);// 創建索引client.prepareIndex(index, type, id).setSource(source).execute();System.out.println("休息6秒鐘,以便創建索引");Thread.sleep(6000);// GetSystem.out.println("================================第一次Get");showById(id);client.prepareDelete(index, type, id).execute();System.out.println("休息6秒鐘,以便刪除索引");Thread.sleep(6000);System.out.println("================================第二次Get");showById(id);} catch (Exception e) {e.printStackTrace();} finally {if (null != client) {client.close();}if (null != node) {node.close();}}}/*** 根據ID查詢,使用Get* @param id* @throws Exception*/private void showById(String id) throws Exception {GetResponse response = client.prepareGet(index, type, id).execute().actionGet();String json = response.getSourceAsString();if (null != json) {Person newPerson = mapper.readValue(json, Person.class);System.out.println("id\t\t" + newPerson.getId());System.out.println("name\t\t" + newPerson.getName());System.out.println("sex\t\t" + newPerson.getSex());System.out.println("age\t\t" + newPerson.getAge());System.out.println("isStudent\t\t" + newPerson.getIsStudent());} else {log.info("未查詢到任何結果!");}} }

    ?

  • 使用 Bulk 執行批量操作
  • package com.gsoft.gsearch.util;import org.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.junit.Test;import com.gsoft.gsearch.BaseTest; import com.gsoft.gsearch.entity.Person;public class BulkDeleteTest extends BaseTest {@Testpublic void delete() {try {String id = "1234567890";BulkRequestBuilder builder1 = client.prepareBulk();for (int i = 0; i < 5; i++) {String myId = id + "_" + i;Person p = new Person();p.setId(myId);p.setAge(20);p.setIsStudent(false);p.setSex("男");p.setName("張三的車");String source = ElasticSearchUtil.BeanToJson(p);// 創建索引builder1.add(client.prepareIndex(index, type, myId).setSource(source).request());}builder1.execute();System.out.println("休息6秒鐘,以便創建索引");Thread.sleep(6000);// GetSystem.out.println("================================第一次查詢");query();BulkRequestBuilder builder = client.prepareBulk();for (int i = 0; i < 5; i++) {String myId = id + "_" + i;builder.add(client.prepareDelete(index, type, myId).request());}builder.execute();System.out.println("休息6秒鐘,以便刪除索引");Thread.sleep(6000);System.out.println("================================第二次查詢");query();} catch (Exception e) {e.printStackTrace();} finally {if (null != client) {client.close();}if (null != node) {node.close();}}}private void query() throws Exception {// 檢索QueryBuilder qb = QueryBuilders.matchPhraseQuery("name", "張三的車");SearchResponse searchResponse = client.prepareSearch(index).setTypes(type).setQuery(qb).setFrom(0).setSize(12).execute().actionGet();SearchHits hits = searchResponse.getHits();if (null == hits || hits.totalHits() == 0) {log.error("使用\"張三的車\"沒有查詢到任何結果!");} else {for (SearchHit hit : hits) {String json = hit.getSourceAsString();Person newPerson = mapper.readValue(json, Person.class);System.out.println("id\t\t" + newPerson.getId());System.out.println("name\t\t" + newPerson.getName());System.out.println("sex\t\t" + newPerson.getSex());System.out.println("age\t\t" + newPerson.getAge());System.out.println("isStudent\t\t" + newPerson.getIsStudent());System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++");}}} }

    ?

  • 根據條件刪除匹配的索引
  • QueryBuild qb = QueryBuilders.matchAllQuery(); client.prepareDeleteQuery(indeices).setQuery(qb).execute();

    ?

  • ?

    第一種 和 第二種 需要知道 索引的ID后方可執行刪除,局限性較大,第三種根據條件刪除匹配索引,也比比較簡單。

    ?

    ?

    ?

    ?

    轉載于:https://my.oschina.net/exit/blog/806993

    總結

    以上是生活随笔為你收集整理的分布式搜索 Elasticsearch —— 删除索引的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。