Elasticsearch-6.x.x 操作集合

#健康檢查
curl 'localhost:9200/_cat/health?v'
status:綠色代表一切正常(集羣功能齊全),黃色意味着所有的數據都是可用的,但是某些複製沒有被分配(集羣功能齊全),紅色則代表因爲某些原因,某些數據不可用
#節點查詢
curl 'localhost:9200/_cat/nodes?v'
#索引查詢
curl 'localhost:9200/_cat/indices?v'
1.新建和刪除Index
curl -X PUT 'localhost:9200/xxx'
curl -X DELETE 'localhost:9200/weather'
2.新增記錄
curl -X PUT 'localhost:9200/hupu/xxx/1' -d '
{
    "title:"怎麼選?",
    "readnum:"432",
    "likeNum:"32"
}'
id可以指定,可以不指定;id 不一定是數字,也可以是字符串
3.查看記錄
curl 'localhost:9200/hupu/xxx/1?pretty=true'
4.刪除記錄
curl -X DELETE 'localhost:9200/hupu/xxx/1'
5.更新記錄
curl -X PUT 'localhost:9200/hupu/xxx/1' -d '
{
    "title":"曾某",
    "readnum":"1",
    "likenum":"23"
}
6.返回所有記錄
curl 'localhost:9200/hupu/xxx/_search'
took字段表示該操作的耗時(單位爲毫秒),time_out字段表示是否超時,hits字段表示命中記錄組成的數組 total:返回記錄數,本例是2條。max_score:最高的匹配程度,本例是1.0 
7.全文搜索
elastic的查詢非常特別,使用自己的查詢語法,要求GET請求帶數據體。
curl 'localhost:9200/accounts/person/_search' -d '
{
    "query":{"match":{"desc":"軟件"}},
    "from":1,
    "size":1
}'
elastic 默認一次返回10條結果,可以通過size字段改變這個設置.還可以通過from 字段,指定位移。
8.邏輯運算
如果有多個搜索關鍵字,elastic認爲它們是or關係。
curl localhost:9200/accounts/person/_search' -d '
{
    "query":{"match":{"desc":"軟件 系統"}}
},
如果要執行多個關鍵字的and搜索,必須使用布爾查詢
curl 'localhost:9200/accounts/person/_search' -d [
{
    "query":{
        "bool":{
            "must":[
                { "match": { "desc": "軟件" } },
                { "match": { "desc": "系統" } }
            ]
        }
    }
}





1.創建索引

PUT exchange
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "test"
}

注意:索引名不能包含大些字母

2.指定參數創建索引

PUT blog
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  } 
}
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "blog"
}

3.查看索引

3.1 查看指定索引的配置信息

GET blog/_settings
{
  "blog": {
    "settings": {
      "index": {
        "creation_date": "1515458969949",
        "number_of_shards": "3",
        "number_of_replicas": "1",
        "uuid": "A7pKNO7bTgucu1uNgmXlQg",
        "version": {
          "created": "5060399"
        },
        "provided_name": "blog"
      }
    }
  }
}

4.刪除索引

DELETE test
{
  "acknowledged": true
}

5.添加索引並指定類型字段信息

PUT exchangehistory
{
    "settings" : {
        "number_of_shards" : 5
    },
    "mappings" : {
        "record" : {
            "properties" : {
        "amount": {
          "type": "float"
        },
        "contract": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "exchange_time": {
          "type": "date"
        },
        "id": {
          "type": "long"
        },
        "last": {
          "type": "float"
        },
        "time": {
          "type": "date"
        },
        "volume": {
          "type": "float"
        }
      }
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章