elasticsearch rest api 快速上手

注:本文檔中除無特別說明,請求方式均爲GET。所有的請求均在Sense中測試通過

遵循的格式爲

curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>

集羣健康查看

epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks 
1441940569 11:02:49  elasticsearch yellow          1         1      7   7    0    0        7             0 
host ip            heap.percent ram.percent load node.role master name     
acer 169.254.9.202           32          52      d         *      Mys-Tech

列出所有的indices

health status index              pri rep docs.count docs.deleted store.size pri.store.size 
yellow open   .marvel-2015.09.11   1   1       3233            0     10.5mb         10.5mb 
yellow open   .marvel-2015.09.10   1   1       1996            0      3.9mb          3.9mb 
yellow open   news                 5   1       3455            0     17.8mb         17.8mb 

創建索引

使用PUT請求創建一個countries的索引

curl -XPUT http://127.0.0.1:9200/countries?pretty

輸出:

{
   "acknowledged": true
}

查看索引列表

curl -XGET http://127.0.0.1:9200/_cat/indices?v

輸出:

health status index              pri rep docs.count docs.deleted store.size pri.store.size 
yellow open   countries              5   1          0            0       575b           575b 
yellow open   .marvel-2015.09.11   1   1       3436            0     11.4mb         11.4mb 
yellow open   .marvel-2015.09.10   1   1       1996            0      3.9mb          3.9mb 
yellow open   news                 5   1       3455            0     17.8mb         17.8mb 

索引文檔

  • 使用自定義id索引文檔

使用PUT請求創建一個索引爲countries類型爲country的文檔。其文檔編號爲1,文檔內容包含namecapital

curl -XPUT http://127.0.0.1:9200/countries/country/1?pretty -d '
{
  "name": "中國",
  "capital": "北京"
}'

輸出:

{
   "_index": "countries",
   "_type": "country",
   "_id": "1",
   "_version": 1,
   "created": true
}
  • 使用系統分配的id索引文檔
curl -XPOST http://127.0.0.1:9200/countries/country?pretty -d '
{
  "name": "韓國",
  "capital": "首爾"
}'

注意:使用系統分配的id時使用POST方式提交文檔,且在索引\類型url格式中不再有id

輸出:

{
   "_index": "countries",
   "_type": "country",
   "_id": "AU-6awteDgxJZYVN-E5I",
   "_version": 1,
   "created": true
}

查詢文檔

使用自定義id查詢文檔

curl -XGET http://127.0.0.1:9200/countries/country/1?pretty

輸出:

{
   "_index": "countries",
   "_type": "country",
   "_id": "1",
   "_version": 1,
   "found": true,
   "_source": {
      "name": "中國",
      "capital": "北京"
   }
}

使用系統分配的id查詢

GET http://127.0.0.1:9200/countries/country/AU-6awteDgxJZYVN-E5I?pretty

輸出:

{
   "_index": "countries",
   "_type": "country",
   "_id": "AU-6awteDgxJZYVN-E5I",
   "_version": 1,
   "found": true,
   "_source": {
      "name": "韓國",
      "capital": "首爾"
   }
}

查看索引信息

GET http://127.0.0.1:9200/countries/

輸出:

{
   "countries": {
      "aliases": {},
      "mappings": {
         "country": {
            "properties": {
               "capital": {
                  "type": "string"
               },
               "name": {
                  "type": "string"
               }
            }
         }
      },
      "settings": {
         "index": {
            "creation_date": "1441941497754",
            "uuid": "UaoQ_WCATaiy5w736cjw2A",
            "number_of_replicas": "1",
            "number_of_shards": "5",
            "version": {
               "created": "1070199"
            }
         }
      },
      "warmers": {}
   }
}

刪除索引

刪除myindex索引

DELETE http://127.0.0.1:9200/myindex/?pretty

輸出:

{
   "acknowledged": true
}

索引或替換一個文檔

根據文檔id索引或替換文檔,若存在則修改替換,否則索引該文檔。

  • 使用已存在的id

修改文檔id爲1的國家信息。

PUT 'http://127.0.0.1:9200/countries/country/1?pretty'
{
    "name": "日本",
    "capital": "東京"
}

查詢其是否已修改

GET http://127.0.0.1:9200/countries/country/1?pretty

輸出:

{
   "_index": "countries",
   "_type": "country",
   "_id": "1",
   "_version": 2,
   "found": true,
   "_source": {
      "name": "日本",
      "capital": "東京"
   }
}

可見國家信息已由中國變爲日本,其首都信息也發生了變化

  • 使用不存在的id則是索引文檔
PUT http://127.0.0.1:9200/countries/country/2?pretty
{
    "name": "澳大利亞",
    "capital": "悉尼"
}

輸出:

{
   "_index": "countries",
   "_type": "country",
   "_id": "2",
   "_version": 1,
   "created": true
}

修改文檔

  • 按doc方式更新文檔

doc方式修改文檔id爲1的文檔

POST http://127.0.0.1:9200/countries/country/1/_update?pretty
{
  "doc": { "name": "美國","capital": "華盛頓"}
}

其中doc是固定寫法,其內容爲要修改的文檔內容

  • 按script方式更新文檔

script方式修改文檔id爲1的文檔

POST http://127.0.0.1:9200/countries/country/1/_update?pretty
{
  "script": "ctx._source.name=\"加拿大\";ctx._source.capital=\"渥太華\""
}

刪除文檔

  • 按文檔id刪除
DELETE http://127.0.0.1:9200/countries/country/1?pretty

輸出:

{
   "found": true,
   "_index": "countries",
   "_type": "country",
   "_id": "1",
   "_version": 6
}
  • 根據查詢結果刪除
DELETE http://127.0.0.1:9200/countries/country/_query?pretty
{
    "query": { "match": { "name": "美國" } }
}

輸出:

{
   "_indices": {
      "countries": {
         "_shards": {
            "total": 5,
            "successful": 5,
            "failed": 0
         }
      }
   }
}

查詢是否還有name爲美國的文檔

GET http://127.0.0.1:9200/countries/country/_query
{
    "query": { "match_all": { "name": "美國" } }
}

批量處理

_bulk api

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html

遵循格式

/_bulk, /{index}/_bulk, {index}/{type}/_bulk

action_and_meta_data\n
optional_source\n
action_and_meta_data\n
optional_source\n

支持的action有indexcreatedeleteupdate

indexcreate在下一行跟上要索引的doc delete則不需要 update在下一行跟上docscript

  • 批量索引文檔
POST http://127.0.0.1:9200/countries/country/_bulk?pretty
{"index":{"_id":"1"}}
{"name": "中國","capital": "北京"}
{"index":{"_id":"2"}}
{"name": "美國","capital": "華盛頓"}
{"index":{"_id":"3"}}
{"name": "日本","capital": "東京"}
{"index":{"_id":"4"}}
{"name": "澳大利亞","capital": "悉尼"}
{"index":{"_id":"5"}}
{"name": "印度","capital": "新德里"}
{"index":{"_id":"6"}}
{"name": "韓國","capital": "首爾"}

以上請求將會批量索引6個文檔。

輸出:

{
   "took": 4,
   "errors": false,
   "items": [
      {
         "index": {
            "_index": "countries",
            "_type": "country",
            "_id": "1",
            "_version": 1,
            "status": 201
         }
      },
      {
         "index": {
            "_index": "countries",
            "_type": "country",
            "_id": "2",
            "_version": 2,
            "status": 200
         }
      },
      {
         "index": {
            "_index": "countries",
            "_type": "country",
            "_id": "3",
            "_version": 1,
            "status": 201
         }
      },
      {
         "index": {
            "_index": "countries",
            "_type": "country",
            "_id": "4",
            "_version": 1,
            "status": 201
         }
      },
      {
         "index": {
            "_index": "countries",
            "_type": "country",
            "_id": "5",
            "_version": 1,
            "status": 201
         }
      },
      {
         "index": {
            "_index": "countries",
            "_type": "country",
            "_id": "6",
            "_version": 1,
            "status": 201
         }
      }
   ]
}
  • 批量執行,含index、create、delete、update
POST http://127.0.0.1:9200/countries/country/_bulk?pretty
{"index":{"_id":"7"}}
{"name": "新加坡","capital": "渥太華"}
{"create":{"_id":"8"}}
{"name": "德國","capital": "柏林"}
{"update":{"_id":"1"}}
{"doc": {"name": "法國","capital": "巴黎" }}
{"update":{"_id":"3"}}
{"script": "ctx._source.name = \"法國\";ctx._source.capital = \"巴黎\""}
{"delete":{"_id":"2"}}

輸出:

{
   "took": 40,
   "errors": false,
   "items": [
      {
         "index": {
            "_index": "countries",
            "_type": "country",
            "_id": "7",
            "_version": 1,
            "status": 201
         }
      },
      {
         "create": {
            "_index": "countries",
            "_type": "country",
            "_id": "8",
            "_version": 1,
            "status": 201
         }
      },
      {
         "update": {
            "_index": "countries",
            "_type": "country",
            "_id": "1",
            "_version": 2,
            "status": 200
         }
      },
      {
         "update": {
            "_index": "countries",
            "_type": "country",
            "_id": "3",
            "_version": 2,
            "status": 200
         }
      },
      {
         "delete": {
            "_index": "countries",
            "_type": "country",
            "_id": "2",
            "_version": 3,
            "status": 200,
            "found": true
         }
      }
   ]
}
  • 導入數據

countries.json

{"index":{"_id":"1"}}
{"name": "新加坡","capital": "渥太華"}
{"index":{"_id":"2"}}
{"name": "韓國","capital": "首爾"}
{"index":{"_id":"3"}}
{"name": "朝鮮","capital": "平壤"}
{"index":{"_id":"4"}}
{"name": "日本","capital": "東京"}
{"index":{"_id":"5"}}
{"name": "馬來西亞","capital": "吉隆坡"}

使用curl的--data-binary參數導入數據

curl XPOST http://127.0.0.1:9200/countries/country/_bulk?pretty --data-binary @countries.json

或者使用postman導入

http://127.0.0.1:9200/countries/country/_bulk?pretty
{"index":{"_id":"1"}}
{"name": "新加坡","capital": "渥太華"}
{"index":{"_id":"2"}}
{"name": "韓國","capital": "首爾"}
{"index":{"_id":"3"}}
{"name": "朝鮮","capital": "平壤"}
{"index":{"_id":"4"}}
{"name": "日本","capital": "東京"}
{"index":{"_id":"5"}}
{"name": "馬來西亞","capital": "吉隆坡"}

search api

  • GET方式搜索(queryString)
GET http://127.0.0.1:9200/countries/_search?q=*&pretty

注:q=*將匹配索引中的所有文檔

輸出:

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 10,
      "max_score": 1,
      "hits": [
         {
            "_index": "countries",
            "_type": "country",
            "_id": "4",
            "_score": 1,
            "_source": {
               "name": "日本",
               "capital": "東京"
            }
         },
         {
            "_index": "countries",
            "_type": "country",
            "_id": "_query",
            "_score": 1,
            "_source": {
               "query": {
                  "match_all": {
                     "name": "美國"
                  }
               }
            }
         },
         {
            "_index": "countries",
            "_type": "country",
            "_id": "5",
            "_score": 1,
            "_source": {
               "name": "印度",
               "capital": "新德里"
            }
         },
         {
            "_index": "countries",
            "_type": "country",
            "_id": "6",
            "_score": 1,
            "_source": {
               "name": "韓國",
               "capital": "首爾"
            }
         },
         {
            "_index": "countries",
            "_type": "country",
            "_id": "1",
            "_score": 1,
            "_source": {
               "name": "新加坡",
               "capital": "渥太華"
            }
         },
         {
            "_index": "countries",
            "_type": "country",
            "_id": "7",
            "_score": 1,
            "_source": {
               "name": "新加坡",
               "capital": "渥太華"
            }
         },
         {
            "_index": "countries",
            "_type": "country",
            "_id": "2",
            "_score": 1,
            "_source": {
               "name": "韓國",
               "capital": "首爾"
            }
         },
         {
            "_index": "countries",
            "_type": "country",
            "_id": "AU-6awteDgxJZYVN-E5I",
            "_score": 1,
            "_source": {
               "name": "韓國",
               "capital": "首爾"
            }
         },
         {
            "_index": "countries",
            "_type": "country",
            "_id": "8",
            "_score": 1,
            "_source": {
               "name": "德國",
               "capital": "柏林"
            }
         },
         {
            "_index": "countries",
            "_type": "country",
            "_id": "3",
            "_score": 1,
            "_source": {
               "name": "朝鮮",
               "capital": "平壤"
            }
         }
      ]
   }
}
  • POST方式搜索(含請求體query)
POST http://127.0.0.1:9200/countries/_search?pretty
{
    "query": {
        "match_all": {}
    }
}

輸出:

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 10,
      "max_score": 1,
      "hits": [
         {
            "_index": "countries",
            "_type": "country",
            "_id": "4",
            "_score": 1,
            "_source": {
               "name": "日本",
               "capital": "東京"
            }
         },
         {
            "_index": "countries",
            "_type": "country",
            "_id": "_query",
            "_score": 1,
            "_source": {
               "query": {
                  "match_all": {
                     "name": "美國"
                  }
               }
            }
         },
         {
            "_index": "countries",
            "_type": "country",
            "_id": "5",
            "_score": 1,
            "_source": {
               "name": "印度",
               "capital": "新德里"
            }
         },
         {
            "_index": "countries",
            "_type": "country",
            "_id": "6",
            "_score": 1,
            "_source": {
               "name": "韓國",
               "capital": "首爾"
            }
         },
         {
            "_index": "countries",
            "_type": "country",
            "_id": "1",
            "_score": 1,
            "_source": {
               "name": "新加坡",
               "capital": "渥太華"
            }
         },
         {
            "_index": "countries",
            "_type": "country",
            "_id": "7",
            "_score": 1,
            "_source": {
               "name": "新加坡",
               "capital": "渥太華"
            }
         },
         {
            "_index": "countries",
            "_type": "country",
            "_id": "2",
            "_score": 1,
            "_source": {
               "name": "韓國",
               "capital": "首爾"
            }
         },
         {
            "_index": "countries",
            "_type": "country",
            "_id": "AU-6awteDgxJZYVN-E5I",
            "_score": 1,
            "_source": {
               "name": "韓國",
               "capital": "首爾"
            }
         },
         {
            "_index": "countries",
            "_type": "country",
            "_id": "8",
            "_score": 1,
            "_source": {
               "name": "德國",
               "capital": "柏林"
            }
         },
         {
            "_index": "countries",
            "_type": "country",
            "_id": "3",
            "_score": 1,
            "_source": {
               "name": "朝鮮",
               "capital": "平壤"
            }
         }
      ]
   }
}
  • 限定返回條目
POST http://127.0.0.1:9200/countries/_search?pretty
{
    "query": {
        "match_all": {}
    },
    "size": 1
}

size控制返回條目,默認爲10

輸出:

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 10,
      "max_score": 1,
      "hits": [
         {
            "_index": "countries",
            "_type": "country",
            "_id": "4",
            "_score": 1,
            "_source": {
               "name": "日本",
               "capital": "東京"
            }
         }
      ]
   }
}
  • 分頁(form,size)
POST http://127.0.0.1:9200/countries/_search?pretty
{
    "query": {
        "match_all": {}
    },
    "from": 2,
    "size": 2
}

使用fromsize來翻頁。其中form默認爲0,size默認爲10

  • 排序(sort)
POST http://127.0.0.1:9200/countries/_search?pretty
{
    "query": {
        "match_all": {}
    },
    "sort": [
       {
          "name": {
             "order": "desc"
          }
       }
    ]
}

其中name爲排序字段

限定返回字段

POST http://127.0.0.1:9200/countries/_search?pretty
{
    "query": {
        "match_all": {}
    },
    "_source": ["name"]
}

使用_source來限定返回的字段。這裏只返回name

高級查詢

  • match_phrase
POST http://127.0.0.1:9200/countries/_search?pretty
{
    "query": {
        "match_phrase": {
           "name": "韓國"
        }
    }
}
  • must
POST http://127.0.0.1:9200/countries/_search?pretty
{
    "query": {
        "bool": {
            "must": [
               {"match": {
                  "name": "日本"
               }},
               {"match": {
                  "capital": "東京"
               }}
            ]
        }
    }
}
  • should
POST http://127.0.0.1:9200/countries/_search?pretty
{
    "query": {
        "bool": {
            "should": [
               {"match": {
                  "name": "日本"
               }},
               {"match": {
                  "name": "韓國"
               }}
            ]
        }
    }
}
  • must_not
POST http://127.0.0.1:9200/countries/_search?pretty
{
    "query": {
        "bool": {
            "must_not": [
               {"match": {
                  "name": "日本"
               }},
               {"match": {
                  "name": "韓國"
               }}
            ]
        }
    }
}
  • filter
POST http://127.0.0.1:9200/countries/_search?pretty
{
    "query": {
        "match_all": {}
    },
    "filter": {
        "term": {
           "capital": "東京"
        }
    }
}
  • 聚合(aggs)
POST http://127.0.0.1:9200/countries/_search?pretty
{
  "size": 0,
  "aggs": {
    "group_by_name": {
      "terms": {
        "field": "name"
      }
    }
  }
}

name統計分組文檔數

輸出:

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 10,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "group_by_name": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "韓國",
               "doc_count": 3
            },
            {
               "key": "新加坡",
               "doc_count": 2
            },
            {
               "key": "印度",
               "doc_count": 1
            },
            {
               "key": "德國",
               "doc_count": 1
            },
            {
               "key": "日本",
               "doc_count": 1
            },
            {
               "key": "朝鮮",
               "doc_count": 1
            }
         ]
      }
   }
}

高亮查詢(highlight)

POST http://127.0.0.1:9200/news/_search?q=李克強
{
    "query" : {
        match_all:{}
    },
    "highlight" : {
        "pre_tags" : ["<font color='red'>", "<b>", "<em>"],
        "post_tags" : ["</font>", "<b>", "</em>"],
        "fields" : [
            {"title" : {}},
            {"content" : {
                "fragment_size" : 350,
                "number_of_fragments" : 3,
                "no_match_size": 150
            }}
        ]
    }
}
POST http://127.0.0.1:9200/news/_search?q=李克強
{
    "query" : {
        match_all:{}
    },
    "highlight" : {
        "pre_tags" : ["<font color='red'><b><em>"],
        "post_tags" : ["</font><b></em>"],
        "fields" : [
            {"title" : {}},
            {"content" : {
                "fragment_size" : 350,
                "number_of_fragments" : 3,
                "no_match_size": 150
            }}
        ]
    }
}

刪除索引

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html

DELETE http://127.0.0.1:9200/news

創建索引

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html

PUT http://127.0.0.1:9200/news

創建或修改mapping

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html

PUT /{index}/_mapping/{type}
PUT http://127.0.0.1:9200/news/_mapping/article
{
  "article": {
    "properties": {
      "pubdate": {
        "type": "date",
        "format": "dateOptionalTime"
      },
      "author": {
        "type": "string"
      },
      "content": {
        "type": "string"
      },
      "id": {
        "type": "long"
      },
      "source": {
        "type": "string"
      },
      "title": {
        "type": "string"
      },
      "url": {
        "type": "string"
      }
    }
  }
}

查看mapping

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html

GET http://127.0.0.1:9200/_all/_mapping

GET http://127.0.0.1:9200/_mapping
GET http://127.0.0.1:9200/news/_mapping/article

輸出:

{
  "news": {
    "mappings": {
      "article": {
        "properties": {
          "author": {
            "type": "string"
          },
          "content": {
            "type": "string"
          },
          "id": {
            "type": "long"
          },
          "pubdate": {
            "type": "date",
            "store": true,
            "format": "yyyy-MM-dd HH:mm:ss"
          },
          "source": {
            "type": "string"
          },
          "title": {
            "type": "string"
          },
          "url": {
            "type": "string"
          }
        }
      }
    }
  }
}

刪除mapping

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-mapping.html

[DELETE] /{index}/{type}

[DELETE] /{index}/{type}/_mapping

[DELETE] /{index}/_mapping/{type}
DELETE http://127.0.0.1:9200/news/_mapping/article

ansj分詞器測試

http://127.0.0.1:9200/news/_analyze?analyzer=ansj_index&text=習近平

http://127.0.0.1:9200/news/_analyze?analyzer=ansj_index&text=我是中國人

http://127.0.0.1:9200/news/_analyze?analyzer=ansj_index&text=汪東興同志遺體在京火化汪東興同志病重期間和逝世後,習近平李克強張德江俞正聲劉雲山王岐山張高麗江澤民胡錦濤等同志,前往醫院看望或通過各種形式對汪東興同志逝世表示沉痛哀悼並向其親屬表示深切慰問新華社北京8月27日電中國共產黨的優秀黨員

ansj分詞器查詢

  • 普通查詢

http://127.0.0.1:9200/news/_search?q=習近平&analyzer=ansj_index&size=50

  • 指定term查詢

http://127.0.0.1:9200/news/_search?q=content:江澤民&analyzer=ansj_index&size=50

http://127.0.0.1:9200/news/_search?q=title:江澤民&analyzer=ansj_index&size=50

http://127.0.0.1:9200/news/_search?q=source:新華網&analyzer=ansj_index&size=50

  • 其中ansj_index爲在elasticsearch.yml文件中配置的ansj分詞器
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章