摘要:使用說明簡介是一個高可用開源全文檢索和分析組件。提供存儲服務(wù),搜索服務(wù),大數(shù)據(jù)準實時分析等。一般用于提供一些提供復(fù)雜搜索的應(yīng)用。為什么選用是一個準實時的搜索工具,在一般情況下延時少于一秒。
elasticsearch 使用說明.md 簡介
ElasticSearch 是一個高可用開源全文檢索和分析組件。提供存儲服務(wù),搜索服務(wù),大數(shù)據(jù)準實時分析等。一般用于提供一些提供復(fù)雜搜索的應(yīng)用。
WHY(為什么選用)ElasticSearch 是一個準實時的搜索工具,在一般情況下延時少于一秒。
支持物理上的水平擴展,并擁有一套分布式協(xié)調(diào)的管理功能
操作比較簡單
restful 風(fēng)格的API
Elasticsearch的使用場景深入詳解
REQUIREMENT (需求)一些復(fù)雜數(shù)據(jù)的查詢導(dǎo)出,使用傳統(tǒng)的數(shù)據(jù)庫(關(guān)系型數(shù)據(jù)庫)查詢以及配合java集合操作,很難解決的問題或者效率并不怎么好的問題,可以考慮使用
weakness(缺點)維護工作量大
HOWTO (如何去做) 安裝 & 啟動下載地址
解壓后進入到bin目錄下,eg:
運行(binelasticsearch.bat)eg:
訪問:http://127.0.0.1:9200/可以看到 eg:
{ "name": "bJs1ahK", "cluster_name": "elasticsearch", "cluster_uuid": "w0fjGOMARzuxt5K5bia07g", "version": { "number": "5.6.3", "build_hash": "1a2f265", "build_date": "2017-10-06T20:33:39.012Z", "build_snapshot": false, "lucene_version": "6.6.1" }, "tagline": "You Know, for Search" }
配置參考
程序中使用API使用文檔
創(chuàng)建client
public class ElasticSearchClient { private volatile static TransportClient client; private ElasticSearchClient (){} public static TransportClient getInstanceClient() { if (client == null) { synchronized (TransportClient.class) { if (client == null) { /** * 1. client.transport.ignore_cluster_name 時候校驗服務(wù)名字 true 校驗 false 不校驗 * 2. client.transport.ping_timeout 鏈接聯(lián)通時間 Defaults 5s * 3. client.transport.nodes_sampler_interval 節(jié)點調(diào)用時間 Defaults 5s */ Settings settings = Settings.builder() .put("cluster.name", "elasticsearch").build(); try { client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9200)); } catch (UnknownHostException e) { e.printStackTrace(); } } } } return client; } }
創(chuàng)建字段映射
public class ElasticsearchIndex { public String jsonIndex() { String json = "{" + ""userId":"1001"," + ""userName":"test"," + ""userSex":true" + ""userAge":"28"" + "}"; return json; } public Map mapIndex() { Mapjson = new HashMap (); json.put("userId", "1001"); json.put("userName", "test"); json.put("userSex", true); json.put("userAge", "28"); return json; } public byte[] objIndex() { ObjectMapper mapper = new ObjectMapper(); // create once, reuse User user = new User(); user.setUserId("1001"); user.setUserName("test"); user.setUserAge(28); user.setUserSex(true); byte[] json = new byte[0]; try { json = mapper.writeValueAsBytes(user); } catch (JsonProcessingException e) { e.printStackTrace(); } return json; } public String buildIndex() { XContentBuilder builder = null; try { builder = jsonBuilder() .startObject() .field("userId", "1001") .field("userName", "test") .field("userSex", true) .field("userAge", 28) .endObject(); } catch (IOException e) { e.printStackTrace(); } try { return builder.string(); } catch (IOException e) { e.printStackTrace(); } return null; } }
操作測試
新增
public void signInsert() { TransportClient client = ElasticSearchClient.getInstanceClient(); for(int i=0;i<100;i++) { IndexResponse response = client.prepareIndex("users", "user", ("" + i)) .setSource(ElasticsearchIndex.buildIndex()).get(); System.out.println(response.getIndex()); } }
* 修改
public void signUpdate() { TransportClient client = ElasticSearchClient.getInstanceClient(); UpdateRequest updateRequest = null; try { updateRequest = new UpdateRequest("users", "user", "2") .doc(jsonBuilder() .startObject() .field("userName", "male") .endObject()); } catch (IOException e) { e.printStackTrace(); } try { UpdateResponse response = client.update(updateRequest).get(); System.out.println(response.getIndex()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } }
根據(jù)ID刪除
public void signDel(){ TransportClient client = ElasticSearchClient.getInstanceClient(); DeleteResponse response = client.prepareDelete("users", "user", "3").get(); System.out.println(response.getIndex()); }
根據(jù)查詢條件刪除
public void searchDel(){ TransportClient client = ElasticSearchClient.getInstanceClient(); BulkByScrollResponse response = DeleteByQueryAction.INSTANCE.newRequestBuilder(client) .filter(QueryBuilders.matchQuery("userName", "male")) .source("users") .get(); long deleted = response.getDeleted(); System.out.println(deleted); }
查詢單條
public void get(){ TransportClient client = ElasticSearchClient.getInstanceClient(); GetResponse response = client.prepareGet("users", "user", "1").get(); if(response.isExists()){ System.out.println(response.getSourceAsString()); } System.out.println(response.getIndex()); }
批量查詢
查詢說明
public void search(){ TransportClient client = ElasticSearchClient.getInstanceClient(); /** * index 可以多個 * type 可以多個 */ SearchResponse response = client.prepareSearch("users") .setTypes("user") .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setQuery(QueryBuilders.termQuery("userName", "test")) // Query .setPostFilter(QueryBuilders.rangeQuery("userAge").from(12).to(30)) // Filter .setFrom(0).setSize(20).setExplain(true) .get(); System.out.println(response.getHits().getHits().length); System.out.println(response.getHits().getAt(0).getSourceAsString()); }參考博客
elasticsearch 配置說明
elasticsearch 索引說明
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/67892.html
閱讀 987·2021-11-24 10:30
閱讀 2316·2021-10-08 10:04
閱讀 3949·2021-09-30 09:47
閱讀 1433·2021-09-29 09:45
閱讀 1435·2021-09-24 10:33
閱讀 6234·2021-09-22 15:57
閱讀 2351·2021-09-22 15:50
閱讀 4079·2021-08-30 09:45