elasticsearch經常使用操做

3.3.1 Preparing a query 準備查詢請求

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.search.SearchHit;

SearchResponse response = client.prepareSearch("library")
.addFields("title", "_source")
.execute().actionGet();
for(SearchHit hit: response.getHits().getHits()) {
     System.out.println(hit.getId());
     if (hit.getFields().containsKey("title")) {
            System.out.println("field.title: "+ hit.getFields().get("title").getValue());
     }
     System.out.println("source.title: " + hit.getSource().get("title"));
}

 

3.3.2 Building queries 構造查詢

import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();node

searchSourceBuilder.query(QueryBuilders.termQuery("sid_s", text));web

Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex("webpage")sql

.build();json

try {緩存

SearchResult result = client.execute(search);app

}catch(Exception e){nosql

e.printStackTrace();elasticsearch

}fetch

 

3.3.3 Using the match all documents query 匹配全部文檔的查詢

queryBuilder = QueryBuilders.matchAllQuery()
.boost(11f).normsField("title");

 

 

3.3.4 The match query 匹配查詢

queryBuilder = QueryBuilders
.matchQuery("message", "a quick brown fox")
.operator(Operator.AND)
.zeroTermsQuery(ZeroTermsQuery.ALL);

 

3.3.5 Using the geo shape query 地理位置查詢

queryBuilder = QueryBuilders.geoShapeQuery("location",
ShapeBuilder.newRectangle()
.topLeft(13, 53)
.bottomRight(14, 52)
.build());

 

3.3.6 Paging query 分頁查詢

SearchResponse response = client.prepareSearch("library")
.setQuery(QueryBuilders.matchAllQuery())
.setFrom(10)   //跳過前10個文檔
.setSize(20)   //獲取20個文檔
.execute().actionGet();

response.getHits().totalHits()能夠統計當前匹配到的結果數優化

 

3.3.7 Sorting  排序

searchSourceBuilder.fetchSource(null, "content").sort("_score");
		searchSourceBuilder.sort("date", SortOrder.DESC);

SortBuilders.scriptSort(script, type) //使用腳原本實現排序

SortBuilders.geoDistanceSort(fieldName) //根據空間距離來進行排序

提到距離問題,附帶一篇博文:關於已知兩點經緯度求球面最短距離的公式推導

 

3.3.8 Filtering 過濾

 

QueryBuilder filterBuilder = QueryBuilders  
         .filteredQuery(  
              QueryBuilders.existsQuery("title").queryName("exist"),  
              QueryBuilders.termQuery("title", "elastic")  
          );  
      
SearchResponse response = client.prepareSearch("library")  
          .setPostFilter(filterBuilder)  
          .execute().actionGet();  

 

  

 

3.3.10 Highlighting 高亮

SearchResponse response = client.prepareSearch("wikipedia")
.addHighlightedField("title")
.setQuery(QueryBuilders.termQuery("title", "actress"))
.setHighlighterPreTags("<1>", "<2>")
.setHighlighterPostTags("</1>", "</2>")
.execute().actionGet();
for(SearchHit hit: response.getHits().getHits()) {
    HighlightField hField = hit.getHighlightFields().get("title");
    for (Text t : hField.fragments()) {
           System.out.println(t.string());
    }
}

 

3.3.11 Suggestions 查詢建議

SearchResponse response = client.prepareSearch("wikipedia")
.setQuery(QueryBuilders.matchAllQuery())
.addSuggestion(new TermSuggestionBuilder("first_suggestion")
.text("graphics designer")
.field("_all"))
.execute().actionGet();

for( Entry<? extends Option> entry : response.getSuggest().getSuggestion("first_suggestion").getEntries()) {
     System.out.println("Check for: " + entry.getText() + ". Options:");
     for( Option option : entry.getOptions()) {
          System.out.println("\t" + option.getText());
     }
}

 

 

3.3.12 Counting 統計

CountResponse response = client.prepareCount("library")
.setQuery(QueryBuilders.termQuery("title", "elastic"))
.execute().actionGet();

 

3.3.13 Scrolling 滾動

SearchResponse responseSearch = client.prepareSearch("library")
.setScroll("1m")
.setSearchType(SearchType.SCAN)
.execute().actionGet();
String scrollId = responseSearch.getScrollId();
SearchResponse response = client.prepareSearchScroll(scrollId).execute().actionGet();

 

3.3.14 Bulk 批量操做

BulkResponse response = client.prepareBulk()
.add(client.prepareIndex("library", "book", "5")
.setSource("{ \"title\" : \"Solr Cookbook\"}")
.request())
.add(client.prepareDelete("library", "book", "2").request()).execute().actionGet();

 

 

3.3.16 Multi GET  多GET

MultiGetResponse response = client.prepareMultiGet()
.add("library", "book", "1", "2")
.execute().actionGet();

 


3.3.16 Multi Search 多搜索

MultiSearchResponse response = client.prepareMultiSearch()
.add(client.prepareSearch("library", "book").request())
.add(client.prepareSearch("news").
.setPostFilter(QueryBuilders.termQuery("tags", "important")))
.execute().actionGet();

 

 

3.3.17 Building JSON queries and documents 構造JSON格式的查詢和文檔

IndexResponse response = client
.prepareIndex("library", "book", "2")
.setSource("{ \"title\": \"Mastering ElasticSearch\"}")
.execute().actionGet();

Map<String, Object> m = Maps.newHashMap();
m.put("1", "Introduction");
m.put("2", "Basics");
m.put("3", "And the rest");
XContentBuilder json = XContentFactory.jsonBuilder().prettyPrint()
.startObject()
.field("id").value("2123")
.field("lastCommentTime", new Date())
.nullField("published")
.field("chapters").map(m)
.field("title", "Mastering ElasticSearch")
.array("tags", "search", "ElasticSearch", "nosql")
.field("values")
.startArray()
.value(1)
.value(10)
.endArray()
.endObject();

 

 

3.4 The administration API

 

3.4.1.1 The cluster and indices health API 集羣和索引健康狀態

ClusterHealthResponse response = client.admin().cluster()
.prepareHealth("library")
.execute().actionGet();

 

 

3.4.1.2 The cluster state API 集羣狀態

ClusterStateResponse response = client.admin().cluster()
.prepareState()
.execute().actionGet();

 

 

3.4.1.3 The update settings API 設置更新

Map<String, Object> map = Maps.newHashMap();
map.put("indices.ttl.interval", "10m");
ClusterUpdateSettingsResponse response = client.admin().cluster()
.prepareUpdateSettings()
.setTransientSettings(map)
.execute().actionGet();

 

3.4.1.4 The reroute API 從新路由

ClusterRerouteResponse response = client.admin().cluster()
.prepareReroute()
.setDryRun(true)   //阻止分配命令的運行,僅容許下面兩個給定命令(move命令 + cacel命令)的執行
.add(new MoveAllocationCommand(new ShardId("library", 3), "G3czOt4HQbKZT1RhpPCULw","PvHtEMuRSJ6rLJ27AW3U6w"),
     new CancelAllocationCommand(new ShardId("library", 2), "G3czOt4HQbKZT1RhpPCULw",true))
.execute().actionGet();

 

3.4.1.5 The nodes information API 節點信息

NodesInfoResponse response = client.admin().cluster()
.prepareNodesInfo()
.setHttp(true)  //響應中包括http信息
.setPlugins(true)  //響應中包括插件信息
.execute().actionGet();

 

3.4.1.6 The node statistics API 節點統計

NodesStatsResponse response = client.admin().cluster()
.prepareNodesStats()
.all()
.execute().actionGet();

 

3.4.1.7 The nodes hot threads API 節點熱點線程

NodesHotThreadsResponse response = client.admin().cluster()
.prepareNodesHotThreads()
.execute().actionGet();

 

 

3.4.1.9 The search shards API 查詢分片

//輸出哪些節點將處理路由值爲12的查詢
ClusterSearchShardsResponse response = client.admin().cluster()
.prepareSearchShards()
.setIndices("library")
.setRouting("12")
.execute().actionGet();

 

3.4.2 The Indices administration API

3.4.2.1 The index existence API 索引存在

IndicesExistsResponse response = client.admin().indices()
.prepareExists("books", "library")
.execute().actionGet();

 

3.4.2.2 The Type existence API 類型存在

TypesExistsResponse response = client.admin().indices()
.prepareTypesExists("library")
.setTypes("book")
.execute().actionGet();

 

3.4.2.3 The indices stats API 索引統計

IndicesStatsResponse response = client.admin().indices()
.prepareStats("library")
.all()
.execute().actionGet();

 


3.4.2.4 Index status 索引狀態

IndicesStatsResponse response = client.admin().indices()
.prepareStatus("library")
.setRecovery(true)
.execute().actionGet();

 

 

3.4.2.5 Segments information API 索引段信息

IndicesSegmentResponse response = client.admin().indices()
.prepareSegments("library")
.execute().actionGet();

 

3.4.2.6 Creating an index API 建立索引

CreateIndexResponse response = client.admin().indices()
.prepareCreate("news")
.setSettings(Settings.settingsBuilder()
.put("number_of_shards", 1))  //分片數爲1
.addMapping("news", XContentFactory.jsonBuilder()
.startObject()
.startObject("news")
.startObject("properties")
.startObject("title")
.field("analyzer", "whitespace")
.field("type", "string")
.endObject()
.endObject()
.endObject()
.endObject())
.execute().actionGet();

 

3.4.2.7 Deleting an index 刪除索引

DeleteIndexResponse response = client.admin().indices()
.prepareDelete("news")
.execute().actionGet();

 

3.4.2.8 Closing an index 關閉索引

CloseIndexResponse response = client.admin().indices()
.prepareClose("library")
.execute().actionGet();

 

3.4.2.9 Opening an index 打開索引

OpenIndexResponse response = client.admin().indices()
.prepareOpen("library")
.execute().actionGet();

 

3.4.2.10 The Refresh API  刷新索引

RefreshResponse response = client.admin().indices()
.prepareRefresh("library")
.execute().actionGet();

 

3.4.2.11 The Flush API 清空緩衝區

FlushResponse response = client.admin().indices()
.prepareFlush("library")
.setFource(false)
.execute().actionGet();

 

3.4.2.12 The Optimize API 索引優化

OptimizeResponse response = client.admin().indices()
.prepareOptimize("library")
.setMaxNumSegments(2)  //最大索引段爲2
.setFlush(true)
.setOnlyExpungeDeletes(false)
.execute().actionGet();

 

3.4.2.13 The put mapping API 設置映射

PutMappingResponse response = client.admin().indices()
.preparePutMapping("news")
.setType("news")
.setSource(XContentFactory.jsonBuilder()
.startObject()
.startObject("news")
.startObject("properties")
.startObject("title")
.field("analyzer", "whitespace")
.field("type", "string")
.endObject()
.endObject()
.endObject()
.endObject())
.execute().actionGet();

 

3.4.2.16 The aliases API 別名

IndicesAliasesResponse response = client.admin().indices()
.prepareAliases()
.addAlias("news", "n")
.addAlias("library", "elastic_books", 
QueryBuilders.termQuery("title", "elasticsearch"))
.removeAlias("news", "current_news") //移除news索引的別名current_news
.execute().actionGet();

 

3.4.2.17 The get aliases API 獲取別名

GetAliasesResponse response = client.admin().indices()
.prepareGetAliases("elastic_books", "n")
.execute().actionGet();

 

3.4.2.18 The aliases exists API 別名存在

AliasesExistResponse response = client.admin().indices()
.prepareAliasesExist("elastic*", "unknown")  //以elastic開頭 || 爲unknown 的別名
.execute().actionGet();

 

3.4.2.19 The clear cache API 清空緩存

ClearIndicesCacheResponse response = client.admin().indices()
.prepareClearCache("library")
.setFieldDataCache(true)
.setFields("title")   //清空標題緩存
.setQueryCache(true)  //清空過濾器緩存
.execute().actionGet();


3.4.2.20 The update settings API  更新設置

UpdateSettingsResponse response = client.admin().indices()
.prepareUpdateSettings("library")
.setSettings(Settings.builder()
.put("index.number_of_replicas", 2))  //將副本數更新爲2
.execute().actionGet();

3.4.2.21 The analyze API 分析API

//主要是用來檢查在library索引中使用whitespace分詞器 + nGram過濾器處理「ElasticSerch Servers」短語的分析處理
AnalyzeResponse response = client.admin().indices()
.prepareAnalyze("library", "ElasticSearch Servers")
.setTokenizer("whitespace")
.setTokenFilters("nGram")
.execute().actionGet();

 

3.4.2.22 The put template API 設置模板

PutIndexTemplateResponse response = client.admin().indices()
.preparePutTemplate("my_template")  //my_template模板名稱
.setTemplate("product*")   //能夠被任何以product開頭的索引使用
.setSettings(Settings.builder()
.put("index.number_of_replicas", 2)  //2個副本
.put("index.number_of_shards", 1))   //一個分片
.addMapping("item", XContentFactory.jsonBuilder()
.startObject()
.startObject("item")
.startObject("properties")
.startObject("title")
.field("type", "string")
.endObject()
.endObject()
.endObject()
.endObject())
.execute().actionGet();

 

 

 

3.4.2.23 The delete template API 刪除模板

DeleteIndexTemplateResponse response = client.admin().indices()
.prepareDeleteTemplate("my_*") //刪除以my_開頭的模板
.execute().actionGet();

 

 

轉自: http://study121007.iteye.com/blog/2296556
相關文章
相關標籤/搜索