内容简介:查询集群的名字查询集群的健康状况获取集群的所有索引
查询集群的名字
⇒ curl -XGET 'http://localhost:9200' 复制代码
查询集群的健康状况
⇒ curl -XGET 'http://localhost:9200/_cluster/health?format=yaml' 复制代码
status字段说明:
- green 一切正常
- yellow replicas没有分配[可能是只有单个节点],集群正常
- red 某些数据取不到 format=yaml指定使用yaml格式输出,方便查看
索引操作
获取集群的所有索引
⇒ curl -XGET 'http://localhost:9200/_cat/indices' 复制代码
索引的字段
⇒ curl -XGET 'http://localhost:9200/mytest/_mapping?format=yaml' 复制代码
结果
mytest:
mappings:
external:
properties:
addre:
type: "string"
name:
type: "string"
复制代码
它类似于数据库的schema,描述文档可能具有的字段或属性、每个字段的数据类型。
字段对于非string类型,一般只需要设置type。string域两重要属性 index analyzer
index
1. analyzed 全文索引这个域。首先分析字符串,然后索引 2. not_analyzed 精确索引 ,不分析 3. no 此域不会被搜索 复制代码
analyzer
将文本分成四核倒排索引的独立词条,后将词条统一化提高可搜索性 复制代码
动态映射: 文档中出现之前从未遇到过的字段,动态确定数据类型,并自动把新的字段添加到类型映射
新建索引
⇒ curl -XPUT 'localhost:9200/mytest' 复制代码
删除索引
⇒ curl -XDELETE 'localhost:9200/mytest?format=yaml' 复制代码
数据查询
插入单条数据
⇒ curl -XPUT 'localhost:9200/mytest/external/1?format=yaml' -d '
quote> { "name":"paxi"}'
复制代码
查询单条数据
⇒ curl -XGET 'localhost:9200/mytest/external/1?format=yaml' 复制代码
删除单条数据
curl -XDELETE 'localhost:9200/mytest/external/3?format=yaml' 复制代码
存储的文本分析
curl -XGET 'localhost:9200/_analyze?format=yaml' -d '
{"papa xixi write"}'
复制代码
结果为
tokens: - token: "papa" start_offset: 3 end_offset: 7 type: "<ALPHANUM>" position: 1 - token: "xixi" start_offset: 8 end_offset: 12 type: "<ALPHANUM>" position: 2 - token: "write" start_offset: 13 end_offset: 18 type: "<ALPHANUM>" position: 3 复制代码
token 表示实际存储的词条,position表示词条在原始文本中的位置。
可以看出完整的文本会被切割存储成不同的词条
不返回元数据
curl -XGET 'localhost:9200/mytest/_search?filter_path=hits.hits._source&format=yaml' -d '
{ "query":{"match":{"name":"papa xixi write"}}}'
复制代码
低版本无法生效
只返回部分字段
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ "query":{"match":{"name":"papa xixi write"}},"_source":["name"]}'
复制代码
低版本无效,可以用通配符
match查询
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ "query":{"match":{"name":"papa xixi write"}}}'
复制代码
查询匹配的结果如下
hits:
- _index: "mytest"
_type: "external"
_id: "11"
_score: 0.6532502
_source:
name: "papa xixi write"
- _index: "mytest"
_type: "external"
_id: "4"
_score: 0.22545706
_source:
name: "papa xixi"
- _index: "mytest"
_type: "external"
_id: "2"
_score: 0.12845722
_source:
name: "papa"
- _index: "mytest"
_type: "external"
_id: "10"
_score: 0.021688733
_source:
name: "xixi"
复制代码
从查询结果,它获取到了所有包含 papa 、 xixi和write 的词,相当于将原来的词拆开,然后两个单词做了 OR 操作,如果要全部匹配,可以使用AND操作
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ "query":{"match":{"name":{"query":"papa xixi write","operator":"and"}}}}'
---
hits:
total: 1
max_score: 0.6532502
hits:
- _index: "mytest"
_type: "external"
_id: "11"
_score: 0.6532502
_source:
name: "papa xixi write"
复制代码
如果只是想提高精度
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ "query":{"match":{"name":{"query":"papa xixi write","minimum_should_match":"75%"}}}}'
---
hits:
total: 2
max_score: 0.6532502
hits:
- _index: "mytest"
_type: "external"
_id: "11"
_score: 0.6532502
_source:
name: "papa xixi write"
- _index: "mytest"
_type: "external"
_id: "4"
_score: 0.22545706
_source:
name: "papa xixi"
复制代码
term查询
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ "query":{"term":{"name":"papa xixi write"}}}'
复制代码
它的结果是什么也没有查到
total: 0 max_score: null hits: [] 复制代码
换用查询语句
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ "query":{"term":{"name":"papa"}}}'
复制代码
结果为
hits:
- _index: "mytest"
_type: "external"
_id: "2"
_score: 1.0
_source:
name: "papa"
- _index: "mytest"
_type: "external"
_id: "4"
_score: 0.37158427
_source:
name: "papa xixi"
- _index: "mytest"
_type: "external"
_id: "11"
_score: 0.2972674
_source:
name: "papa xixi write"
复制代码
match 和 term的区别
match 如果在全文字段上查询,会使用正确的分析器分析查询字符串;如果精确值字段使用,会精确匹配。 term精确匹配,只要包含了对应的文本就可以,不对文本分析(not_analyzed文本会精确匹配,terms 多个值只要有一个匹配就匹配);
从"papa xixi write"的存储文本分析来看,它本身会被切割成不同的词条,所以用 term查询"papa xixi write",无法获取到结果,但是match确能够匹配
filter使用
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ "query":{"filtered":{"filter":{"range":{"name":{"gt":"w"}}}}}}'
复制代码
或者
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ "query":{"constant_score":{"filter":{"range":{"name":{"gt":"w"}}}}}}'
复制代码
验证语法是否正确
⇒ curl -XGET 'localhost:9200/_validate/query?explain&format=yaml' -d '{ "query":{{"filter":{"range":{"name":{"gt":"w"}}}}}'
---
valid: false
//原因省略
复制代码
bool使用
使用term查询
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ "query":{"term":{"addre":"beijing"}}}'
复制代码
结果为
hits:
- _index: "mytest"
_type: "external"
_id: "5"
_score: 0.30685282
_source:
addre: "beijing"
- _index: "mytest"
_type: "external"
_id: "6"
_score: 0.30685282
_source:
addre: "beijing"
name: "px"
复制代码
转换为bool查询,结果一样
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ query:{bool:{must:{match:{addre:"beijing"}}}}}'
复制代码
如果只想要最后一条
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ query:{bool:{must:{match:{addre:"beijing"}},must:{match:{name:"px"}}}}}'
复制代码
想要第一条
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ query:{bool:{must:{match:{addre:"beijing"}},must_not:{match:{name:"px"}}}}}'
复制代码
都想要
curl -XGET 'localhost:9200/mytest/_search?format=yaml' -d '
{ query:{bool:{must:{match:{addre:"beijing"}},should:{match:{name:"px"}}}}}'
复制代码
must的意思是当前值必须是有的,must_not必须没有,should表示数据可以有也可以没有
以上所述就是小编给大家介绍的《通过命令curl 操作ElasticSearch指南》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 通过实例学习 tcpdump 命令
- 通过 laravel 命令构建项目报错
- 如何通过命令行 msbuild 编译项目
- 通过 mysqlbinlog 和 grep 命令定位binlog文件中指定操作
- 再看Zebrocy:APT28通过Delphi后门执行了哪些命令?
- 通过命令行在Python中测试以太坊RPC客户端
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
游戏编程权威指南
Mike McShaffry 麦克沙福瑞、David “Rez” Graham 格雷海姆 / 师蓉、李静、李青翠 / 人民邮电 / 2016-3 / 99.00元
全书分为4个部分共24章。首部分是游戏编程基础,主要介绍了游戏编程的定义、游戏架构等基础知识。 第二部分是让游戏跑起来,主要介绍了初始化和关闭代码、主循环、游戏主题和用户界面等。 第三部分是核心游戏技术,主要介绍了一些*为复杂的代码 示例,如3D编程、游戏音频、物理和AI编程等。 第四部分是综合应用,主要介绍了网络编程、多道程序设计和用C#创建工具等,并利用前面所讲的 知识开发出......一起来看看 《游戏编程权威指南》 这本书的介绍吧!