Elasticsearch基礎之:通過Restful API操作數(shù)據(jù)

1、上傳數(shù)據(jù)

PUT http://localhost:9200/megacorp/employee/1
請求body的內(nèi)容:

{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}

注:
也可以用以下命令來上傳數(shù)據(jù),
POST http://localhost:9200/megacorp/employee/
這個時候上傳之后的"_ID"就不像上面自定義的為1,而是由elasticsearch自動生成ID。
PUT方式相當于是覆蓋數(shù)據(jù),可以用來做修改操作

如果是批量上傳數(shù)據(jù),可以使用如下命令:
curl -XPOST 127.0.0.1:9200/bank/account/_bulk?pretty --data-binary @accounts.json
其中accounts.json 是一個json數(shù)據(jù)

2、查詢數(shù)據(jù)

{"_index":"megacorp","_type":"employee","_id":"1","_version":1,"found":true,"_source":{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}}
{"took":6,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":3,"max_score":1.0,"hits":[{"_index":"megacorp","_type":"employee","_id":"2","_score":1.0,"_source":{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}},{"_index":"megacorp","_type":"employee","_id":"1","_score":1.0,"_source":{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}},{"_index":"megacorp","_type":"employee","_id":"3","_score":1.0,"_source":{
    "first_name" :  "Douglas",
    "last_name" :   "Fir",
    "age" :         35,
    "about":        "I like to build cabinets",
    "interests":  [ "forestry" ]
}}]}}
{
  "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    },
  "_source":["first_name","last_name","age"],
  "from":0,
  "size":2,
  "sort":{"first_name":{"order":"desc"}}
}

其中:
from和size是起到分頁的作用,sort是排序作用
_source代表結(jié)果只顯示這幾個字段


當然還有更復雜的語法,例如增加filter:

{
    "query" : {
        "filtered" : {
            "filter" : {
                "range" : {
                    "age" : { "gt" : 30 }
                }
            },
            "query" : {
                "match" : {
                    "last_name" : "smith"
                }
            }
        }
    }
}

范圍操作符包含:
gt : 大于
gte : 大于等于
lt : 小于
lte : 小于等于

{
    "query" : {
        "match" : {
            "about" : "rock climbing"
        }
    }
}

以上查詢代表查詢about字段包含rock或者climbing的數(shù)據(jù)
如果要查詢about字段包含"rock climbing"整個字符串的數(shù)據(jù),那要這么查:

{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    }
}
  • 2.5、bool搜索

POST http://localhost:9200/megacorp/employee/_search
請求body如下:

{
  "query" : {
    "bool":{
        "must":[
          {"match" : {"first_name" : "Jane"}},
          {"match" : {"last_name" : "Smith"}}
          ]
    }
  }
}

返回的結(jié)果只包含Jane Smith
其中:
1、must是代表必須同時滿足,相當于&&
2、如果must改成should,代表有一個滿足就可以,相當于||,得到的結(jié)果是John Smith和Jane Smith
3、如果must改成must_not,代表兩個都不滿足,得到的結(jié)果是Douglas Fir

{
    "aggs" : {
        "all_interests" : {
            "terms" : { "field" : "interests" },
            "aggs" : {
                "avg_age" : {
                    "avg" : { "field" : "age" }
                }
            }
        }
    }
}

其中,terms是分組操作,相當于group by。

返回結(jié)果:

{
...,
"aggregations": {
        "all_interests": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "music",
                    "doc_count": 2,
                    "avg_age": {
                        "value": 28.5
                    }
                },
                {
                    "key": "forestry",
                    "doc_count": 1,
                    "avg_age": {
                        "value": 35
                    }
                },
                {
                    "key": "sports",
                    "doc_count": 1,
                    "avg_age": {
                        "value": 25
                    }
                }
            ]
        }
    }

}

3、刪除數(shù)據(jù)

DELETE http://localhost:9200/megacorp/employee/123

總結(jié)

索引操作的命令如下:

1、獲取索引 
curl -XGET ‘[http://localhost:9200/](http://localhost:9200/){index}/{type}/{id}’ 
2、索引數(shù)據(jù) 
curl -XPOST ‘[http://localhost:9200/](http://localhost:9200/){index}/{type}/{id}’ -d’{“a”:”avalue”,”b”:”bvalue”}’ 
3、刪除索引 
curl -XDELETE ‘[http://localhost:9200/](http://localhost:9200/){index}/{type}/{id}’ 
4、設置mapping

curl -XPUT [http://localhost:9200/](http://localhost:9200/){index}/{type}/_mapping -d ‘{ 
“{type}” : { 
“properties” : { 
“date” : { 
“type” : “l(fā)ong” 
}, 
“name” : { 
“type” : “string”, 
“index” : “not_analyzed” 
}, 
“status” : { 
“type” : “integer” 
}, 
“type” : { 
“type” : “integer” 
} 
} 
} 
}’ 
5、獲取mapping 
curl -XGET [http://localhost:9200/](http://localhost:9200/){index}/{type}/_mapping 
6、搜索

curl -XGET ‘[http://localhost:9200/](http://localhost:9200/){index}/{type}/_search’ -d ‘{ 
“query” : { 
“term” : { “user” : “kimchy” } //查所有 “match_all”: {} 
}, 
“sort” : [{ “age” : {“order” : “asc”}},{ “name” : “desc” } ], 
“from”:0, 
“size”:100 
} 
curl -XGET ‘[http://localhost:9200/](http://localhost:9200/){index}/{type}/_search’ -d ‘{ 
“filter”: {“and”:{“filters”:[{“term”:{“age”:”123”}},{“term”:{“name”:”張三”}}]}, 
“sort” : [{ “age” : {“order” : “asc”}},{ “name” : “desc” } ], 
“from”:0, 
“size”:100 
}

參考

elasticsearch 通過HTTP RESTful API 操作數(shù)據(jù)
https://es.xiaoleilu.com/010_Intro/25_Tutorial_Indexing.html
使用curl命令操作elasticsearch And 使用http 查詢ES

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、環(huán)境搭建 軟件版本: Linux:centos7 64bit JDK:1.8.0 elasticsearch:...
    lailai900201閱讀 3,687評論 0 12
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,533評論 19 139
  • ElasticSearch(以下簡稱ES)是一個基于Apache Lucene(TM)的開源搜索引擎。無論在開源還...
    Watson_Xu閱讀 263評論 0 2
  • ListView的優(yōu)化https://github.com/sghiassy/react-native-sglis...
    精神病患者link常閱讀 4,902評論 0 22
  • 2017年的最后一天,我榮幸的加入了007這個大家庭。提起筆準備寫文章的時候,突然發(fā)現(xiàn)自己好詞窮,感覺自己腦海中一...
    石頭愛湉江閱讀 180評論 0 1

友情鏈接更多精彩內(nèi)容