在使用elasticsearch进程中,经常需要用到一些类似范围的搜索。

返回查询数量

当使用search的时候,默认只返回10条数据,可以通过size参数设置:

curl --cacert http_ca.crt -u elastic -XGET https://localhost:9200/test_record/_search?pretty  -H 'Content-Type: application/json' -d '{"size":100,"query":{"match_all":{}}}'

如果正确就会返回test_record的100条数据:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_record",
        "_id" : "vdoNN4QBHJgRqN7h6HeV",
        "_score" : 1.0,
        "_source" : {
          "date" : "2021-11-16T13:12:00",
          "counts" : 11,
          "key" : "中华人民共和国国徽"
        }
      },
      {
        "_index" : "test_record",
        "_id" : "qjK5HIQB8xpN1Xtdy25q",
        "_score" : 1.0,
        "_source" : {
          "date" : "2021-12-15T14:12:00",
          "counts" : 2,
          "key" : "中华人民共和国国旗"
        }
      },
      {
        "_index" : "test_record",
        "_id" : "qDK5HIQB8xpN1Xtdcm4P",
        "_score" : 1.0,
        "_source" : {
          "date" : "2021-10-16T13:12:00",
          "counts" : 0,
          "key" : "中华人民共和国"
        }
      },
      {
        "_index" : "test_record",
        "_id" : "qTK5HIQB8xpN1Xtdm245",
        "_score" : 1.0,
        "_source" : {
          "date" : "2021-11-15T13:12:00",
          "counts" : 1,
          "key" : "中华人民共和国万岁"
        }
      },
      {
        "_index" : "test_record",
        "_id" : "vNoNN4QBHJgRqN7h6HeV",
        "_score" : 1.0,
        "_source" : {
          "date" : "2021-11-15T13:12:00",
          "counts" : 3,
          "key" : "中华人民共和国国籍"
        }
      }
    ]
  }
}

分页查询

使用数据库的时候,当查询结果太多的时候,可以用分页查询,在elasticsearch中也可以:

curl --cacert http_ca.crt -u elastic -XGET https://localhost:9200/test_record/_search?pretty  -H 'Content-Type: application/json' -d '{"from":0,"size":2,"query":{"match_all":{}}}' 

from就是起始位置,size就是每次取的数量。

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_record",
        "_id" : "vdoNN4QBHJgRqN7h6HeV",
        "_score" : 1.0,
        "_source" : {
          "date" : "2021-11-16T13:12:00",
          "counts" : 11,
          "key" : "中华人民共和国国徽"
        }
      },
      {
        "_index" : "test_record",
        "_id" : "qjK5HIQB8xpN1Xtdy25q",
        "_score" : 1.0,
        "_source" : {
          "date" : "2021-12-15T14:12:00",
          "counts" : 2,
          "key" : "中华人民共和国国旗"
        }
      }
    ]
  }
}

查询排序

使用sort可以对查询进行排序:

url --cacert http_ca.crt -u elastic -XGET https://localhost:9200/test_record/_search?pretty  -H 'Content-Type: application/json' -d '{"from":0,"size":2,"query":{"match_all":{}},"sort":[{"counts":{"order":"desc"}}]}'

sort是一个数组类型的,可以实现对多个字段的综合排序

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "test_record",
        "_id" : "vdoNN4QBHJgRqN7h6HeV",
        "_score" : null,
        "_source" : {
          "date" : "2021-11-16T13:12:00",
          "counts" : 11,
          "key" : "中华人民共和国国徽"
        },
        "sort" : [
          11
        ]
      },
      {
        "_index" : "test_record",
        "_id" : "vNoNN4QBHJgRqN7h6HeV",
        "_score" : null,
        "_source" : {
          "date" : "2021-11-15T13:12:00",
          "counts" : 3,
          "key" : "中华人民共和国国籍"
        },
        "sort" : [
          3
        ]
      }
    ]
  }
}
0

本文为原创文章,转载请注明出处,欢迎访问作者网站(和而不同)

发表评论

error: Content is protected !!