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

ElasticSearch基础

时间:2018-12-11 19:57:07      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:类型   高亮   smi   elastic   toc   https   let   last   tar   

一:一个 Elasticsearch 请求和任何 HTTP 请求一样由若干相同的部件组成

curl -X<VERB> <PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING> -d <BODY>

被 < > 标记的部件:

VERB 适当的 HTTP 方法 或 谓词 : GET`、 `POST`、 `PUT`、 `HEAD 或者 `DELETE`。
PROTOCOL http 或者 https`(如果你在 Elasticsearch 前面有一个 `https 代理)
HOST Elasticsearch 集群中任意节点的主机名,或者用 localhost 代表本地机器上的节点。
PORT 运行 Elasticsearch HTTP 服务的端口号,默认是 9200 。
PATH API 的终端路径(例如 _count 将返回集群中文档数量)。Path 可能包含多个组件,例如:_cluster/stats 和 _nodes/stats/jvm 。
QUERY_STRING 任意可选的查询字符串参数 (例如 ?pretty 将格式化地输出 JSON 返回值,使其更容易阅读)
BODY 一个 JSON 格式的请求体 (如果请求需要的话)
 
例如,计算集群中文档的数量:
curl -XGET http://localhost:9200/_count?pretty -d  { "query": { "match_all": {} } } 

缩写格式显示:

GET /_count { "query": { "match_all": {} } }

 

二:ES数据架构的主要概念(与关系数据库Mysql对比)

技术分享图片

 

三:基础的常用命令

 创建雇员1:megacorp为索引名称(相当于database),employee为类型名称(相当于table)

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

搜索雇员1的详细信息:

GET /megacorp/employee/1

获取所有雇员:

GET /megacorp/employee/_search

获取姓氏为Smith的雇员:

GET /megacorp/employee/_search?q=last_name:Smith

用查询表达式,获取姓氏为Smith的雇员:

GET /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    }
}
搜索姓氏为 Smith 的雇员,但这次我们只需要年龄大于 30 的:
GET /megacorp/employee/_search
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "last_name" : "smith" 
                }
            },
            "filter": {
                "range" : {
                    "age" : { "gt" : 30 }
                }
            }
        }
    }
}
搜索下所有喜欢攀岩(rock climbing)的雇员:
这样会有一个相关性得分。如有雇员爱好中只有rock或climbing也会被检索到。并且分支明显低于rock climbing
GET /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "about" : "rock climbing"
        }
    }
}
短语搜索,不会返回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" : {}
        }
    }
}

聚合搜索:

GET /megacorp/employee/_search
{
  "aggs": {
    "all_interests": {
      "terms": { "field": "interests" }
    }
  }
}

 

参考文献:ElasticSearch权威指南

 

 

ElasticSearch基础

标签:类型   高亮   smi   elastic   toc   https   let   last   tar   

原文地址:https://www.cnblogs.com/parent-absent-son/p/10104230.html

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