使用cURL嘗試ElasticSearch

測試環境:debian 9
官網提供了 deb,rpm,源碼下載java

官方下載地址:https://www.elastic.co/downloads/elasticsearchgit

經過源碼安裝會遇到一些小問題,爲了方便,我直接下載deb安裝(我不肯定經過deb安裝是否須要java環境,由於我提早安裝了openjdk-8-jdk)。github

能夠經過 service elasticsearch start/stop 啓動關閉服務,默認監聽了 9200端口,能夠更改配置文件json

經過deb安裝的配置文件在:/etc/elasticsearch/elasticsearch.ymlapp

若是要在localhost外鏈接elasticsearch ,更改配置文件中的 network.host:0.0.0.0curl

若是一塊兒順利就能夠開始測試了elasticsearch

查看es基本信息
curl localhost:9200

列出全部的Index
curl -X GET 'http://localhost:9200/_cat/indices?v'

列舉每一個Index下的Type
curl 'localhost:9200/_mapping?pretty=true'

添加Index
curl -X PUT 'localhost:9200/weather'

刪除Index
curl -X DELETE 'localhost:9200/weather'

安裝中文分詞插件ik (安裝完須要重啓es)
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.4/elasticsearch-analysis-ik-6.5.4.zip

建立一個Index,並設置其結構和分詞
curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/accounts' -d '
{
  "mappings": {
    "person": {
      "properties": {
        "user": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word"
        },
        "title": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word"
        }
      }
    }
  }
}'

向Index增長記錄
PUT方式
curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/accounts/person/1' -d '
{
  "user": "張三",
  "title": "工程師"
}' 

POST方式(POST方式不須要傳id,id隨機生成)
curl -X POST -H 'Content-Type: application/json' 'localhost:9200/accounts/person' -d '
{
  "user": "李四",
  "title": "工程師"
}'
注意:若是沒有先建立 Index(這個例子是accounts),直接執行上面的命令,Elastic 也不會報錯,而是直接生成指定的 Index。因此,打字的時候要當心,不要寫錯 Index 的名稱。

查看指定條目的記錄
curl 'localhost:9200/accounts/person/1?pretty=true'

刪除一條記錄
curl -X DELETE 'localhost:9200/accounts/person/1'

更新一條記錄
curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/accounts/person/1' -d '
{
    "user" : "張三",
    "title" : "軟件開發"
}' 

查詢全部記錄
curl 'localhost:9200/accounts/person/_search?pretty=true'

簡單查詢
curl -H 'Content-Type: application/json' 'localhost:9200/accounts/person/_search?pretty=true'  -d '
{
  "query" : { "match" : { "title" : "工程" }},
  "from": 1, #0開始
  "size": 1, #返回幾條數據
}'

OR查詢
curl -H 'Content-Type: application/json' 'localhost:9200/accounts/person/_search?pretty=true'  -d '
{
  "query" : { "match" : { "title" : "工程 哈哈" }}
}'

AND查詢
curl -H 'Content-Type: application/json' 'localhost:9200/accounts/person/_search?pretty=true'  -d '
{
  "query": {
    "bool": {
      "must": [
        { "match": { "title": "工程" } },
        { "match": { "title": "哈哈" } }
      ]
    }
  }
}'
相關文章
相關標籤/搜索