1 使用postman對elasticsearch進行測試
:下載插件: https://www.getpostman.com/apps ,下載時exe文件,雙擊自動安裝,首次打開注冊。下面就可以使用進行測試
請求:
響應:
2:elasticsearch中可以使用post,put,get等請求方式,索引原理:https://my.oschina.net/90888/blog/1617292
3:對elasticsearch操作有,post新增索引,數據,put更新索引,數據,get獲取,分詞等
3.1:新增一條索引,數據,index只能小寫。
新增結果:
如果post中沒有id值,elasticsearch將會自動創建id,
3.2 查詢數據
往查詢url POST數據即可:
URL格式:http://xxxhost:8201/qa_xx2/qa_xx3/_search
a. 查詢title中包含有my字段的文檔。Highlight設置高亮命中的詞。POST方法的body:
{ "query": { "match": { "title": { //字段 "query": "my " } } }, "highlight": { "fields": { "title": { //查詢到的文檔進行高亮 } } } }
結果:查詢成功
{ "took": 3, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.2876821, "hits": [ { "_index": "indextest", "_type": "type", "_id": "i_WWCGIBMRJTfK5J3FTg", "_score": 0.2876821, "_source": { //源文檔 "title": "this is my fisrt elasticsearch test", "content": "Elasticsearch is a highly scalable open-source full-text search and analytics engine. It allows you to store, search, and analyze big volumes of data quickly and in near real time. It is generally used as the underlying engine/technology that powers applications that have complex search features and requirements.", "desc": "結束,字段名相當於列名" }, "highlight": { //高亮顯示 "title": [ "this is <em>my</em> fisrt elasticsearch test" ] } } ] } }
4 使用javaapi對elasticsearch進行操作
4.1 添加maven依賴 build.gradle
'org.elasticsearch:elasticsearch:6.0.0',
'org.elasticsearch.client:transport:6.0.0',
'com.alibaba:fastjson:1.2.44',
4.2進行測試
@Test public void getData() { try { Settings settings = Settings.builder().put("cluster.name", "my-esLearn").build(); //創建client client = new PreBuiltTransportClient(settings) .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); //搜索數據 blog 索引,article,類型,1 id GetResponse response = client.prepareGet("blog", "article", "1").execute().actionGet(); //輸出結果 System.out.println(response.getSource()); //關閉client client.close(); } catch (Exception e) { e.printStackTrace(); } }
結果:
5:使用java api對elasticsearch進行請求並指定ik分詞器
@Test public void test2() throws IOException { BulkRequestBuilder bulkRequest = client.prepareBulk(); // either use client#prepare, or use Requests# to directly build index/delete requests bulkRequest.add(client.prepareIndex("twitter", "tweet", "1") //創建索引 .setSource(jsonBuilder() //請求體,數據 .startObject() .field("analyzer","ik_max_word") //指定分詞器 .field("user", "kimchy") .field("postDate", new Date()) .field("message", "trying out Elasticsearch") .endObject() ) ); bulkRequest.add(client.prepareIndex("twitter", "tweet", "2") .setSource(jsonBuilder() .startObject() .field("user", "kimchy") .field("postDate", new Date()) .field("message", "another post") .endObject() ) ); BulkResponse bulkResponse = bulkRequest.get(); if (bulkResponse.hasFailures()) { // process failures by iterating through each bulk response item } BulkItemResponse[] items = bulkResponse.getItems(); //一次發送多個請求,會返回一個響應數組。 DocWriteResponse response = items[0].getResponse(); String index = response.getIndex(); System.out.println(index); //結果 twitter }
參考:https://www.cnblogs.com/cswuyg/p/5651620.html
javaapi 操作elasticsearch http://blog.csdn.net/zjcjava/article/details/78659721
英文 文檔:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html
1. Elasticsearch在電商領域的實戰應用
http://t.cn/REEzwES
2.使用Docker和Elasticsearch搭建全文本搜索引擎應用
http://t.cn/REEzUng
3.剖析Elasticsearch索引原理
https://my.oschina.net/90888/blog/1617292
編輯:銘毅天下
歸檔:https://elasticsearch.cn/article/525
訂閱:https://tinyletter.com/elastic-daily