码迷,mamicode.com
首页 > 其他好文 > 详细

ElasticSearch入门

时间:2020-04-29 16:41:27      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:its   localhost   引擎   ati   local   更新记录   位置   发送   文档   

ElasticSearch

技术图片

全文搜索属于最常见的需求,开源的 Elasticsearch (以下简称 Elastic)是目前全文搜索引擎的首选。

它可以快速地储存、搜索和分析海量数据。维基百科、Stack Overflow、Github 都采用它。

概念

Elastic会索引所有字段,查询就是查索引。

1,索引(Index)是Elastic数据管理的顶层单位,一个索引就是一个数据库。索引名称必须小写。

2,Index 里的记录称为文档(Document),多条Document组成一个Index,Document是Json格式。

3,Document文档可以分组(Type),是虚拟的逻辑分组,用来过滤Document

技术图片

实操

0,

显示所有Index:curl -X GET ‘http://localhost:9200/_cat/indices?v‘

显示所有Type:curl ‘localhost:9200/_mapping?pretty=true‘

1,

新建Index,向Elastic服务器发送PUT请求,名为weather:curl -X PUT ‘localhost:9200/weather’

删除Index,发送DELETE请求:curl -X DELETE ‘localhost:9200/weather’

2,

新增记录,向指定Index/Type发请求:

# 向accounts索引Type组中新增记录

# 方式一:发送PUT请求,要指定该条记录的ID,下例中为1,ID不一定是数字,任何字符串都可以。
curl -X PUT ‘localhost:9200/accounts/person/1‘ -H ‘Content-Type: application/json‘ -d ‘
{
  "user": "张三",
  "title": "工程师",
  "desc": "数据库管理"
}‘ 

# 方式二:发送POST请求,ID会随机创建,和方式一等价。
curl -X POST ‘localhost:9200/accounts/person‘ -H ‘Content-Type: application/json‘ -d ‘
{
  "user": "张三",
  "title": "工程师",
  "desc": "数据库管理"
}‘

删除记录,发送DELETE请求,删除accounts索引person组中ID等于1的记录:curl -X DELETE ‘localhost:9200/accounts/person/1‘

更新记录,发送PUT请求,更新ID等于1的记录:

curl -X PUT ‘localhost:9200/accounts/person/1‘ -H ‘Content-Type: application/json‘ -d ‘
{
    "user" : "张三",
    "title" : "工程师",
    "desc" : "数据库管理,软件开发"
}‘ 
# 更新返回的数据中,该记录version从1变为2,记录更新次数

查看记录,发送GET请求,accounts索引person分组ID为1的记录:

curl ‘localhost:9200/accounts/person/1?pretty=true‘
# pretty=true以易读的格式返回。ID不存在就查不到数据。

3,

数据查询

查询所有记录,GET请求:

curl ‘localhost:9200/accounts/person/_search‘

# response中:took表示耗时(毫秒)、time_out表示是否超时、hits表示命中的记录

全文搜索

# 查询accounts索引person组的记录,条件是desc字段包含”软件“二字
curl ‘localhost:9200/accounts/person/_search‘ -H ‘Content-Type: application/json‘ -d ‘
{
  "query" : { "match" : { "desc" : "软件" }}
}‘

# Elastic默认返回10条记录,size字段可设置返回记录条数,from字段表示位移,从哪个位置开始,默认从0开始
#{
#  "query" : { "match" : { "desc" : "软件" }},
#  "from": 1,
#  "size": 5
#}

逻辑运算

有多个条件,默认or

# 软件 or 系统
curl ‘localhost:9200/accounts/person/_search‘ -H ‘Content-Type: application/json‘ -d ‘
{
  "query" : { "match" : { "desc" : "软件 系统" }}
}‘

要执行and,必须使用布尔查询

# 软件 and 系统
curl ‘localhost:9200/accounts/person/_search‘ -H ‘Content-Type: application/json‘ -d ‘
{
  "query": {
    "bool": {
      "must": [
        { "match": { "desc": "软件" } },
        { "match": { "desc": "系统" } }
      ]
    }
  }
}‘

ElasticSearch入门

标签:its   localhost   引擎   ati   local   更新记录   位置   发送   文档   

原文地址:https://www.cnblogs.com/ldy-miss/p/12802873.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!