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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Elasticsearch使用REST API实现全文检索

發布時間:2023/11/29 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Elasticsearch使用REST API实现全文检索 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Elasticsearch REST API

elasticsearch支持通過http請求響應服務,http請求默認使用9200斷開,因此通過curl命令,可以發送http請求,并得到json返回內容。常用的REST API包括一下幾個:

檢查ES集群狀態

curl http://localhost:9200/_cat/health?v

檢查ES節點的狀態

curl http://localhost:9200/_cat/nodes?v

查詢所有的索引

curl http://localhost:9200/_cat/indices?v

創建索引

curl -XPUT http://localhost:9200/myindex/mytype/id -H 'Content-Type: application/json' -d '{"name":"ysl"}'

刪除索引

curl -XDELETE http://localhost:9200/myindex

查詢索引

curl -XGET http://localhost:9200/myindex/mytype/id

添加數據

curl -XPUT 'http://localhost:9200/kimchy/doc/1?pretty' -H 'Content-Type: application/json' -d ' {"user": "kimchy","post_date": "2009-11-15T13:12:00","message": "Trying out Elasticsearch, so far so good?" }'

查詢數據

curl -XGET 'http://localhost:9200/kimchy,another_user/_search?pretty=true' -H 'Content-Type: application/json' -d ' {"query" : {"match_all" : {}} }'

批量添加數據

動態映射無法添加數據,不要擔心!可以使用bulk命令,添加json文件內的數據

定義json數據

{"index":{"_index":"test123","_id":1}} {"name":"ysl","age":25} {"index":{"_index":"test123","_id":2}} {"name":"wdd","age":25} {"index":{"_index":"test123","_id":3}} {"name":"yjb","age":25}

注意的是,json文件名稱隨意指定,第一行定義了索引和一些常用字段:

  •  _index定義了索引的名稱,如果沒有指定需要在curl命令中添加索引名稱字段
  • _type定義了索引的類型,如果沒有指定需要在curl命令中添加索引類型字段
  •  _id定義了該行數據的id,如果沒有指定,會隨機生成一串數字。

執行命令

進入到json文件所在的目錄,執行一下命令:

curl localhost:9200/索引名稱/索引類型/_bulk?pretty --data-binary @data.json

注意的是:如果json文件中定義了_index和_type,那么這里可以不寫(即便寫了也會按照json文件中的生成)。

curl localhost:9200/_bulk?pretty --data-binary @data.json

類似的,如果按照上面我們定義了_index卻沒有定義_type,那么索引會是test123,類型為我們curl命令中指定的類型。

執行上述命令

curl http://localhost:9200/test123/test123/_bulk?pretty --data-binary @data.json

得到以下結果

{"took" : 1233,"errors" : false,"items" : [ {"index" : {"_index" : "test123","_type" : "test123","_id" : "1","_version" : 1,"_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"status" : 201}}, {"index" : {"_index" : "test123","_type" : "test123","_id" : "2","_version" : 1,"_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"status" : 201}} ] }

轉載于:https://www.cnblogs.com/senlinyang/p/8337076.html

總結

以上是生活随笔為你收集整理的Elasticsearch使用REST API实现全文检索的全部內容,希望文章能夠幫你解決所遇到的問題。

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