四、ElasticSearch-基础查询操作

1、插入部分数据

post /my_index_data/_doc/1
{
    "first_name" :  "tom1",
    "last_name" :   "Smith",
    "age" :         35
}


post /my_index_data/_doc/2
{
    "first_name" :  "tom2",
    "last_name" :   "Smith",
    "age" :         30
}

post /my_index_data/_doc/3
{
    "first_name" :  "tom3",
    "last_name" :   "Smith",
    "age" :         39
}

post /my_index_data/_doc/4
{
    "first_name" :  "tom4",
    "last_name" :   "Smith",
    "age" :         20
}

post /my_index_data/_doc/5
{
    "first_name" :  "tom5",
    "last_name" :   "Smith",
    "age" :         30
}

post /my_index_data/_doc/6
{
    "first_name" :  "tom6",
    "last_name" :   "Smith",
    "age" :         36
}


post /my_index_data/_doc/7
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         33
}

post /my_index_data/_doc/8
{
    "first_name" :  "Jane3",
    "last_name" :   "Smith",
    "age" :         29
}

2、查看所有数据

  • 查询语句
GET /my_index_data/_search
{
 "query": {
 "match_all": {}
 } 
}
# "query":这里的 query 代表一个查询对象,里面可以有不同的查询属性
# "match_all":查询类型,例如:match_all(代表查询所有), match,term , range 等等
# {查询条件}:查询条件会根据类型的不同,写法也有差异
  • 查询结果
{
  "took"【查询花费时间,单位毫秒】 : 2,
  "timed_out" 【是否超时】: false,
  "_shards" 【分片信息】: {
    "total" 【总数】: 1,
    "successful"【成功】 : 1,
    "skipped" 【忽略】: 0,
    "failed" 【失败】: 0
  },
  "hits" 【搜索命中结果】: {
    "total" 【搜索条件匹配的文档总数】: {
      "value"【总命中计数的值】 : 8,
      "relation" 【计数规则】: "eq"  # eq 表示计数准确, gte 表示计数不准确
    },
    "max_score" 【匹配度分值】 : 1.0,
    "hits" s【命中结果集合】: [
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "tom1",
          "last_name" : "Smith",
          "age" : 35
        }
      },
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "tom2",
          "last_name" : "Smith",
          "age" : 30
        }
      },
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "tom3",
          "last_name" : "Smith",
          "age" : 39
        }
      },
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "tom4",
          "last_name" : "Smith",
          "age" : 20
        }
      },
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "tom5",
          "last_name" : "Smith",
          "age" : 30
        }
      },
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "tom6",
          "last_name" : "Smith",
          "age" : 36
        }
      },
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "7",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "Jane",
          "last_name" : "Smith",
          "age" : 33
        }
      },
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "8",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "Jane3",
          "last_name" : "Smith",
          "age" : 29
        }
      }
    ]
  }
}

3、匹配查询

解释:match 匹配类型查询,会把查询条件进行分词,然后进行查询,多个词条之间是 or 的关系

GET /my_index_data/_search
{
 "query": {
 "match": {
   "first_name": "tom1" //查询条件
 }
 } 
}

#查询结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.8126745,
    "hits" : [
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.8126745,
        "_source" : {
          "first_name" : "I like tom1", //分词命中。
          "last_name" : "Smith",
          "age" : 30
        }
      },
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.6808895,
        "_source" : {
          "first_name" : "my name is tom1", #分词命中
          "last_name" : "Smith",
          "age" : 35
        }
      }
    ]
  }
}

注意:这里查询条件是tom1,命中两条数据记录,当我们输入tom的时候是不能命中,并不是我们数据库中的模糊搜索,当我们输入 I tom1的时候确定命中,ES在使用条件搜索的时候,使用了分词

#查询条件 tom
GET /my_index_data/_search
{
 "query": {
 "match": {
   "first_name": "tom"
 }
 } 
}

#查询结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}




------------------------------------------
#查询条件 I tom1
GET /my_index_data/_search
{
 "query": {
 "match": {
   "first_name": "I tom1" 分词为I 和 tom1
 }
 } 
}

{
  "took" : 36,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 2.2266572,
    "hits" : [
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 2.2266572,
        "_source" : {
          "first_name" : "I like tom1",
          "last_name" : "Smith",
          "age" : 30
        }
      },
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.6808895,
        "_source" : {
          "first_name" : "my name is tom1",
          "last_name" : "Smith",
          "age" : 35
        }
      }
    ]
  }
}

4、字段匹配查询

解释:multi_match match 类似,不同的是它可以在多个字段中查询。

#查询first_name和last_name数据中包含I tom1(注意I tom1分词之后的数据也能命中)的数据
get /my_index_data/_search
{
 "query": {
 "multi_match": {
 "query": "I tom1",
 "fields": ["first_name","last_name"]
  }
 } 
}


#结果
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.5507698,
    "hits" : [
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : 1.5507698,
        "_source" : {
          "first_name" : "tom6",
          "last_name" : "Smith tom1", #命中tom1
          "age" : 36
        }
      },
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.8588407,
        "_source" : {
          "first_name" : "I like tom1", #命中I和tom1
          "last_name" : "Smith",
          "age" : 30
        }
      },
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.7180705,
        "_source" : {
          "first_name" : "my name is tom1", #命中tom1
          "last_name" : "Smith",
          "age" : 35
        }
      }
    ]
  }
}

5、关键字精确查询

解释:term 查询,精确的关键词匹配查询,不对查询条件进行分词。

#不分词的时候查询
get /my_index_data/_search
{"query":{
  "term": {
    "first_name": {
      "value": "I tom1"
    }
  }
}}

#不能命中数据
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

#查询含有tom1的数据
get /my_index_data/_search
{"query":{
  "term": {
    "first_name": {
      "value": "tom1"
    }
  }
}}

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值