目前只是ES查询的初步入门,只是到一个会用的阶段。
前面接触的都是当一个框架进行直接的使用,现在需要了解其查询方式以及语法了,所以有该文。
基本搜索
-
内容全局搜索
curl -H 'Content-Type:application/json' -XPOST "http://127.0.0.1:9200/rss/_search" -d '{"query": {"multi_match" : {"query" : "<keyword>", "fuzziness": "AUTO"}}}'
-
基本的按字段来进行搜索
curl -H 'Content-Type:application/json' -XPOST "http://127.0.0.1:9200/rss/_search" -d '{"query" :{"terms" : {"feed_link" : ["php","java"]}}}' curl -H 'Content-Type:application/json' -XPOST "http://127.0.0.1:9200/rss/_search" -d ' {"size":0 ,"query": {"terms": {"_id": ["67f46d00eef39992fa78a3749b2a3bee", "2d5f7ad88b88f530b55777c89927d245"]}}}'
磁盘空间不足导致索引只读
在清理磁盘之后解除索引只读状态,在Console执行如下命令。
PUT _settings
{
"index": {
"blocks": {
"read_only_allow_delete": "false"
}
}
}
由于data的分区的空间使用量到95%,会自动的标记索引为只读,导致写入 403
FORBIDDEN/12/index read-only / allow delete (api)
索引添加一个字段
es是不支持进行删改的,所以只能是增加一个新的字段,这种打补丁的方式,可能会使得索引的mapping越来越冗杂
PUT http://host/uploadusagestats/_mapping/usagestats
{
"properties":{
"last_time_used":{"type" : "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"}
}
}
es 随机返回部分数据
从所有的查询数据中随机返回返回指定数量的数据,其中size的值约束返回的数量
{
"from": 0,
"size": 10,
"query": {"bool": {"must": {"term": {"level": 1}}}},
"sort": {
"_script": {
"script": "Math.random()",
"type": "number",
"order": "asc"
}
}
}
使用聚合
使用聚合来进行一类数据的统计以及运算。 关键的地方在下面的bucket_script
部分,可以进行聚合之后的桶计算。49行
POST /metricbeat-iops_kafka-*/_search?
{
"size": 0,
"query": {
"bool": {
"filter": [
{
"range": {
"@timestamp": {
"gte": "1568563200000",
"lte": "1568649599999",
"format": "epoch_millis"
}
}
},
{
"query_string": {
"analyze_wildcard": true,
"query": "kafka.topic.name:appjoox and metricset.name : \"consumergroup partition\""
}
}
]
}
},
"aggs": {
"2": {
"date_histogram": {
"interval": "1m",
"field": "@timestamp",
"min_doc_count": 0,
"extended_bounds": {
"min": "1568563200000",
"max": "1568649599999"
},
"format": "epoch_millis"
},
"aggs": {
"3": {
"max": {
"field": "kafka.partition.offset.newest"
}
},
"4": {
"max": {
"field": "kafka.consumergroup.offset"
}
},
"7":{
"bucket_script": {
"buckets_path": {
"w_offset": "3",
"r_offset": "4"
},
"script": "params.w_offset - params.r_offset"
}
}
}
}
}
}
iK的中文索引
使用 ik插件来提升中文搜索的体验,解决分词不准确的问题。
curl -XPOST http://localhost:9200/index/_mapping -H 'Content-Type:application/json' -d'
{
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
}
}'