Elasticsearch 索引API

Elasticsearch 索引文档


Request URL:http://localhost:4567/{index}/_search
字段index为索引编号
eg:http://localhost:4567/app_access_log-2018.04.06/_search

Request Method: POST

Request Payload

{
    "query":{查询体},
    "from": 0,
    "size": 10,
    "sort": [],
    "aggs": {}
}

json格式,query为查询字典的key里面是具体的查询语句,from为Response返回结果列表在页面显示的开始位置。默认为。size:返回数据的数量。sort默认为空列表[]aggs默认为空字典{}

query 查询体字段解释

"query": {
    "bool": {
        "must": [查询条件],
        "must_not": [查询条件],
        "should": [查询条件]
    }
}

must列表填充查询结果里必须包含的query字段,must_not列表填充查询结果里必须不包含的query字段,should列表填充查询结果里可能存在的query字段。
bool must语句指明了,对于一个文档,所有的查询都必须为真,这个文档才能够匹配成功。
bool should语句指明,对于一个文档,查询列表中,只要有一个查询匹配,那么这个文档就被看成是匹配的。
bool must_not语句指明,对于一个文档,查询列表中的的所有查询都必须都不为真,这个文档才被认为是匹配的。
[]查询条件格式

[{
    "query_string": {
        "default_field": "message",
        "query": "具体关键字"
    }
}, {
    "range": {
        "@timestamp": {
            "gt": "2018-04-06T00:00:06.824Z",
            "lt": "2018-04-08T00:00:06.824Z"
        }
    }
}]

字典query_string内包含查询的内容query具体关键字
字典range内包含时间戳@timestamp时间戳的范围可以用gtlt,gte,lte来限定大于,小于,大于,等于。具体时间格式为2018-04-06T00:00:06.824Z这样的格式。

举例查询

Request Payload(不加时间戳限制条件,单条件查询。必须包含查询字段:金融 ;一次返回10条查询记录):

{
"query": {
    "bool": {
        "must": [{
            "query_string": {
                "default_field": "message",
                "query": "金融"
            }
        }],
        "must_not": [],
        "should": []
    }
},
"from": 0,
"size": 10,
"sort": [],
"aggs": {}
}

Request Payload(不加时间戳限制条件,多条件查询。必须包含查询字段:金融 ,一定不包含查询字段:财务;一次返回10条查询记录):

{
"query": {
    "bool": {
        "must": [{
            "query_string": {
                "default_field": "message",
                "query": "金融"
            }
        }],
        "must_not": [{
            "query_string": {
                "default_field": "message",
                "query": "财务"
            }
        }],
        "should": []
    }
},
"from": 0,
"size": 10,
"sort": [],
"aggs": {}
}

Request Payload(不加时间戳限制条件,多条件查询。必须包含查询字段:金融 ,一定不包含查询字段:财务,可能存在字段:发财了;一次返回10条查询记录):

{
"query": {
    "bool": {
        "must": [{
            "query_string": {
                "default_field": "message",
                "query": "金融"
            }
        }],
        "must_not": [{
            "query_string": {
                "default_field": "message",
                "query": "财务"
            }
        }],
        "should": [{
            "query_string": {
                "default_field": "message",
                "query": "发财了"
            }
        }]
    }
},
"from": 0,
"size": 10,
"sort": [],
"aggs": {}
}

Request Payload(加时间戳限制条件,多条件查询。时间限制大于 2018-04-06T00:00:06.824Z ,小于等于 2018-04-08T00:00:06.824Z 必须包含查询字段:金融 ,一定不包含查询字段:财务,可能存在字段:发财了;一次返回10条查询记录):

{
"query": {
    "bool": {
        "must": [{
            "query_string": {
                "default_field": "message",
                "query": "金融"
            }
        }, {
            "range": {
                "@timestamp": {
                    "gt": "2018-04-06T00:00:06.824Z",
                    "lte": "2018-04-08T00:00:06.824Z"
                }
            }
        }],
        "must_not": [{
            "query_string": {
                "default_field": "message",
                "query": "财务"
            }
        }],
        "should": [{
            "query_string": {
                "default_field": "message",
                "query": "发财了"
            }
        }]
    }
},
"from": 0,
"size": 10,
"sort": [],
"aggs": {}
}

Response 响应结果

{
"took": 354,
"timed_out": false,
"_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
},
"hits": {
    "total": 60884,
    "max_score": 2.1993985,
    "hits": [查询的结果]
}
}

对于这个响应,我们看到了以下的部分:
- took —— Elasticsearch执行这个搜索的耗时,以毫秒为单位
- timed_out —— 指明这个搜索是否超时
- _shards —— 指出多少个分片被搜索了,同时也指出了成功/失败的被搜索的shards的数量
- hits —— 搜索结果
- hits.total —— 能够匹配我们查询标准的文档的总数目
- hits.hits —— 真正的搜索结果数据(默认只显示前10个文档)
- _score和max_score —— 现在先忽略这些字段

hits.hits查询结果

{
"_index": "app_log-2018.05.01",
"_type": "app_log",
"_id": "AWMZbfFNkdRKmtq_ckcT",
"_score": 2.1993985,
"_source": {
    "@timestamp": "2018-05-01T02:00:25.209Z",
    "message": "具体的log信息",
    "@version": "1",
    "path": "/data/logs-app/all.log",
    "host": "localhost",
    "type": "app_log",
    "tags": ["_grokparsefailure"]
}
}

对于hits.hits查询结果,我们看到了以下的部分:

- _index —— log索引的日志
- _type —— log类型
- _id —— 可忽略
- _score —— 索引结果 接近匹配程度的程度,分值越高,越符合要求
- _source —— 查询到的结果 message中就是我们想要的结果

:
有一点需要重点理解一下,一旦你取回了你的搜索结果,Elasticsearch就完成了使命,它不会维护任何服务器端的资源或者在你的结果中打开游标。这是和其它类似SQL的平台的一个鲜明的对比, 在那些平台上,你可以在前面先获取你查询结果的一部分,然后如果你想获取结果的剩余部分,你必须继续返回服务端去取,这个过程使用一种有状态的服务器端游标技术。

参考文献

ElasticSearch查询介绍
Elasticsearch基础

猜你喜欢

转载自blog.csdn.net/bug4pie/article/details/80217450