zoukankan      html  css  js  c++  java
  • REST API

    下面简单介绍下elasticsearch提供的一套api的用法:

    1. 创建索引 PUT (注意,index名称里的字母必须小写)

    下面创建了索引jiu3,里面配置信息重写,否则默认5个分片和一个副本,映射里写了一个type:lvh,以及两个字段。当然,大括号里这些并不是必需的。

    1 curl -XPUT 192.168.100.15:9200/index
     1 curl -XPUT http://192.168.100.15:9200/jiu3/ -d '{
     2     "settings":{
     3         "index":{
     4             "number_of_shards":1,
     5             "number_of_replicas":0
     6         }
     7     },
     8     "mappings":{
     9         "lvh":{
    10             "properties":{
    11                 "name":{
    12                     "type":"string"},
    13                 "age":{
    14                     "type":"long"}
    15             }
    16         }
    17     }
    18 }'
    1 curl -XPUT 192.168.100.15:9200/index/type/_mapping -d @mapping.json

    其中mapping.json的内容是以下形式:
     1 {
     2    "lvh":{
     3         "properties":{
     4              "name":{
     5                   "type":"string"},
     6               "age":{
     7                    "type":"long"}
     8            }
     9      }
    10 }

    以上都是手动添加索引映射,还有另一种方法:插入数据,根据数据自动创建索引和映射:

    1 #以后eshost都代替192.168.100.15:9200
    2 curl -XPUT eshost/index/type/id -d '{
    3         "name":"lvhuan",
    4         "age":"21"
    5 }'

    2. 添加文档(无则添加,有则覆盖) POST

    添加文档时,可以在现有type里添加,亦可以新建一个type来添加文档。唯一标识符(_id)可写可不写。

    这里需要注意的是,覆盖特效。新内容覆盖对应id所有的旧内容。

    1 curl -XPOST eshost/jiu3/lih/1 -d '{
    2     "name":"huan"}'

    批量添加(特别注意格式,尤其是每条记录之间的回车必不可少; 同样,id可以缺省)(另外,记录数可以等于一)

    POST /_bulk
    { "index": { "_index": "addr", "_type": "contact", "_id": 1 }}
    { "name": "Fyodor Dostoevsky", "country": "RU" }
    { "create": { "_index": "addr", "_type": "contact", "_id": 2 }}
    { "name": "Erich Maria Remarque", "country": "DE" }
    { "create": { "_index": "addr", "_type": "contact", "_id": 2 }}
    { "name": "Joseph Heller", "country": "US" }
    { "delete": { "_index": "addr", "_type": "contact", "_id": 4 }}
    { "delete": { "_index": "addr", "_type": "contact", "_id": 1 }}
    curl -XPOST 'localhost:9200/_bulk?pretty' --data-binary
    @documents.json

    其中,json文件内容就是之前那种记录排列的格式。

    3. 获取索引、文档 GET

    pretty是以JSON格式显示

    1 curl -XGET http://192.168.100.15:9200/jiu3?pretty
    1 curl -XGET http://192.168.100.15:9200/jiu3/lih/1?pretty

    4. 查询文档 GET

    对于字符串匹配,可模糊查询,即匹配字符串部分内容。

    查询所有信息

    1 curl -XGET http://192.168.100.15:9200/jiu3/lih/_search?pretty
    1 curl -XGET eshost/index/type/_search -d '{
    2     "query":{
    3         "match_all":{}
    4      }
    5 }'

    查询,匹配单字段

    1 curl -XGET eshost/index/type/_search?pretty&q=name:huan
    1 curl -XGET eshost/index/type/_search -d '{
    2   "query":{
    3       "match":{
    4           "age":21}
    5     }
    6 }'
    1 curl -XGET eshost/index/type/_search -d '{
    2   "query":{
    3       "query_string":{
    4           "query":"age:21"
    5        }
    6     }
    7 }'

    匹配多字段

     1 curl -XGET eshost/index/type/_search -d '{
     2   "query": {
     3     "bool": {
     4       "should": [
     5         { "match": { "title":  "War and Peace" }},
     6         { "match": { "author": "Leo Tolstoy"   }}
     7       ]
     8     }
     9   }
    10 }‘

    匹配单词条 这里的词条是指字段的单元,确切、未经分析的词条,即必须完全匹配。

    1 curl -XGET eshost/index/type/_search -d '{
    2    "query": {
    3      "term": {
    4        "tags":"novel"
    5       }
    6   }
    7 }‘

    匹配多词条

    1 curl -XGET eshost/index/type/_search -d '{
    2    "query": {
    3      "terms": {
    4        "tags":["novel","book"]
    5       }
    6   }
    7 }‘

    5.删除文档 DELETE

    1 curl -XDELETE eshost/index/type/id

    6.更新文档 POST

    这里只举两个我常用的写法,其他可参看http://www.cnblogs.com/jiu0821/p/5635610.html

    若下面第一个写法报错,解决方案在上面链接里可找到。

    与前面覆盖文档不同,这里只需写要更新的字段即可,其他字段不写。有则更新,无则添加。

    1 curl -XPOST eshost/index/type/id/_update -d '{
    2     "script":"ctx._source.name = "lv huan" "
    3 }'
    1 curl -XPOST eshost/index/type/id/_update -d '{
    2     "doc":{
    3        "name" = "lv huan"
    4     }
    5 }'
  • 相关阅读:
    BPM平台在企业业务系统中使用的价值讨论
    零售餐饮行业的信息化建设
    LINQ 与Oracle应用 :转帖
    k2之于.NET流程应用开发者
    利用xslt导出复杂样式的excel,支持多个worksheet
    利用偏移量快速定位数据内容
    简单天气项目中观察者模式解析
    作业3:基于墨刀的:视频剪辑软件原型设计
    必做作业2:视频剪辑软件调研
    .Net Core项目依赖项问题
  • 原文地址:https://www.cnblogs.com/jiu0821/p/5635417.html
Copyright © 2011-2022 走看看