1 Elasticsearch简介
目录
7.2.3 ctx._source.remove() 删除一个字段
7.2.4 ctx._source.tag.contains(params.tag)选择性删除文档
8.3 多搜索Multi-search API(_msearch)
1 docker 安装
1.1 docker 安装Elasticsearch
https://www.elastic.co/guide/en/elasticsearch/reference/5.0/docker.html#docker-cli-run-prod-mode
docker pull docker.elastic.co/elasticsearch/elasticsearch:5.0.2
docker run -itd --name es -p 9200:9200 -e "http.host=0.0.0.0" -e "transport.host=192.168.84.184" docker.elastic.co/elasticsearch/elasticsearch:5.0.2
curl -u elastic '192.168.187.108:9200/_cat/indices?v'
新增跨域配置:
cluster.name: "docker-cluster"
network.host: 0.0.0.0
http.cors.enabled: true
http.cors.allow-origin: "*"
# minimum_master_nodes need to be explicitly set when bound on a public IP
# set to 1 to allow single node clusters
# Details: https://github.com/elastic/elasticsearch/pull/17288
discovery.zen.minimum_master_nodes: 1
1.2 docker 安装kibana
docker pull kibana:5.0.2
docker run --name kibana -e ELASTICSEARCH_URL=http://192.168.84.184:9200 -p 5601:5601 -d 388661dcd03e
http://192.168.84.184:5601
2 在终端里访问 REST API 时报错
https://blog.csdn.net/muzizongheng/article/details/85257087
2.1 添加账户密码
https://blog.csdn.net/hellozhxy/article/details/86649667
在终端里访问 REST API 的时候
| |
会报错
| |
解决办法是添加上账号密码就好了
| |
Elasticsearch服务器开发(第2版):http://note.youdao.com/noteshare?id=2ae6f3e10570a0b6bd1e7f8c0faa6a06
这个账号密码实际上是 X-Pack 这个插件附带的认证功能。X-Pack 是一个 Elastic Stack 的扩展,将安全,警报,监视,报告和图形功能包含在一个易于安装的软件包中。
2.2 修改账户密码
(1) 去elasticsearch.yml或者kibana.yml 修改用户名密码
- elasticsearch.username: "elastic"
- elasticsearch.password: "xxxxxx"
(2)发现刚才的elastic账号登录还是失败, 在前面的elasticsearch.yml和kibana.yml中添加一行
xpack.security.enabled: false
(3)重启es和kibana, 等待个半小时。然后测试http://localhost:5601/(kibana的地址)是否正常, 用户名用elastic或者user,密码是自己设置的elastic的密码, user用户名是运行安装包时设置的。
(4)登录http://localhost:81/elk/app , 用elastic和user都测试是否能登录正常, 不行就重启es和kibana再等待一段时间。
3 查找elasticsearch.yml文件
方法一:
find / | grep config/elasticsearch.yml
方法二:采用进入容器的方法
docker exec -it es bash
注:进入容器:docker exec -it 容器名或容器ID 执行命令(一般为bash或sh)
4 自动创建名为library的索引,并添加信息
ES支持一种JSON格式的查询,叫做DSL,domain specific language。
例一:
curl --user elastic:changeme -XPUT 'http://192.168.84.184:9200/library/book/1?pretty' -d '{
"author":"ravi",
"title":"test-driven",
"page":"240"
}'
例二:
curl --user elastic:changeme -XPUT 'http://192.168.84.184:9200/library/book/1?op_type=create&pretty' -d '{
"author":"ravi",
"title":"test-driven",
"page":"240"
}'
例三:
curl --user elastic:changeme -XPUT 'http://192.168.84.184:9200/library/book/1/_create?pretty' -d '{
"author":"ravi",
"title":"test-driven",
"page":"240"
}'
例四:
curl --user elastic:changeme -XPOST 'http://192.168.84.184:9200/library/book?pretty' -d '{
"author":"ravi",
"title":"test-driven",
"page":"240"
}'
https://www.cnblogs.com/xing901022/p/4999364.html
4.1 自动创建索引
如果上面执行操作前,ES中没有library这个索引,那么默认会直接创建这个索引;并且type字段也会自动创建。也就是说,ES并不需要像传统的数据库事先定义表的结构。
每个索引中的类型都有一个mapping映射,这个映射是动态生成的,因此当增加新的字段时,会自动增加mapping的设置。
通过在配置文件中设置action.auto_create_index为false,可以关闭自动创建index这个功能。
自动创建索引功能,也可以设置黑名单或者白名单,比如:
设置action.auto_create_index为 +aaa*,-bbb*,'+'号意味着允许创建aaa开头的索引,'-'号意味着不允许创建bbb开头的索引。
4.2 pretty
命令中URL的结尾使用了pretty参数,来输出缩进格式良好的JSON结果(如果有的话)。
4.3 操作类型op_type
ES通过参数op_type提供“缺少即加入”的功能,即如果ES中没有该文档,就进行索引;如果有了,则报错返回。
如果已经存在id为1的文档,则会报错,直接使用_create API,效果一样,如例二和例三。
默认情况下,如果没有任何具体说明(如例一),这个操作将设置为create。
4.4 自动创建ID
按照例一、例二和例三来说,ES会把我们指定的文档id做为ID。如果不指定ID,那么就会随机分配一个,例如例四,返回:
5 获取文档get API
5.1 单文档API
5.1.1 展示实际的文档内容
curl -XGET 'http://192.168.84.184:9200/library/book/1?pretty'
其中,搜索结果中表头的source展示了实际的文档内容。
5.1.2 不需要source字段中的内容
curl -XGET 'http://192.168.84.184:9200/library/book/1?_source=false&pretty'
5.1.3 不想要source 中page字段的内容
curl -XGET 'http://192.168.84.184:9200/library/book/1?_source_exclude=page&pretty'
5.1.4 只需要source 中author字段的内容
curl -XGET 'http://192.168.84.184:9200/library/book/1?_source_include=author&pretty'
或者 :
curl -XGET 'http://192.168.84.184:9200/library/book/1?_source=author&pretty'
5.1.4 只需要source字段的数据
curl -XGET 'http://192.168.84.184:9200/library/book/1/_source?pretty'
或者:
curl -XGET 'http://192.168.84.184:9200/library/book/1/_source'
5.1.5 stored_fields
使用stored_fields参数获得存储的字段,存储的字段应该在创建是有:"store": true 设置
curl -XGET 'http://192.168.84.184:9200/library/book/1?stored_fields=author,page&pretty'
5.2 多文档API
5.2.1 获取一个类型的多个文档
#1
curl -XGET 'localhost:9200/_mget?pretty' -H 'Content-Type: application/json' -d'
{
"docs" : [
{
"_index" : "test",
"_type" : "type",
"_id" : "1"
},
{
"_index" : "test",
"_type" : "type",
"_id" : "2"
}
]
}'
#2
curl -XGET 'localhost:9200/test/_mget?pretty' -H 'Content-Type: application/json' -d'
{
"docs" : [
{
"_type" : "type",
"_id" : "1"
},
{
"_type" : "type",
"_id" : "2"
}
]
}'
#3
curl -XGET 'localhost:9200/test/type/_mget?pretty' -H 'Content-Type: application/json' -d'
{
"docs" : [
{
"_id" : "1"
},
{
"_id" : "2"
}
]
}'
#4
curl -XGET 'localhost:9200/test/type/_mget?pretty' -H 'Content-Type: application/json' -d'
{
"ids" : ["1", "2"]
}'
如果不想指定类型的字段,则要么将类型的位置留空,要么使用_all来代替。在这种情况下,程序会返回左右类型的文档中第一个匹配到指定ids的文档数据。如下:
#5
curl -XGET 'localhost:9200/test/_mget?pretty' -H 'Content-Type: application/json' -d'
{
"ids" : ["1", "2"]
}'
#6
curl -XGET 'localhost:9200/test/_all/_mget?pretty' -H 'Content-Type: application/json' -d'
{
"ids" : ["1", "2"]
}'
5.2.2 明确指定不同的类型
curl -XGET 'localhost:9200/test/_mget?pretty' -H 'Content-Type: application/json' -d'
{
"docs" : [
{
"_type":"typeA",
"_id" : "1"
},
{
"_type":"typeB",
"_id" : "1"
}
]
}'
5.2.3 过滤_source
curl -XGET 'localhost:9200/_mget?pretty' -H 'Content-Type: application/json' -d'
{
"docs" : [
{
"_index" : "test",
"_type" : "type",
"_id" : "1",
"_source" : false
},
{
"_index" : "test",
"_type" : "type",
"_id" : "2",
"_source" : ["field3", "field4"]
},
{
"_index" : "test",
"_type" : "type",
"_id" : "3",
"_source" : {
"include": ["user"],
"exclude": ["user.location"]
}
}
]
}'
5.2.4 指定stored_field字段返回
指定stored_field字段返回。 1 返回 field1,field2,2返回field3,field4。
curl -XGET 'localhost:9200/test/type/_mget?stored_fields=field1,field2&pretty' -H 'Content-Type: application/json' -d'
{
"docs" : [
{
"_id" : "1"
},
{
"_id" : "2",
"stored_fields" : ["field3", "field4"]
}
]
}'
5.2.5 指定routing
curl -XGET 'localhost:9200/_mget?routing=key1&pretty' -H 'Content-Type: application/json' -d'
{
"docs" : [
{
"_index" : "test",
"_type" : "type",
"_id" : "1",
"routing" : "key2"
},
{
"_index" : "test",
"_type" : "type",
"_id" : "2"
}
]
}'
5.3 不指定id获取文档
Elasticsearch 的所有查询都发送到_search 端点,可以搜索单个或多个索引,也可以将搜索范围缩小到给定的一个或多个文档。
5.3.1 寻找library索引
curl --user elastic:changeme -XGET '192.168.84.184:9200/library/_search?pretty'
took:
Elasticsearch执行搜索的时间(以毫秒为单位)timed_out:
告诉我们搜索是否超时_shards:
告诉我们搜索了多少碎片,以及搜索碎片成功/失败的次数hits:
搜索结果hits.total:
符合我们搜索条件的文件总数hits.hits:
实际的搜索结果数组(默认为前10个文档)hits.sort:
对结果进行排序键(按分数排序时丢失)
5.3.2 如果还有一个索引叫clients
curl --user elastic:changeme -XGET '192.168.84.184:9200/library,clients/_search?pretty'
5.3.3 只在library索引的book类型中搜索
curl --user elastic:changeme -XGET '192.168.84.184:9200/library/book/_search?pretty'
5.3.4 要寻找任意索引,用星号(*)为索引名称
curl --user elastic:changeme -XGET '192.168.84.184:9200/*/_search?pretty'
5.3.4 搜索集群中的所有数据
curl --user elastic:changeme -XGET '192.168.84.184:9200/_search?pretty'
5.4 检查文档是否存在(XHEAD )
curl -XHEAD -i 'http://192.168.84.184:9200/library/book/1'
或者:
curl --head 'http://192.168.84.184:9200/library/book/1'
5.5 Bulk API
批量操作在写法上需要注意的事项:换行\n结尾; 回车\r ;开始 Content-Type
头部应该被设置为application/x-ndjson;
支持的参数:version_type;routing;wait_for_active_shards;refresh;
update操作支持的动作参数:retry_on_conflict; doc; doc_as_upsert; script; lang; source.
#批量操作数据结构
action_and_meta_data \ n
optional_source \ n
action_and_meta_data \ n
optional_source \ n
....
action_and_meta_data \ n
optional_source \ n
curl -XPOST 'localhost:9200/_bulk?pretty' -H 'Content-Type: application/json' -d'
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }'
curl -XPOST 'localhost:9200/_bulk?pretty' -H 'Content-Type: application/json' -d'
{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "index1", "retry_on_conflict" : 3} }
{ "doc" : {"field" : "value"} }
{ "update" : { "_id" : "0", "_type" : "type1", "_index" : "index1", "retry_on_conflict" : 3} }
{ "script" : { "source": "ctx._source.counter += params.param1", "lang" : "painless", "params" : {"param1" : 1}}, "upsert" : {"counter" : 1}}
{ "update" : {"_id" : "2", "_type" : "type1", "_index" : "index1", "retry_on_conflict" : 3} }
{ "doc" : {"field" : "value"}, "doc_as_upsert" : true }
{ "update" : {"_id" : "3", "_type" : "type1", "_index" : "index1", "_source" : true} }
{ "doc" : {"field" : "value"} }
{ "update" : {"_id" : "4", "_type" : "type1", "_index" : "index1"} }
{ "doc" : {"field" : "value"}, "_source": true}'
5.6 _termvectors
#示例
curl -XGET 'localhost:9200/twitter/tweet/1/_termvectors?pretty'
#可以指定要检索的字段
curl -XGET 'localhost:9200/twitter/tweet/1/_termvectors?fields=message&pretty'
返回字段包含:term infomation, term statistics, field statistics。
默认返回:term infomation 和 field statistics。
- term infomation: 1.字段的term频率;2.term 位置;3.开始和结束偏移量;4.term 有效负载 即使没保存也可以计算。
- term statistics : 1.术语在总文档中出现的频次 2.包含当前术语的总文档数
- field statistics : 1.包含该字段的文档数doc_count 2.该字段中所有term的文档总频次 sum_doc_freq 3.该term中每个term的总频次 sum_ttf
5.7 _mtermvectors
multi term vectors 一次性取回多个termvectors
curl -XPOST 'localhost:9200/_mtermvectors?pretty' -H 'Content-Type: application/json' -d'
{
"docs": [
{
"_index": "twitter",
"_type": "tweet",
"_id": "2",
"term_statistics": true
},
{
"_index": "twitter",
"_type": "tweet",
"_id": "1",
"fields": [
"message"
]
}
]
}'
参考:https://www.cnblogs.com/zhxdxf/p/8383983.html
6 删除文档Delete API
curl -XDELETE 'http://192.168.84.184:9200/library/book/1?pretty'
每一个写操作,包括删除操作,数据中version字段的值都会加1。
7 更新文档Update API
7.1 直接修改文档
7.1.1 部分更新文档
- http方法: post方法
- 关键词: _update, doc
curl -XPOST 'http://192.168.84.184:9200/library/book/2/_update?pretty' -d
'{
"doc": {
"page":100
}
}'
“doc”为关键字, 要修改的文档放在doc中, 实例修改了type为library索引下book中id为2 的page属性。
7.1.2 检测noop更新
如果指定doc,则其值与现有的_source合并。
默认情况下,不更改任何内容的更新会检测到它们不会更改任何内容,并返回“结果”:“noop”。
如果设置:"detect_noop": false,则在更新文档时,即使source没有变化,也可以进行更新。
curl -XPOST 'http://192.168.84.184:9200/library/book/2/_update?pretty' -d
'{
"doc": {
"page":100
},
"detect_noop": false
}'
7.2 脚本修改文档
通过脚本修改的api格式与直接修改的是一致的。
http方法: post方法
关键字”script”: 标志以脚本的方式修改文档;
“lang”: 表示以何种脚本语言进行修改, “painless”表示以es内置的脚本语言进行修改. 此外es还支持多种脚本语言, 如Python, js等等;
“inline”:指定脚本内容 ;
“ctx”代表es上下文, _source 代表文档。ctx映像包括_index、_type、_id、_version、_routing、_parent、_timestamp、_ttl和_source等变量。
7.2.1 方法一
curl -XPOST 'http://192.168.84.184:9200/library/book/2/_update?pretty' -d
'{
"script": {
"lang": "painless",
"inline": "ctx._source.page+= 30"
}
}'
7.2.2 方法二(将参数放到外面)
curl -XPOST 'http://192.168.84.184:9200/library/book/2/_update?pretty' -d
'{
"script": {
"lang": "painless",
"inline": "ctx._source.name = params.name; ctx._source.age = params.age",
"params": {
"age": 30,
"name": "谁是瓦力"
}
}
}'
7.2.3 ctx._source.remove() 删除一个字段
curl -XPOST 'http://192.168.84.184:9200/library/book/2/_update?pretty' -d
' {
"script" : "ctx._source.remove("category")",
"lang":"painless"
}'
7.2.4 ctx._source.tag.contains(params.tag)选择性删除文档
curl -XPOST 'http://192.168.84.184:9200/library/book/2/_update?pretty' -d
' {
"script":{
"inline" : " if(ctx._source.tag.contains(params.tag)) {ctx.op=\"delete\"} else{ ctx.op=\"none\"}",
"lang":"painless",
"params":{
"tag":"red"
}
}
}'
检查类别tags的字段中是否包含一个叫做"red"的文档,,如果是删除该文档,ctx.op="delete"为删除;否则,ctx.opt="none"什么也不做。
7.2.5 upsert
如果文档尚不存在,则会将upsert元素的内容作为新文档插入。 如果文档确实存在,那么脚本将被执行:
curl -XPOST 'http://192.168.84.184:9200/test/type1/1/_update?pretty' -d
'{
"script" : {
"source": "ctx._source.counter += params.count",
"lang": "painless",
"params" : {
"count" : 4
}
},
"upsert" : {
"counter" : 1
}
}'
如果id=2的数据不存在,则会新建一个文档,如下:
{
"_index": "test",
"_type": "type1",
"_id": "2",
"_version": 1,
"found": true,
"_source": {
"counter": 1
}
}
将doc_as_upsert
设置为true
,将不会发送部分文档和upsert文档,而是将doc的内容用作upsert值:
curl -XPOST 'http://192.168.84.184:9200/test/type1/1/_update?pretty' -d
{
"doc" : {
"name" : "new_name"
},
"doc_as_upsert" : true
}
7.2.6 scripted_upsert
curl -XPOST 'http://192.168.84.184:9200/test/type1/1/_update?pretty' -d
'{
"scripted_upsert":true,
"script" : {
"id": "my_web_session_summariser",
"params" : {
"pageViewEvent" : {
"url":"foo.com/bar",
"response":404,
"time":"2014-01-01 12:32"
}
}
},
"upsert" : {}
}'
如果指定了文档script和脚本doc,同时出现在命令中时doc将被忽略,只有script会运行。 最好的办法是将你的部分文档的字段对放入脚本本身。 即脚本内容可覆盖doc。
7.3 更新操作支持以下查询字符串参数
7.3.1 retry_on_conflict
:
在更新的获取和索引阶段之间,可能有另一个进程可能已经更新了同一个文档。默认情况下,更新将失败并出现版本冲突异常。该retry_on_conflict
参数控制最终抛出异常之前重试更新的次数。
7.3.2 routing
如果正在更新的文档不存在,路由用于将更新请求路由到右分片并设置upsert
请求的路由。不能用于更新现有文档的路由。
7.3.3 parent
:
Parent用于将更新请求路由到右分片,并在正在更新的文档不存在时为upsert请求设置父项。不能用于更新parent现有文档。如果指定了别名索引路由,则它会覆盖父路由,并将其用于路由请求。
7.3.4 timeout
:
超时等待碎片变为可用。
7.3.5 wait_for_active_shards
:
在继续更新操作之前,需要激活的分片副本的数量。
7.3.6 refresh
控制此请求所做的更改对搜索是否可见。
7.3.7 _source
允许控制是否以及如何在响应中返回更新的源。默认情况下,更新的源不会被返回。
7.3.8 version & version_type
:
更新API在内部使用Elasticsearch的版本控制支持来确保文档在更新期间不会更改
。您可以使用该version 参数来指定仅当文档的版本与指定的版本匹配时才更新文档。通过设置版本类型,force
您可以在更新后强制更新文档的新版本(小心使用!force
并不保证文档没有更改)。
参考:https://blog.csdn.net/weixin_39471249/article/details/80427339
8 有关搜索的API
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html
8.1 搜索API
8.1.1 单个索引的所有类型
curl -XGET 'http://192.168.84.184:9200/library/_search?q=author:yuvar&pretty'
返回author字段包含‘yuvar’的所有文档(模糊查询)
8.1.2 搜索多个索引
curl -XGET 'http://192.168.84.184:9200/library1,library2/_search?q=author:yuvar&pretty'
8.1.3 搜索特定类型
curl -XGET 'http://192.168.84.184:9200/library/book,user/_search?q=author:yuvar&pretty'
8.1.4 搜索所有索引
curl -XGET 'http://192.168.84.184:9200/_all/_search?q=author:yuvar&pretty'
或者:
curl -XGET 'http://192.168.84.184:9200/_search?q=author:yuvar&pretty'
8.1.5 其他参数
| 查询字符串(映射到 |
| 在查询中未定义字段前缀时使用的默认字段。 |
| 分析查询字符串时要使用的分析器名称。 |
| 是否应分析通配符和前缀查询。默认为 |
| 应在协调节点上一次减少的分片结果数。如果请求中潜在的分片数量很大,则应将此值用作保护机制,以减少每个搜索请求的内存开销。 |
| 要使用的默认运算符可以是 |
| 如果设置为true将导致忽略基于格式的失败(如向数字字段提供文本)。默认为false。 |
| 对于每个命中,包含如何计算命中得分的解释。 |
| 设置为 |
| 每个匹配返回的文档的选择性存储字段,逗号分隔。不指定任何值将导致没有字段返回。 |
| 排序执行。可以是 |
| 排序时,设置为 |
| 设置为 |
| 搜索超时,将搜索请求限制在指定的时间值内执行,并使用在到期时累积的点击数进行保释。默认为无超时。 |
| 在达到查询执行将提前终止时,为每个分片收集的最大文档数。如果设置,响应将具有一个布尔字段, |
| 从命中的索引开始返回。默认为 |
| 要返回的点击次数。默认为 |
| 要执行的搜索操作的类型。可以是 |
|
|
如果在搜索时没有找到索引,程序会抛出index_not_found_exception异常。
8.2 搜索分片API
8.2.1 路由机制
路由(routing)是指分组从源到目的地时,决定端到端路径的网络范围的进程。
路由是指路由器从一个接口上收到数据包,根据数据包的目的地址进行定向并转发到另一个接口的过程。路由通常与桥接来对比,在粗心的人看来,它们似乎完成的是同样的事。它们的主要区别在于桥接发生在OSI参考模型的第二层(数据链路层),而路由发生在第三层(网络层)。这一区别使二者在传递信息的过程中使用不同的信息,从而以不同的方式来完成其任务。
路由是什么:https://blog.csdn.net/qq_29344757/article/details/82830912
Elasticsearch的路由机制与其分片机制有着直接的关系。Elasticsearch的路由机制即是通过哈希算法,将具有相同哈希值的文档放置到同一个主分片中。这个和通过哈希算法来进行负载均衡几乎是一样的。
而Elasticsearch也有一个默认的路由算法:它会将文档的ID值作为依据将其哈希到相应的主分片上,这种算法基本上会保持所有数据在所有分片上的一个平均分布,而不会产生数据热点。
而我们为什么会需要自定义的Routing模式呢?首先默认的Routing模式在很多情况下都是能满足我们的需求的——平均的数据分布、对我们来说是透明的、多数时候性能也不是问题。但是在我们更深入地理解我们的数据的特征之后,使用自定义的Routing模式可能会给我们带来更好的性能。
假设你有一个100个分片的索引。当一个请求在集群上执行时会发生什么呢?
- 这个搜索的请求会被发送到一个节点
- 接收到这个请求的节点,将这个查询广播到这个索引的每个分片上(可能是主分片,也可能是复制分片)
- 每个分片执行这个搜索查询并返回结果
- 结果在通道节点上合并、排序并返回给用户
因为默认情况下,Elasticsearch使用文档的ID(默认为是路由值)(类似于关系数据库中的自增ID,当然,如果不指定ID的话,Elasticsearch使用的是随机值)将文档平均的分布于所有的分片上,这导致了Elasticsearch不能确定文档的位置,所以它必须将这个请求广播到所有的100个分片上去执行。这同时也解释了为什么主分片的数量在索引创建的时候是固定下来的,并且永远不能改变。因为如果分片的数量改变了,所有先前的路由值就会变成非法了,文档相当于丢失了。
8.2.2 指定个性化路由
所有的文档API(get,index,delete,update和mget)都能接收一个routing参数,可以用来形成个性化文档分片映射。一个个性化的routing值可以确保相关的文档存储到同样的分片上——比如,所有属于同一个用户的文档。
方法一:
比较直观的方法就是直接在请求的URL中指定routing参数:
curl -XPOST 'http://localhost:9200/store/order?routing=user123' -d '
{
"productName": "sample",
"customerID": "user123"
}'
这样我们就按照用户的customerID的值将具有相同customerID的文档置于同一分片上了。
方法二:
直接从文档中提取到对应的路由值。在索引过程中,elasticsearch允许指定一个字段,用该字段的值作为路由值。因此需要在类型定义中进行如下设置:
curl -XPUT 'http://localhost:9200/store/order/_mapping' -d '
{
"order": {
"_routing": {
"required": true,
"path": "customerID"
}
}
}'
方法二和第一种方法在效果上一样的,但是有一点需要注意,相比于第一种方法,方法二的效率稍低,因为第一种方法直接就在请求的参数中确定了路由的值,而第二种方法中,首先需要将文档读入之后,再从中提取到对应的路由值。
8.2.3 _search_shards
https://www.elastic.co/guide/en/elasticsearch/reference/5.0/search-shards.html
返回所有用于搜索和索引的分片:
curl -XGET 'http://192.168.84.184:9200/library/_search_shards?pretty'
返回特定routing下用于搜索和索引的分片:
curl -XGET 'http://192.168.84.184:9200/library/_search_shards?pretty&routing=qupta'
8.3 多搜索Multi-search API(_msearch)
https://www.elastic.co/guide/en/elasticsearch/reference/5.0/search-multi-search.html
需要提供一个包含搜索请求头部和主体部分的文件。请求头的部分指定索引、要操作的类型、search_type、首选项和routing。主体部分包含查询、来源、大小、聚合等。
(1)例如,新建一个名为requests的文件,内容如下:
{"index":"library","type":"book"}
{"query":{"term":{"author":"today"}}}
执行:
curl -XGET "http://192.168.84.184:9200/_msearch?pretty" --data-binary @requests; echo
使用--data-binary,可以指定包含所有头部和主体的文件。@requests是文件名。
(2)新建一个名为requests的文件,内容如下:
{}
{"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
{}
{"query" : {"match_all" : {}}}
{"index" : "test2"}
{"query" : {"match_all" : {}}}
curl -XGET "http://192.168.84.184:9200/_msearch?pretty" --data-binary @requests; echo
8.4 计数API (_count)
https://www.elastic.co/guide/en/elasticsearch/reference/5.0/search-count.html
curl -XGET "http://192.168.84.184:9200/library/book/_count?pretty" -d '{
"query":{
"term":{
"author":"today"
} } }'
结果返回:
结果显示在这五个分片之中,有两个匹配到了文档。
8.5 验证API
https://www.elastic.co/guide/en/elasticsearch/reference/5.0/search-validate.html#search-validate
查询可以变得非常的复杂,尤其和不同的分析器与不同的字段映射结合时,理解起来就有点困难了。不过 validate-query
API 可以用来验证查询是否合法。该操作将针对验证的字段返回true或false的结果。
curl -XGET "http://192.168.84.184:9200/library/book/_validate/query?pretty" -d '{
"query":{
"term":{
"author":"today"
} } }'
结果返回:
8.5.1 explain
explain
参数可以提供更多关于查询不合法的信息。
(1)例如,将term改为query,返回false的示例:
curl -XGET "http://192.168.84.184:9200/library/book/_validate/query?pretty&explain=true" -d '{
"query":{
"query":{
"author":"today"
} } }'
结果返回:
(2)返回true的示例:
curl -XGET "http://192.168.84.184:9200/library/book/_validate/query?pretty&explain=true" -d '{
"query":{
"term":{
"author":"today"
} } }'
我们查询的每一个 index 都会返回对应的 explanation
,因为每一个 index 都有自己的映射和分析器。
8.5.2 rewrite
When the query is valid, the explanation defaults to the string representation of that query. With rewrite
set to true
, the explanation is more detailed showing the actual Lucene query that will be executed.
当查询是true的,使用rewrite=true ,可以获得更详细的实际的Lucene查询的解释。
curl -XGET "http://192.168.84.184:9200/library/book/_validate/query?pretty&rewrite=true" -d '{
"query":{
"term":{
"author":"today"
} } }'
结果:
请求仅在一个随机选择的分片上执行。查询的详细解释可能取决于正在使用哪个分片,因此可能因请求的不同而有所不同。
8.6 结果解释API(_explain)
explain api 计算查询和特定文档的得分说明。无论文档是否匹配特定查询,这都可以提供有用的反馈。
curl -XGET "http://192.168.84.184:9200/library/book/1/_explain?pretty" -d '{
"query":{
"term":{
"author":"today"
} } }'
或者:
curl -XGET "http://192.168.84.184:9200/library/book/1/_explain?q=author:today&pretty"
8.7 字段统计信息API(_field_stats)
该功能是实验性的,在将来的版本中可能会完全更改或删除。在6.+版本中就没有了。
https://www.elastic.co/guide/en/elasticsearch/reference/5.0/search-field-stats.html
默认情况下,_field_stats api在所有索引上执行,但也可以在特定索引上执行。
curl -XGET "http://localhost:9200/_field_stats?fields=rating"
curl -XGET "http://localhost:9200/index1,index2/_field_stats?fields=rating"
参数:
| A list of fields to compute stats for. The field name supports wildcard notation. For example, using |
| Defines if field stats should be returned on a per index level or on a cluster wide level. Valid values are |
curl -XPOST "http://localhost:9200/_field_stats?level=indices" -d '{
"fields" : ["rating"]
}'
curl -XGET "http://192.168.84.184:9200/_field_stats?fields=page&pretty"
结果:
9 删除索引
删除名称tree开头的索引:
curl -XDELETE "http://192.168.84.184:9200/tree*"
如果有,则删除。如果不存在名称为tree开头的索引,并且使用了通配符,则会得到如下结果:
{
"acknowledged":true
}
如果在没有通配符的情况下删除一个索引,若索引不存在时,则程序会抛出index_not_found_exception异常。
curl -XPOST 'http://192.168.84.184:9200/library/book/_search?q=author:今天天气很好&pretty' 未检测出结果
安装分词器?
https://www.cnblogs.com/wangzhuxing/p/9480760.html
ES字段类型及ES内置analyzer分析:http://www.cnblogs.com/wangzhuxing/p/9502183.html
term 查询和match 查询(text和keyword)。分词:https://www.cnblogs.com/shaosks/p/7813729.html
https://blog.csdn.net/andyzhaojianhui/article/details/81382471
https://segmentfault.com/q/1010000017312707
https://www.cnblogs.com/yjf512/p/4897294.html
文档API:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html
query string query :https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#query-dsl-query-string-query
路由:https://blog.csdn.net/qq_29344757/article/details/82830912
%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;%SystemRoot%/system32;%SystemRoot%;