Elasticsearch 基本操作

Elasticsearch2.0之基本操作


http://blog.csdn.net/gloria__zhang/article/details/49647107

指定id存储

PUT /{index}/{type}/{id}
{
  "field": "value",
  ...
}
例:
PUT /megacorp/employee/1
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}
//megacorp->索引名
//employee->类型名
//1->id

使用自动生成id

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

}

返回所有字段

GET /megacorp/employee/1?pretty
结果:
{
  "_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" ]
  }
}
//pretty会美化输出的JSON结果
返回指定字段
GET /megacorp/employee/1?_search=first_name,age
结果:
{
  "_index" :   "megacorp",
  "_type" :    "employee",
  "_id" :      "1",
  "_version" : 1,
  "found" :    true,
  "_source" :  {
      "first_name" :  "John",
      "age" :         25
  }
}

只返回_source信息,不返回元数据

GET /megacorp/employee/1/_source
结果:
{
   "first_name" :  "John",
   "last_name" :   "Smith",
   "age" :         25,
   "about" :       "I love to go rock climbing",
   "interests":  [ "sports", "music" ]
}

GET /megacorp/employee/_search?q=last_name:Smith
结果:
{
   ...
   "hits": {
      "total":      1,
      "max_score":  0.30685282,
      "hits": [
         {
            ...
            "_source": {
               "first_name":  "John",
               "last_name":   "Smith",
               "age":         25,
               "about":       "I love to go rock climbing",
               "interests": [ "sports", "music" ]
            }
         }
      ]
   }
}

更新

完全更新

PUT /megacorp/employee/1
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        26,
    "about" :      "I love to go rock climbing",
    "interests": [ "art", "music" ]

}
//更新的实际操作过程其实是先从旧文档中检索对应的数据,然后进行修改,再删除旧文档,索引新文档

局部更新

POST /megacorp/employee/1/_update
{
   "doc" : {
      "tags" : [ "testing" ],
      "interests":  [ "sports" ],
      "member": 0
   }
}
更新后的文档:
{
  "_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" ],
      "tags" : [ "testing" ],
      "member": 0
  }
}

删除

DELETE /megacorp/employee/1

DSL语句查询

GET /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    }
}
//删除一个文档也不会立即从磁盘上移除,它只是被标记成已删除。Elasticsearch将会在你之后添加更多索引的时候才会在后台进行删除内容的清理

全文搜索

GET /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "about" : "rock climbing"
        }
    }
}

短语搜索

GET /megacorp/employee/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    }
}

高亮搜索

GET /megacorp/employee/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    },
    "highlight": {
        "fields" : {
            "about" : {}
        }
    }
}

URI搜索

$ curl -XGET 'http://localhost:9200/twitter/tweet/_search?q=user:kimchy'

等价于DSL查询

GET /twitter/tweet/_search
{
    "query" : {
            "match" : {
            "user" : "kimchy"
        }
    }
}

搜索参数说明:

名称描述(说明)
q待查询的字符串
df没有定义字段前缀时的默认前缀
analyzer分析器
lowercase_expanded_terms是否terms进行自动转换成小写形式,默认为true
analyze_eldcard是否分配通配符和前缀查询,默认是false
default_operator默认操作,AND或者OR,默认为OR
lenient如果设置为true,将会忽略由于格式化引起的问题(如向数据字段提供文本),默认为false
explain对于每一个hit
_source设置为false则会禁止返回_source字段内容,也可通过设置_source_include和_source_exclude检索部分文档
fields字段
sort排序方式
track_scores当排序的时候,设置为true,可以返回相关度得分
timeout默认没有超时时间
from从第几条结果开始返回,默认为0
size返回结果的总数量,默认为10
search_type搜索类型包括dfs_query_then_fetch, query_then_fetch, scan or count,默认为query_then_fetch

- query and fetch:参数是query_and_fetch,它在所有相关的分片上执行查询,返回结果。每个分片返回size个结果。因为每个分片返回size个结果,所以这个类型实际返回 size乘以分片个数的结果。
- query then fetch:参数是query_then_fetch,查询也依赖于所有分片,但是只返回足够的信息(不是文档内容)。基于这个结果进行分类和排名,之后才访问相关分片的实际文档内容。 这个类型返回结果的实际个数是size。这是默认的类型,你不必指定一个特定的search_type。
- dfs query and fetch:参数是dfs_query_and_fetch,和query_and_fetch相似。除了初始scatter的阶段,这个阶段为了更精确的得分,计算分布式项频率。
- dfs_query_then_fetch:参数是dfs_query_then_fetch,和query_then_fetch相似。除了初始scatter的阶段,这个阶段为了更精确的得分,计算分布式项频率。
- count:参数是count,返回满足查询条件的hits的数量。
- scan:参数是scan,scan查询类型禁用排序,允许通过大型结果集非常有效的滚动(scrolling)。

请求体搜索

$ curl -XGET 'http://localhost:9200/twitter/tweet/_search' -d '{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

参数

名称描述(说明)
timeout默认没有timeout
from从第几条结果开始返回,默认为0
size返回结果的总数量,默认为10
search_type搜索类型包括dfs_query_then_fetch, query_then_fetch, scan or count,默认为query_then_fetch
query_cache当?search_type=count时,查询结果是否缓存

查询

{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

from / size

用from和size进行结果分页

{
    "from" : 0, "size" : 10,
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

排序

{
    "sort" : [
        { "post_date" : {"order" : "asc"}},
        "user",
        { "name" : "desc" },
        { "age" : "desc" },
        "_score"//按照搜索打分进行排序
    ],
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

source过滤

控制结果返回的字段内容

  1. “_source” : false
  2. “_source” : “obj.*”
  3. “_source” : [ “obj1.” , “obj2.” ]
  4. “_source” : {
    “include” : [ “obj1.” , “obj2.” ],
    “exclude” : [ “*.description” ]
    }

参考:
https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
http://es.xiaoleilu.com/

Elasticsearch是一个开源的分布式搜索和分析引擎,它可以用于存储搜索和分析大量的数据。以下是一些Elasticsearch基本操作: 1. 安装和启动Elasticsearch:首先,你需要从Elasticsearch官方网站下载和安装Elasticsearch。安装完成后,你可以使用命令行或者图形界面来启动Elasticsearch。 2. 创建索引:在Elasticsearch中,数据存储在索引中。你可以使用PUT请求来创建一个新的索引。例如,使用curl命令可以发送以下请求来创建一个名为"my_index"的索引: ``` curl -XPUT 'localhost:9200/my_index' ``` 3. 添加文档:一旦索引创建好了,你可以使用POST请求来向索引中添加文档。文档是以JSON格式表示的数据。以下是向名为"my_index"的索引添加一个文档的示例请求: ``` curl -XPOST 'localhost:9200/my_index/_doc' -d ' { "title": "Elasticsearch Basics", "content": "This is a basic introduction to Elasticsearch" }' ``` 4. 搜索文档:你可以使用GET请求来搜索索引中的文档。以下是一个搜索名为"my_index"的索引中包含关键字"elasticsearch"的文档的示例请求: ``` curl -XGET 'localhost:9200/my_index/_search?q=elasticsearch' ``` 5. 更新文档:使用POST请求可以更新索引中的文档。以下是更新名为"my_index"的索引中ID为1的文档的示例请求: ``` curl -XPOST 'localhost:9200/my_index/_doc/1/_update' -d ' { "doc": { "content": "This is an updated content" } }' ``` 6. 删除文档:使用DELETE请求可以删除索引中的文档。以下是删除名为"my_index"的索引中ID为1的文档的示例请求: ``` curl -XDELETE 'localhost:9200/my_index/_doc/1' ``` 这些是Elasticsearch的一些基本操作。你可以根据需要进一步探索和学习更多高级功能和API。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值