文章目录
- 1,请求命令格式
- 2,关闭服务
- 3,添加员工信息
- 4,检索单个员工信息
- 5,检索所有员工信息
- 6,简单查询查询last_name为Smith的员工信息:全匹配
- 7,DSL语句查询 查询last_name为Smith的员工信息:全匹配
- 8,查询last_name为Smith, 并且年龄大于30岁的员工信息
- 9,全文搜索, 搜索喜欢rock climbing的员工信息
- 10,短语搜索,查询同时包含"rock"和"climbing"(并且是相邻的)的员工记录
- 11,高亮显示显示搜索结果中关键字
- 12,聚合查询
- 13,集群状态说明:
- 14,分片副本确定
- 15,创建索引
- 16,增加副本数量
- 17,检查文档是否存在
- 18,更新, 创建一个新文档,_create参数表示已存在就不创建
- 19,删除文档
- 20,版本控制
- 21,局部更新操作
- 22,mget批量检索
- 23,mget批量检索 在相同的index和type下搜索
- 24,bulk 批量添加
- 25,查看某个类型的mapping
- 26,指定分词器
- 27,指定分词器, 测试中文分词器
- 28,空查询,查询所有数据
- 29,查询子句,match
- 30,合并多子句
- 31,term过滤
- 32,terms过滤
- 33,range过滤
- 34,exists和missing 过滤
- 35,bool 过滤
- 36,单条过滤语句
- 37,重新索引数据
- 38,索引别名
- 39,创建索引
- 40,设置索引别名
- 41,refeash API : 刷新索引
- 42,全文检索,match匹配查询
- 43,match多词查找
- 44,组合查询
- 45multi_match跨字段对象查询
1,请求命令格式
向Elasticsearch发出的请求的组成部分与其它普通的HTTP请求是一样的:
curl -H "Content-Type: application/json" -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 -H "Content-Type: application/json" -XGET 'http://localhost:9200/_count?pretty' -d '
{
"query": {
"match_all": {}
}
}'
2,关闭服务
curl -XPOST 'http://localhost:9200/_shutdown'
3,添加员工信息
PUT /{index}/{type}/{id}
curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/megacorp/employee/3' -d '
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}'
4,检索单个员工信息
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/1?pretty'
5,检索所有员工信息
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search?pretty'
6,简单查询查询last_name为Smith的员工信息:全匹配
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search?q=last_name:Smith&_source=first_name'
7,DSL语句查询 查询last_name为Smith的员工信息:全匹配
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search' -d '{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}'
8,查询last_name为Smith, 并且年龄大于30岁的员工信息
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search' -d '{
"query" : {
"bool" : {
"filter" : {
"range" : {
"age" : { "gt" : 30 }
}
},
"must" : {
"match" : {
"last_name" : "Smith"
}
}
}
}
}'
9,全文搜索, 搜索喜欢rock climbing的员工信息
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search' -d '{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}'
10,短语搜索,查询同时包含"rock"和"climbing"(并且是相邻的)的员工记录
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search' -d '{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}'
11,高亮显示显示搜索结果中关键字
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search' -d '{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}'
12,聚合查询
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search' -d '{
"aggs": {
"all_interests": {
"terms": { "field": "interests" }
}
}
}'
13,集群状态说明:
green 所有主要分片和复制分片都可用
yellow 所有主要分片可用,但不是所有复制分片都可用
red 不是所有的主要分片都可用
14,分片副本确定
分片数量根据业务实际确定
副本数量根据集群节点数量确定
15,创建索引
curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/blogs' -d '{
"settings" : {
"number_of_shards" : 3,
"number_of_replicas" : 1
}
}'
16,增加副本数量
curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/blogs/_settings' -d '{
"number_of_replicas" : 4
}'
17,检查文档是否存在
curl -i -H "Content-Type: application/json" -XHEAD http://localhost:9200/megacorp/employee/123
18,更新, 创建一个新文档,_create参数表示已存在就不创建
curl -i -H "Content-Type: application/json" -XPUT 'http://localhost:9200/megacorp/employee/3/_create?pretty' -d '
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}'
19,删除文档
curl -H "Content-Type: application/json" -XDELETE 'http://localhost:9200/megacorp/employee/1?pretty'
20,版本控制
curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/website/blog/2/_create?pretty' -d '{
"title": "safasdfadsfasdf",
"text": "ddddddddddddddddddddddddddd"
}'
curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/website/blog/2?version=1' -d '{ -- 更新,表示老的version为1的时候才生效
"title": "cccccccccccccccccccccc",
"text": "ccccccccccccccccccccc"
}'
21,局部更新操作
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/megacorp/employee/1/_update' -d '{
"doc" : {
"first_name" : "weiyuan",
"views": 0
}
}'
22,mget批量检索
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/_mget?pretty' -d '{
"docs" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : 2
},
{
"_index" : "website",
"_type" : "blog",
"_id" : 1,
"_source": "views"
}
]
}'
23,mget批量检索 在相同的index和type下搜索
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/megacorp/employee/_mget?pretty' -d '{
"ids" : [ "2", "1","3","234" ]
}'
24,bulk 批量添加
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/_bulk/?pretty' -d '{
{ "delete": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "create": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "title": "My first blog post" }
{ "index": { "_index": "website", "_type": "blog" }}
{ "title": "My second blog post" }
{ "update": { "_index": "website", "_type": "blog", "_id": "123", "_retry_on_conflict" : 3} }
{ "doc" : {"title" : "My updated blog post"} }
}'
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/_bulk/?pretty' -d '{
{ "delete":{ "_index":"website", "_type": "blog", "properties" : {"_id": "123" }}}
}'
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/_bulk/?pretty' -d '{
{"delete":{"_index":"test_index","_type":"test_type","_id":"2"}}
{"create":{"_index":"test_index","_type":"test_type","_id":"3"}}
{"test_field":"test3"}
{"create":{"_index":"test_index","_type":"test_type","_id":"2"}}
{"test_field":"test2"}
{"index":{"_index":"test_index","_type":"test_type","_id":"4"}}
{"test_field":"test4"}
{"index":{"_index":"test_index","_type":"test_type","_id":"1"}}
{"test_field":"replaced test1111","test_field2":"test_field2"}
{ "update": { "_index": "test_index", "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} }
{ "doc" : {"test_field2" : "bulk test1"} }
}'
POST /_bulk
'{"delete":{"_index":"test_index","_type":"test_type","_id":"2"}}
{"create":{"_index":"test_index","_type":"test_type","_id":"3"}}
{"test_field":"test3"}
{"create":{"_index":"test_index","_type":"test_type","_id":"2"}}
{"test_field":"test2"}
{"index":{"_index":"test_index","_type":"test_type","_id":"4"}}
{"test_field":"test4"}
{"index":{"_index":"test_index","_type":"test_type","_id":"1"}}
{"test_field":"replaced test1111","test_field2":"test_field2"}
{ "update": { "_index": "test_index", "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} }
{ "doc" : {"test_field2" : "bulk test1"} }'
路由:确定操作在哪个分片上的算法
shard = hash(routing) % number_of_primary_shards
25,查看某个类型的mapping
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/_mapping/employee/?pretty'
26,指定分词器
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/_analyze?pretty=true' -d '{
"analyzer": "standard", "text": "Text to analyze"
}'
27,指定分词器, 测试中文分词器
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/_analyze/?pretty' -d '{
"analyzer":"ik_max_word","text":"中华人民共和国"
}'
28,空查询,查询所有数据
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/_search/?pretty' -d '{
"query": {
"match_all": {}
}
}'
29,查询子句,match
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/_search/?pretty' -d '{
"query": {
"match" : {
"last_name" : "Smith"
}
}
}'
30,合并多子句
{
"bool": {
"must": { "match": { "last_name": "Smith" }},
"must_not": { "match": { "name": "mary" }},
"should": { "match": { "tweet": "full text" }}
}
}
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/_search/?pretty' -d '{
"query" : {
"bool": {
"must": { "match": { "last_name": "Smith" }},
"must_not": { "match": { "name": "mary" }},
"should": { "match": { "tweet": "full text" }}
}
}
}'
31,term过滤
主要用于精确匹配哪些值,比如日期,数字,布尔值,或not_analyzed的字符串
{ “term”: { “age”: 26 }}
{ “term”: { “date”: “2014-09-01” }}
{ “term”: { “public”: true }}
{ “term”: { “tag”: “full_text” }}
32,terms过滤
terms 跟 term 有点类似,但 terms 允许指定多个匹配条件。 如果某个字段指定了多个值,那么文档需要一起去做匹配
{
"terms": {
"last_name": [ "sam", "tom", "smith" ]
}
}
GET /my_store/products/_search
{
"query" : {
"filtered" : {
"filter" : {
"term" : {
"productID" : "XHDK-A-1293-#fJ3"
}
}
}
}
}
33,range过滤
range过滤允许我们按照指定范围查找一批数据
{
“range”: {
“age”: {
“gte”: 20,
“lt”: 30
}
}
}
GET /my_store/products/_search
{
"query" : {
"filtered" : {
"filter" : {
"range" : {
"price" : {
"gte" : 20,
"lt" : 40
}
}
}
}
}
}
"range" : {
"timestamp" : {
"gt" : "2014-01-01 00:00:00",
"lt" : "2014-01-07 00:00:00"
}
}
34,exists和missing 过滤
exists 和 missing 过滤可以用于查找文档中是否包含指定字段或没有某个字段,类似于SQL语句中的IS_NULL条件
{
“exists”: {
“field”: “title”
}
}
35,bool 过滤
bool 过滤可以用来合并多个过滤条件查询结果的布尔逻辑,它包含一下操作符:
must :: 多个查询条件的完全匹配,相当于 and。
must_not :: 多个查询条件的相反匹配,相当于 not。
should :: 至少有一个查询条件匹配, 相当于 or。
{
“bool”: {
“must”: { “term”: { “folder”: “inbox” }},
“must_not”: { “term”: { “tag”: “spam” }},
“should”: [
{ “term”: { “starred”: true }},
{ “term”: { “unread”: true }}
]
}
}
match_all查询: 使用match_all 可以查询到所有文档,是没有查询条件下的默认语句。
{
“match_all”: {}
}
36,单条过滤语句
GET /_search
{
"query": {
"filtered": {
"filter": { "term": { "folder": "inbox" }}
}
}
}
查询语句中的过滤
GET /_search
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": { "term": { "folder": "inbox" }},
"must_not": {
"query": {
"match": { "email": "urgent business proposal" }
}
}
}
}
}
}
}
37,重新索引数据
GET /old_index/_search?search_type=scan&scroll=1m
{
"query": {
"range": {
"date": {
"gte": "2014-01-01",
"lt": "2014-02-01"
}
}
},
"size": 1000
}
38,索引别名
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/_search/?pretty'
39,创建索引
curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/my_index_v1/?pretty'
40,设置索引别名
curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/my_index_v1/_alias/my_index/?pretty'
41,refeash API : 刷新索引
POST /_refresh <1>
POST /blogs/_refresh <2>
<1> refresh所有索引
<2> 只refresh 索引blogs
flush API : 进行一次数据持久到硬盘,并删除事务日志的操作 (分片默认每30分钟一次)
flush API可用来进行一次手动flush:
POST /blogs/_flush <1>
POST /_flush?wait_for_ongoing <2>
<1> flush索引blogs
<2> flush所有索引,等待操作结束再返回
你很少需要手动flush,通常自动的就够了。
当你要重启或关闭一个索引,flush该索引是很有用的。当ES尝试恢复或者重新打开一个索引时,
它必须重放所有事务日志中的操作,所以日志越小,恢复速度越快。
42,全文检索,match匹配查询
GET /my_index/my_type/_search
{
"query": {
"match": {
"title": "QUICK!"
}
}
}
43,match多词查找
GET /my_index/my_type/_search
{
"query": {
"match": {
"title": "BROWN DOG!"
}
}
}
GET /my_index/my_type/_search
{
"query": {
"match": {
"title": {
"query": "BROWN DOG!",
"operator": "and" -- 控制匹配程度,要求brown和dog都存在
}
}
}
}
GET /my_index/my_type/_search
{
"query": {
"match": {
"title": {
"query": "quick brown dog",
"minimum_should_match": "75%" --设置匹配精度百分比
}
}
}
}
44,组合查询
GET /my_index/my_type/_search
{
"query": {
"bool": {
"should": [
{ "match": { "title": "brown" }},
{ "match": { "title": "fox" }},
{ "match": { "title": "dog" }}
],
"minimum_should_match": 76% --设置匹配精度百分比
}
}
}
45multi_match跨字段对象查询
{
"query": {
"multi_match": {
"query": "Poland Street W1V",
"type": "most_fields",
"fields": [ "street", "city", "country", "postcode" ]
}
}
}