其他分享
首页 > 其他分享> > ElasticSearch学习笔记(四) 检索相关

ElasticSearch学习笔记(四) 检索相关

作者:互联网

查询字段解析:

_search:我们可以使用_search API来检索查询索引。搜索API的最基础的形式是没有指定任何查询的空搜索,它简单地返回集群中所有索引下的所有文档:

GET /_search

返回结果如图:

{
   "hits" : {
      "total" :       14,
      "hits" : [
        {
          "_index":   "us",
          "_type":    "tweet",
          "_id":      "7",
          "_score":   1,
          "_source": {
             "date":    "2014-09-17",
             "name":    "John Smith",
             "tweet":   "The Query DSL is really powerful and flexible",
             "user_id": 2
          }
       },
        ... 9 RESULTS REMOVED ...
      ],
      "max_score" :   1
   },
   "took" :           4,
   "_shards" : {
      "failed" :      0,
      "successful" :  10,
      "total" :       10
   },
   "timed_out" :      false
}

返回结果内容解析:

total :查询到的结果总条数;

hits:包含 total 字段来表示匹配到的文档总数,并且一个 hits 数组默认会包含所查询结果的前十个文档。

took: 执行整个搜索请求耗费了多少毫秒。

shards: 告诉我们在查询中参与分片的总数,以及这些分片成功了多少个失败了多少个。

timeout:告诉我们查询是否超时。默认情况下,搜索请求不会超时。在请求超时之前,Elasticsearch 将会返回已经成功从每个分片获取的结果。

查询相关:

//多索引查询
/gb,us/doc/_search
//单索引查询
/gb/doc/_search

查询表达式:
查询语句结构:

{
    QUERY_NAME: {
        FIELD_NAME: {
            ARGUMENT: VALUE,
            ARGUMENT: VALUE,...
        }
    }
}

(1)空查询:查询索引所有文档

GET /_search
{
    "query": {
        "match_all": {}
    }
}

(2)针对某个字段查询:使用 match 查询语句 来查询 tweet 字段中包含 elasticsearch 的 tweet:

GET /_search
{
    "query": {
        "match": {
            "tweet": "elasticsearch"
        }
    }
}

(3)合并查询语句:查询语句(Query clauses) 就像一些简单的组合块,这些组合块可以彼此之间合并组成更复杂的查询。这些语句可以是如下形式:

{
    "bool": {
        "must":     { "match": { "tweet": "elasticsearch" }},
        "must_not": { "match": { "name":  "mary" }},
        "should":   { "match": { "tweet": "full text" }},
        "filter":   { "range": { "age" : { "gt" : 30 }} }
    }
}

一条复合语句可以合并任何其它查询语句,包括复合语句,了解这一点是很重要的。这就意味着,复合语句之间可以互相嵌套,可以表达非常复杂的逻辑。

标签:检索,语句,tweet,笔记,查询,score,文档,ElasticSearch,match
来源: https://blog.csdn.net/weixin_43981315/article/details/117711236