【ElasticSearch(五)进阶】两种_search检索方式,match_all检索,Query DSL基本使用

【ElasticSearch(五)进阶】两种_search检索方式,match_all检索,Query DSL基本使用


一、导入测试数据

ElasticSearch官方为我们准备了一部分测试数据供调试使用,我们可以Kinaba内进行数据导入处理

1.获取数据
https://download.elastic.co/demos/kibana/gettingstarted/accounts.zip

2.执行批量添加

http://localhost:5601中选择Kibana,选择侧边栏的Dev Tools

POST /bank/account/_bulk
后面粘贴刚才下载的数据 

二、_search检索

ES支持两种基本方式检索:

一个是通过使用 REST request URI发送搜索参数(uri+检索参数)

另一个是通过使用 REST request body来发送它们(uri请求体)


【例子1】

GET /bank/_search?q=*&sort=account_number:asc

q=*表示全部搜索

sort=account_number:asc:根据account_number排序,是升序排序


返回结果:

结果并不会返回所有数据,只返回10条数据,类似于分页

took:检索花费时间

timed_out:检索是否超时

_shards:集群情况下,每个分片都为检索做了什么操作

hits:命中的记录

total.value:总记录的数量

max_score:最相关文档的得分

hits:所有命中的记录

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1000,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "0",
        "_score" : null,
        "_source" : {
          "account_number" : 0,
          "balance" : 16623,
          "firstname" : "Bradshaw",
          "lastname" : "Mckenzie",
          "age" : 29,
          "gender" : "F",
          "address" : "244 Columbus Place",
          "employer" : "Euron",
          "email" : "bradshawmckenzie@euron.com",
          "city" : "Hobucken",
          "state" : "CO"
        },
        "sort" : [
          0
        ]
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "account_number" : 1,
          "balance" : 39225,
          "firstname" : "Amber",
          "lastname" : "Duke",
          "age" : 32,
          "gender" : "M",
          "address" : "880 Holmes Lane",
          "employer" : "Pyrami",
          "email" : "amberduke@pyrami.com",
          "city" : "Brogan",
          "state" : "IL"
        },
        "sort" : [
          1
        ]
      },
     。。。
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "9",
        "_score" : null,
        "_source" : {
          "account_number" : 9,
          "balance" : 24776,
          "firstname" : "Opal",
          "lastname" : "Meadows",
          "age" : 39,
          "gender" : "M",
          "address" : "963 Neptune Avenue",
          "employer" : "Cedward",
          "email" : "opalmeadows@cedward.com",
          "city" : "Olney",
          "state" : "OH"
        },
        "sort" : [
          9
        ]
      }
    ]
  }
}

【例子2】

先按照account_number进行降序,

query:查询条件

match_all:匹配所有

sort:排序条件

"account_number": "desc"先按照account_number进行降序排列

"balance": "desc":如果account_number相同,就按照balance进行降序

GET bank/_search
{
  "query": {
      "match_all": {}
  },
  "sort": [
    {
      "account_number": "desc"
    },
    {
      "balance": "desc"
    }
  ]
}

返回结果:

{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1000,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "999",
        "_score" : null,
        "_source" : {
          "account_number" : 999,
          "balance" : 6087,
          "firstname" : "Dorothy",
          "lastname" : "Barron",
          "age" : 22,
          "gender" : "F",
          "address" : "499 Laurel Avenue",
          "employer" : "Xurban",
          "email" : "dorothybarron@xurban.com",
          "city" : "Belvoir",
          "state" : "CA"
        },
        "sort" : [
          999,
          6087
        ]
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "998",
        "_score" : null,
        "_source" : {
          "account_number" : 998,
          "balance" : 16869,
          "firstname" : "Letha",
          "lastname" : "Baker",
          "age" : 40,
          "gender" : "F",
          "address" : "206 Llama Court",
          "employer" : "Dognosis",
          "email" : "lethabaker@dognosis.com",
          "city" : "Dunlo",
          "state" : "WV"
        },
        "sort" : [
          998,
          16869
        ]
      },
        。。。
    ]
  }
}

上面这种 查询条件 写成 请求体 的方式,就称为Query DSL。

三、Query DSL

1.基本语法格式

ElasticSearch提供了一个可以执行查询的Json风的DSL(domain-specific language 领域特定语言),这被称为 Query DSL。

该查询语言非常全面,并且刚开始的时候感觉有点复杂,真正学好它的方法是从一些基础的示例开始的。


【例子1】

一个查询语句 的典型结构:

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

例如查询索引bank的所有数据:

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

【例子2】

如果是针对某个字段查询,查询结构:

{
    QUERY_NAME:{
        FIELD NAME:{
            ARGUMENI:VALUE,
            ARGUMENT:VALUE...
        }
    }
}

按照 balance 降序查询:

GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "balance": {
        "order": "desc"
      }
    }
  ]
}

其中

"balance": {
    "order": "desc"
}

可以简写为:

"balance": "desc"

【例子3】

from:从查询到的第一条记录开始

size:共显示5条

GET /bank/_search
{
  "query":{
    "match_all": {}
  },
  "sort":[
    {
      "balance": "desc"
    }
  ],
  "from": 0,
  "size": 5
}

返回的记录原本默认是10条,现在确实只有5条了

【ElasticSearch(五)进阶】两种_search检索方式,match_all检索,Query DSL基本使用


【例子4】

"_source": ["balance", "account_number"]:只显示字段balanceaccount_number。相当于筛选字段。

GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "balance": {
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 5,
  "_source": ["balance", "account_number"]
}
上一篇:C++ 之Boost 实用工具类及简单使用


下一篇:Postgresql杂谈 05—Postgresql中的JSON和JSONB类型