查询集群的名字数据库
⇒ curl -XGET 'http://localhost:9200'
复制代码
查询集群的健康情况bash
⇒ curl -XGET 'http://localhost:9200/_cluster/health?format=yaml'
复制代码
status字段说明:app
- green 一切正常
- yellow replicas没有分配[多是只有单个节点],集群正常
- red 某些数据取不到 format=yaml指定使用yaml格式输出,方便查看
获取集群的全部索引curl
⇒ curl -XGET 'http://localhost:9200/_cat/indices'
复制代码
索引的字段ui
⇒ curl -XGET 'http://localhost:9200/mytest/_mapping?format=yaml'
复制代码
结果url
mytest:
mappings:
external:
properties:
addre:
type: "string"
name:
type: "string"
复制代码
它相似于数据库的schema,描述文档可能具备的字段或属性、每一个字段的数据类型。spa
字段对于非string类型,通常只须要设置type。string域两重要属性 index analyzercode
indexorm
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"]}'
复制代码
低版本无效,能够用通配符
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"
复制代码
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精确匹配,只要包含了对应的文本就能够,不对文本分析(not_analyzed文本会精确匹配,terms 多个值只要有一个匹配就匹配);
从"papa xixi write"的存储文本分析来看,它自己会被切割成不一样的词条,因此用 term查询"papa xixi write",没法获取到结果,可是match确可以匹配
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
//缘由省略
复制代码
使用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表示数据能够有也能够没有