(八):ElasticSearch结构化查询 Query DSL

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

1、准备工作

1.1、测试数据下载

  测试数据下载:https://download.elastic.co/demos/kibana/gettingstarted/accounts.zip

  测试数据如下:

0

1.2、测试数据导入

  进入到下载的accounts.json目录,执行如下命令导入数据:

  curl -H "Content-Type: application/json" -XPOST http://localhost:9200/items/_bulk --data-binary @accounts.json

  执行结果如下:

0

  通过访问head插件,查看索引及类型结果:

0

2、基本语法

  完整的语法:

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

   针对某字段的语法:

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

3、match_all - 匹配所有

  match_all是匹配所有的数据,DSL如下:

# 匹配所有数据
GET /items/_search
{
  "query": {"match_all": {}}
  , "sort": [
    {
      "account_number": {
        "order": "desc"
      },
      "age": {
        "order": "desc"
      }
    }
  ]
}

  查询出所有数据,按照 account_number 、 age 倒序输出。

4、macth - 分词匹配

  match_all是匹配所有的数据,match是条件匹配。

# 条件匹配
GET /items/_search
{
  "query": {
    "match": {
      "account_number": 99
    }
  }
}

  match返回的是 account_number:99的记录,执行结果如下:

0

  match会对检索的字段中的值做分词,如 586 Lloyd Court 会分为 586、Lloyd、Court 这三个词。

  查询出address中包含 586 或 Lloyd 或 Court 的所有记录,并给出相关性得分。

0

5、match_phrase - 不分词匹配

  match_phrase不会对检索字段中的值做分词,匹配的值会被当成一个整体单词(不分词)进行检索,短语匹配。

  查询出address中包含 586 Lloyd Court 的所有记录,并给出相关性得分。

0

6、multi_match - 多字段匹配

  多字段匹配,多字段匹配指定内容,对应查询的值会完成分词操作。

GET items/_search
{
  "query": {
    "multi_match": {
      "query": "Putnam Avenue",
      "fields": ["address", "city"]
    }
  }
}

  查询出city或者address中包含 Putnam Avenue 的记录。

0

7、bool - 布尔查询

  布尔查询也被称为组合查询,bool用来实现复合查询。bool通过 must、should、must_not的方式进行组合,合并其他查询语句,实现复杂逻辑查询。

  must表示必须满足,must-not表示必须不满足,should表示不满足条件也会显示,若满足了条件,相关性分数会比较高。

GET /items/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { 
          "age": "28" 
        }}
      ],
      "must_not": [
        { "match": 
        { 
          "gender": "F" 
        }}
      ],
      "should": [
        {"match": {
          "state": "ND"
        }}
      ]
    }
  }
}

   查询age为28,gender不为F,state可以为ND的记录。

0

8、filter - 结果过滤

  filter结果过滤,对字段进行区间取值。

GET /items/_search
{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": {
        "range": {
          "balance": {
            "gte": 30000,
            "lte": 35000
          }
        }
      }
    }
  }
}

  查询出账户余额 30000 ~ 35000 的记录。

0

9、term - 匹配非text字段

  term用于匹配某个属性的值,全文检索字段用match,其他非text字段匹配用term。

# term匹配非text字段
GET /items/_search
{
  "query": {
    "term": {
      "account_number": 30
    }
  }
}

  匹配 account_number 为 30 的记录:

0

10、from size - 分页查询

  ES中的记录数过多,可使用分页查询获取数据,使用 from、size 关键字:

# 分页查询 第一页
GET /items/_search
{
  "query": {"match_all": {}},
  "from": 0,
  "size": 2
}

# 第2页
GET /items/_search
{
  "query": {"match_all": {}},
  "from": 2,
  "size": 2
}

  分页查询数据:

0

11、_ids - 多id查询

  对记录的多个id进行查询:

# 多id查询
GET /items/_search
{
  "query": {
    "ids": {
      "values": ["33", "55"]
    }
  }
}

12、_source - 返回指定字段

  使用 _source 指定查询到的字段。

# 返回指定字段
GET /items/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["account_number","balance"]
}

  查询 account_number、balance 字段。

0
 
posted @ 2024-01-18 17:07  无虑的小猪  阅读(28)  评论(0编辑  收藏  举报