kibana安装
- 下载并解压。
- 打开
config/kibana.yml配置elasticsearch.hosts设置es链接,本机默认环境可忽略。 - 运行
bin/kibana或者bin\kibana.bat on Windows。 - 等待运行输出后,浏览器打开
http://localhost:5601。 - 找到
dev Tools在console中可以调试查询语句。
index设置
mapping设置
查询
getting-started-search
search-search
query-filter-context
DSL
查询所有使用match_all,使用query设置查询条件,sort设置排序。from、size设置分页参数:
{
"query": { "match_all": {} },
"sort": [
{ "{sort_porperty}": "asc" }
],
"from": 10,
"size": 10
}
返回结果:
{
"took" : 506,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 470,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "contents",
"_type" : "_doc",
"_id" : "298718",
"_score" : 1.0,
"_source" : {
"id" : 298718,
"desc" : "阿里妈妈图标库,医药箱"
}
}
]
}
}
_score为每一条结果匹配程度的分数,分数根据query查询子句进行计算,如果没有查询子句默认为1,es将按匹配分数降序返回,分数越高的在前面。但是如果指定了排序条件则不会计算分数而是按排序条件进行排序后返回结果。
_source保存的原始文档。
在一个字段中进行搜索使用match:
{
"query": { "match": { "address": "mill lane" } }
}
一个match中只能指定一个字段,可以使用bool包含多个查询子句构建复杂查询,查询子句需要指定判断标准:must(必须匹配)、should(应该匹配)、must_not(必须不匹配),每一个查询子句将会参与文档匹配分数的计算,分数越高文档越匹配。
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "sex": "female" } }
]
}
}
must类似SQL中的and,如果must包含多个查询子句,则需要满足所有查询子句。
should查询子句类似于SQL的or但不完全等于or,should中当有多个查询子句时,则需要至少匹配其中一个条件。如果多个查询子句返回的结果与预期有差异,可以通过minimum_should_match参数来控制匹配的条件生效个数。
must_not查询子句的功能与filter筛选器一样,决定一个doc是否匹配,如果不匹配则不会返回在结果中。
使用filter过滤:
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": [
{ "term": { "status": "published" }},
{ "range": { "publish_date": { "gte": "2015-01-01", "lte": "2020-06-12"}}}
]
}
}
}
term匹配相等,range匹配范围。
查询条件命名
给查询条件命名,在结果中会显示每一条记录匹配了那些条件:
GET contents/_search
{
"query": {
"bool": {
"filter": [
{
"terms" : {
"type" : ["picture"],
"_name" : "filter"
}
}
],
"should": [
{
"term": {
"permission": 2
}
},
{
"bool": {
"must": [
{
"terms" : {
"permission" : [1],
"_name" : "permission"
}
},
{
"terms" : {
"owner_ids" : [2],
"_name" : "owner_ids"
}
}
]
}
}
]
}
}
}
在结果中查看matched_queries中匹配的条件:
{
"_index" : "contents",
"_type" : "_doc",
"_id" : "308009",
"_score" : null,
"_source" : {
"id" : 308009,
"type" : "picture",
"owner_ids" : [
2
],
"permission" : 1,
},
"matched_queries" : [
"filter",
"permission",
"owner_ids"
]
}
中文分词
通过docker容器启动的es安装了中文分词后提示analyzer [ik_smart] not found for field [content]的解决方法:重启一下容器就行了docker-compose restart es01
SDK
RESTful API
查看所有index
curl -XGET 'http://localhost:9200/_cat/indices?v'
使用id查看index某一个document
curl -XGET 'http://localhost:9200/{index}/_doc/{id}?pretty=true'
查询
curl -XGET 'http://localhost:9200/{index}/_search?pretty=true' -H 'Content-Type:application/json' -d'
{
"query": { "match_all": {} },
"sort": [
{ "{sort_property}": "asc" }
],
"from": 10,
"size": 10
}'