ES 查询练习demo

简介: 本文主要介绍部分es查询操作

POST /student/_doc
{
"std_id": 2,
"std_name": "guan yu",
"std_sex": "man",
"std_age": 20,
"create_time": "2021/05/27 11:37:00",
"update_time": "2021/05/27 11:37:00"
}

PUT /student/_doc/1
{
"std_id": 1,
"std_name": "zhao zi long",
"std_sex": "man",
"std_age": 18,
"create_time": "2021/05/27 11:37:00",
"update_time": "2021/05/27 11:37:00"
}

GET student/_doc/_search
{
"query":{

"match_all":{}

}
}

ES基础语法

默认不同分词之间连接为or 就是只要包含就行

GET student/_search
{
"query":{

"match": {
  "std_name": {
    "query": "guan zhao"
  }
}

}
}

指定为and 都要包含才会被查询出

GET student/_doc/_search
{
"query":{

"match":{
  "std_name":{
    "query":"guan yu",
    "operator":"and"
  }
}

}
}

短语匹配 都要包含且顺序要一致

GET student/_doc/_search
{
"query":{

"match_phrase":{
  "std_name":"zhao zi"
}

}
}

短语匹配 只是这个支持最后一个词项的前缀匹配

GET student/_doc/_search
{
"query":{

"match_phrase_prefix":{
  "std_name":"zhao zi lo"
}

}
}

在多个字段搜素关键字且支持通配符和权重指定(例如关键字在std_name^3出现的权重是std_age的三倍)

GET student/_search
{
"query": {

"multi_match":{
  "query":"zhao",
  "fields":["std_name","std_sex"]
}

}
}

common_terms query common_terms query提供了一种解决方案,它把 query 分词后的词项分成重要词项(低频词项)和不重要的词项(高频词,也就是之前的停用词)。

GET books/_search
{

"query": {
    "common": {
        "body": {
            "query": "nelly the elephant as a cartoon",
            "cutoff_frequency": 0.001,
            "low_freq_operator": "and"
        }
    }
}

}

term query 精确查询(不会被分词)通常用来查询某个标识字段例如,name userid。。。 而不是来查询会被分分词的text类型字段

GET student/_search
{
"query":{

"term":{
  "std_age":"18"
}

}
}

原理见上 不同点为可以指定多个搜索的关键词

GET student/_search
{
"query":{

"terms":{
  "std_age":[18,20]
}

}
}

range query 范围查询,只能使用一个字段过滤 18< age <=20

GET student/_search
{
"query":{

"range":{
  "std_age":{
    "gt":18,
    "lte":20
  }
}

}
}

exists query exists 查询会返回字段中至少有一个非空值的文档

GET student/_search
{
"query": {

"exists": {
  "field": "std_sex"
}

}
}

prefix query 前缀匹配查询 std-name以zh开头的doc

GET student/_search
{
"query": {

"prefix": {
    "std_name": "zh"
}

}
}

wildcard query 通配符查询

GET student/_search
{
"query": {

"wildcard": {
  "std_name":"zhao*"
}

}
}

regexp query 正则表达式

type query 根据类型查询文档

ids query 根据id查询文档

GET student/_search
{
"query": {

"ids": {
  "values": ["1","I1oSIoEB40FFYrLjwuXn"]
}

}
}

复合查询

bool 查询可以把任意多个简单查询组合在一起,使用 must、should、must_not、filter 选项来表示简单查询之间的逻辑,每个选项都可以出现 0 次到多次,它们的含义如下:

must 文档必须匹配 must 选项下的查询条件,相当于逻辑运算的 AND,且参与文档相关度的评分。

should 文档可以匹配 should 选项下的查询条件也可以不匹配,相当于逻辑运算的 OR,且参与文档相关度的评分。

must_not 与 must 相反,匹配该选项下的查询条件的文档不会被返回;需要注意的是,must_not 语句不会影响评分,它的作用只是将不相关的文档排除。

filter 和 must 一样,匹配 filter 选项下的查询条件的文档才会被返回,但是 filter 不评分,只起到过滤功能,与 must_not 相反。

GET student/_search
{
"query":{

"bool": {
  "filter": [
    {"range": {
      "std_age": {
        "gte": 18
      }
    }}
  ],
  "must": [
    {
      "match": {
      "std_name": "zhao"
    }
      

    }
  ]
}

}
}

boosting query 调整查询结果的权重评分

boosting 查询包括 positive、negative 和 negative_boost 三个部分,positive 中的查询评分保持不变,negative 中的查询会降低文档评分

GET student/_search
{
"query": {

"boosting": {
  "positive": {
    "match": {
      "std_sex": "man"
    }
  },
  "negative": {
  "range":{
    "std_age": {
      "lte": 19
    }
  }
  },
  "negative_boost": 0.2
}

}
}

constant_score query 包装一个 filter query,并返回匹配过滤器查询条件的文档,且它们的相关性评分都等于 boost 参数值

function_score 修改评分的方式

下面这条查询语句会返回 books 索引中的所有文档,文档的最大得分为 5,每个文档的得分随机生成,权重的计算模式为相乘模式。

GET student/_search
{
"query": {

"function_score": {
  "query": {
    "match_all": {}
  },
  "boost": "5",
  "random_score": {},
  "boost_mode": "multiply"
}

}
}

通过脚本 这里把 std_age 值的十分之一开方作为每个文档的得分

GET student/_search
{
"query": {

"function_score": {
  "query": {
    "match": {
      "std_name": "zhao"
    }
  },
  "script_score": {
    "inline": "Math.sqrt(doc['std_age'].value/10)"
  }
}

}
}

嵌套查询

nested query(嵌套查询)

文档中可能包含嵌套类型的字段,这些字段用来索引一些数组对象,每个对象都可以作为一条独立的文档被查询出来。

has_child query(有子查询)和 has_parent query(有父查询)

父子关系可以存在单个的索引的两个类型的文档之间。has_child 查询将返回其子文档能满足特定查询的父文档,而 has_parent 则返回其父文档能满足特定查询的子文档。

nested query 嵌套查询,每个字段也是一个对象

PUT /drivers
{

"mappings" : {
    "properties" : {
        "driver" : {
            "type" : "nested",
            "properties" : {
                "last_name" : {
                    "type" : "text"
                },
                "vehicle" : {
                    "type" : "nested",
                    "properties" : {
                        "make" : {
                            "type" : "text"
                        },
                        "model" : {
                            "type" : "text"
                        }
                    }
                }
            }
        }
    }
}

}

PUT /drivers/_doc/1
{
"driver" : {

    "last_name" : "McQueen",
    "vehicle" : [
        {
            "make" : "Powell Motors",
            "model" : "Canyonero"
        },
        {
            "make" : "Miller-Meteor",
            "model" : "Ecto-1"
        }
    ]
}

}

GET /drivers/_doc/1

has_child query

PUT /company
{

"mappings": {
    "branch": {},
    "employee": {
        "parent": { "type": "branch" }
    }
}

}

has_parent query

通过父文档查询子文档使用 has_parent 查询。比如,搜索哪些 employee 工作在 UK,查询命令如下:

这里employee 是 child type,branch 是 parent type,

GET company/employee/_search
{

"query": {
    "has_parent": {
        "parent_type": "branch",
        "query": {
            "match": { "country": "UK }
        }
    }
}

}

位置查询

PUT my_location
{
"mappings": {

"properties": {
  "location":{
    "type": "geo_point"
  }
}

}
}

POST my_location/_bulk
{"index":{"_id":2}}
{"text":"上海站","location":{"lat":31.256224,"lon":121.462311}}
{"index":{"_id":3}}
{"text":"五一广场","location":"POINT (121.460186 31.251281)"}
{"index":{"_id":4}}
{"text":"交通公园","location":"31.253531,121.473939"}
{"index":{"_id":5}}
{"text":"万业远景大厦","location":[121.448215,31.26229]}

geo_bounding_box query 两点确定一个矩形(连线为对角线)来查询

GET /my_location/_search
{
"query": {

"bool": {
  "must": {
    "match_all": {}
  },
  "filter": {
    "geo_bounding_box": {
      "location": {
        "top_left": {
          "lat": 31.265395,
          "lon": 121.444075
        },
        "bottom_right": {
          "lat": 31.253845,
          "lon": 121.468417
        }
      }
    }
  }
}

}
}

geo_distance 根据距离distance搜索 ,查询并排序

GET my_location/_search
{
"query": {

"bool": {
  "must": {
    "match_all": {}
  },
  "filter": {
    "geo_distance": {
      "distance": "600m",
      "distance_type": "arc",
      "_name": "optional_name",
      "location": {
        "lat": 31.256224,
        "lon": 121.462311
      }
    }
  }
}

},
"sort": [

{
  "_geo_distance": {
    "location": {
      "lat": 31.256224,
      "lon": 121.462311
    },
    "order": "desc",
    "unit": "m",
    "distance_type": "arc"
  }
}

]
}

相关文章
|
6月前
|
存储 缓存 Linux
Go Modules 介绍与基本操作(上)
Go Modules 介绍与基本操作
52 0
|
前端开发 API
ES 高级实战(四)查询 ES 数据
ES 高级实战(四)查询 ES 数据
1268 0
ES 高级实战(四)查询 ES 数据
|
1月前
|
存储 Unix 索引
ES常用查询命令
ES常用查询命令
44 2
|
6月前
|
存储 Go API
Go Modules 如何创建和发布 v2 及更高版本?
Go Modules 如何创建和发布 v2 及更高版本?
68 0
|
6月前
|
存储 Go 开发工具
GOPATH 模式怎么迁移至 Modules 模式?
GOPATH 模式怎么迁移至 Modules 模式?
31 0
|
6月前
|
Go API
Go Modules 介绍与基本操作(下)
Go Modules 介绍与基本操作(下)
33 0
|
8月前
es6如何使用padStart()和padEnd()方法
es6如何使用padStart()和padEnd()方法
59 0
|
8月前
|
前端开发 JavaScript
解决前端报错 Error: Cannot find module ‘xxx‘(包含 uniapp)
解决前端报错 Error: Cannot find module ‘xxx‘(包含 uniapp)
386 0
|
9月前
|
索引
ES5新增方法(一)
前言 今天和大家分享一下ES5中一些新增的方法。 一、数组方法 迭代(遍历)方法:forEach(),map(),filter(),some(),every() array.forEach(function(value,index,arr)) value:数组当前项的值 index:数组当前项的索引 arr:数组对象本身
|
10月前
|
JavaScript 前端开发
每天3分钟,重学ES6-ES12(六)ES7 ES8 新增内容
每天3分钟,重学ES6-ES12(六)ES7 ES8 新增内容
91 0