| 上一章我们介绍了ES的基础查询操作四、ElasticSearch-基础查询操作,下面我们来介绍ES的高级查询操作 1、多关键字精准查询terms 查询和 term 查询一样,但它允许你指定多值进行匹配如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件,类似于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、指定查询字段查询指定字段相当于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、过滤字段includes:来指定想要显示的字段 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、组合查询 
  
   `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
        }
      }
    ]
  }
}
 |