五、ElasticSearch-高级查询操作一

上一章我们介绍了ES的基础查询操作四、ElasticSearch-基础查询操作,下面我们来介绍ES的高级查询操作

1、多关键字精准查询

  1. terms 查询和 term 查询一样,但它允许你指定多值进行匹配
  2. 如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件,类似于mysql中的in
#查询数据中frist_name中包含name或者like或者tom2的数据
get /my_index_data/_search
{"query":{
  "terms": {
    "first_name": ["name","like","tom3"]
  }
}}

#查询结果
{
  "took" : 36,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "tom3", #匹配tom3
          "last_name" : "Smith",
          "age" : 39
        }
      },
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "my name is tom1",#匹配name
          "last_name" : "Smith",
          "age" : 35
        }
      },
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "I like tom1",#匹配like
          "last_name" : "Smith",
          "age" : 30
        }
      }
    ]
  }
}

2、指定查询字段

  1. 查询指定字段相当于mysql中不使用*查询所有字段,而是查询指定字段
#查询指定字段的数据,这里只查询frist_name和age字段,默认情况下查询的是source中所有的字段
get /my_index_data/_search
{
  "_source":["first_name","age"],
  "query":{
  "terms": {
    "first_name": ["name","like","tom3"]
  }
 }
}

#查询结果 结果列表中只出现了frist_name和age字段
{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "tom3", 
          "age" : 39
        }
      },
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "my name is tom1",
          "age" : 35
        }
      },
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "I like tom1",
          "age" : 30
        }
      }
    ]
  }
}

3、过滤字段

  1. includes:来指定想要显示的字段
  2. excludes:来指定不想要显示的字段
#查询字段中包括first_name和age
get /my_index_data/_search
{
  "_source":{"includes":["first_name","age"]},
  "query":{
  "terms": {
    "first_name": ["name","like"]
  }
 }
}
#####结果显示
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "my name is tom1",
          "age" : 35
        }
      },
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "I like tom1",
          "age" : 30
        }
      }
    ]
  }
}


#查询结果中除了first_name和age之外的元素
get /my_index_data/_search
{
  "_source":{"excludes":["first_name","age"]},
  "query":{
  "terms": {
    "first_name": ["name","like","tom3"]
  }
 }
}

#查询结果
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "last_name" : "Smith" #除了 frist_name和age之外只有last_name这个字段
        }
      },
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "last_name" : "Smith"
        }
      }
    ]
  }
}

4、组合查询

  1. `bool` 把各种其它查询通过 `must` (必须 )、 `must_not` (必须不)、 `should` (应该)的方
    式进行组合
#查询frist_name为tom1 age为30 或者 last_name = 12
get /my_index_data/_search
{
  "query":{
  "bool": {
    "must": [{
      "match":{
      "first_name":"tom1"
    }}
    ],
    "must_not": [
      {"match": {
        "age": "30"
      }}
      ],
      "should": [
        {"match": {
          "last_name": "12"
        }}
      ]
  }
 }
}

#查询结果
{
  "took" : 38,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.8638863,
    "hits" : [
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.8638863,
        "_source" : {
          "first_name" : "my name is tom1",
          "last_name" : "Smith",
          "age" : 35
        }
      }
    ]
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值