微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ElasticSearch操作数据(Document)

一、添加数据

# /[index]/[type]/[id]
# 认type为_doc,id为no1
PUT 'http://localhost:9200/index1/_doc/no1'

{
	"user":"123",
	"title":"123",
	"desc":"123"
}

四、查询数据

GET 'http://localhost:9200/index1/_doc/no1'

# 结果
{
  "_index" : "accounts",
  "_type" : "person",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "user" : "123",
    "title" : "123",
    "desc" : "123"
  }
}

五、删除数据

DELETE 'http://localhost:9200/index1/_doc/no1'

六、更新数据

# 更新记录就是使用 PUT 请求,重新发送一次数据。
PUT 'http://localhost:9200/index1/_doc/no1'

{
	"user":"123",
	"title":"123",
	"desc":"123"
}

七、数据查询

7.1 查询所有记录

# 使用 GET 方法,直接请求/Index/Type/_search,就会返回所有记录。
GET 'http://localhost:9200/index1/_doc/_search'

{
  "took":2,
  "timed_out":false,
  "_shards":{"total":5,"successful":5,"Failed":0},
  "hits":{
    "total":2,
    "max_score":1.0,
    "hits":[
      {
        "_index":"accounts",
        "_type":"person",
        "_id":"AV3qGfrC6jMbsbXb6k1p",
        "_score":1.0,
        "_source": {
          "user": "李四",
          "title": "工程师",
          "desc": "系统管理"
        }
      },
      {
        "_index":"accounts",
        "_type":"person",
        "_id":"1",
        "_score":1.0,
        "_source": {
          "user" : "123",
          "title" : "123",
          "desc" : "123"
        }
      }
    ]
  }
}

7.2 全文查询

# 使用 Match 查询,指定的匹配条件是desc字段里面包含"123"这个词
GET 'http://localhost:9200/index1/_doc/_search'

{
  "query" : { "match" : { "desc" : "123" }}
}

# 结果
{
  "took":3,
  "timed_out":false,
  "_shards":{"total":5,"successful":5,"Failed":0},
  "hits":{
    "total":1,
    "max_score":0.28582606,
    "hits":[
      {
        "_index":"accounts",
        "_type":"person",
        "_id":"1",
        "_score":0.28582606,
        "_source": {
          "user" : "123",
          "title" : "123",
          "desc" : "123"
        }
      }
    ]
  }
}
# Elastic 认一次返回10条结果,可以通过size字段改变这个设置
# 下面代码指定,每次只返回一条结果
GET 'http://localhost:9200/index1/_doc/_search'
{
  "query" : { "match" : { "desc" : "123" }},
  "size": 1
}'
# 还可以通过from字段,指定位移
GET 'http://localhost:9200/index1/_doc/_search'
{
  "query" : { "match" : { "desc" : "管理" }},
  "from": 1,
  "size": 1
}

7.3 逻辑运算

# 如果有多个搜索关键字, Elastic 认为它们是or关系
# 下面面代码搜索的是软件 or 系统
GET 'http://localhost:9200/index1/_doc/_search'
{
  "query" : { "match" : { "desc" : "1 3" }}
}
# 如果要执行多个关键词的and搜索,必须使用布尔查询
GET 'http://localhost:9200/index1/_doc/_search'
{
  "query": {
    "bool": {
      "must": [
        { "match": { "desc": "1" } },
        { "match": { "desc": "3" } }
      ]
    }
  }
}'

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐