0. 查询所有(match_all)
match_all关键字: 返回索引中的全部文档
GET /ems/emp/_search
{
"query": { "match_all": {} }
}
1. 查询结果中返回指定条数(size)
size 关键字: 指定查询结果中返回指定条数。 默认返回值10条
GET /ems/emp/_search
{
"query": { "match_all": {} },
"size": 1
}
2. 分页查询(from)
from 关键字: 用来指定起始返回位置,和size关键字连用可实现分页效果
GET /ems/emp/_search
{
"query": {"match_all": {}},
"sort": [
{
"age": {
"order": "desc"
}
}
],
"size": 2,
"from": 1
}
3. 查询结果中返回指定字段(_source)
_source 关键字: 是一个数组,在数组中用来指定展示那些字段
GET /ems/emp/_search
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}
4. 关键词查询(term)
term 关键字: 用来使用关键词查询
GET /ems/emp/_search
{
"query": {
"term": {
"address": {
"value": "北京"
}
}
}
}
NOTE1: 通过使用term查询得知ES中默认使用分词器为标准分词器(StandardAnalyzer),标准分词器对于英文单词分词,对于中文单字分词。
NOTE2: 通过使用term查询得知,在ES的Mapping Type 中 keyword , date ,integer, long , double , boolean or ip 这些类型不分词,只有text类型分词。
5. 范围查询(range)
range 关键字: 用来指定查询指定范围内的文档
GET /ems/emp/_search
{
"query": {
"range": {
"age": {
"gte": 8,
"lte": 30
}
}
}
}
6. 前缀查询(prefix)
prefix 关键字: 用来检索含有指定前缀的关键词的相关文档
GET /ems/emp/_search
{
"query": {
"prefix": {
"content": {
"value": "redis"
}
}
}
}
7. 通配符查询(wildcard)
wildcard 关键字: 通配符查询 ? 用来匹配一个任意字符 * 用来匹配多个任意字符
GET /ems/emp/_search
{
"query": {
"wildcard": {
"content": {
"value": "re*"
}
}
}
}
8. 多id查询(ids)
ids 关键字 : 值为数组类型,用来根据一组id获取多个对应的文档
GET /ems/emp/_search
{
"query": {
"ids": {
"values": ["lg5HwWkBxH7z6xax7W3_","lQ5HwWkBxH7z6xax7W3_"]
}
}
}
9. 模糊查询(fuzzy)
fuzzy 关键字: 用来模糊查询含有指定关键字的文档
GET /ems/emp/_search
{
"query": {
"fuzzy": {
"content":"spring"
}
}
}
fuzzy 模糊查询 最大模糊错误 必须在0-2之间
搜索关键词长度为 2 不允许存在模糊 0
搜索关键词长度为3-5 允许一次模糊 0 1
搜索关键词长度大于5 允许最大2模糊
10. 布尔查询(bool)
bool 关键字: 用来组合多个条件实现复杂查询
must: 相当于&& 同时成立
should: 相当于|| 成立一个就行
must_not: 相当于! 不能满足任何一个
filter: 过滤出符合条件的
GET /ems/emp/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"age": {
"gte": 0,
"lte": 30
}
}
}
],
"must_not": [
{
"wildcard": {
"content": {
"value": "redi?"
}
}
}
],
"filter": {
"term": {
"hobby": "网吧"
}
}
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
11. 高亮查询(highlight)
highlight 关键字: 可以让符合条件的文档中的关键词高亮
GET /ems/emp/_search
{
"query": {
"term": {
"content": {
"value": "redis"
}
}
},
"highlight": {
"fields": {
"*": {}
}
}
}
自定义高亮html标签: 可以在highlight中使用
pre_tags
和post_tags
GET /ems/emp/_search
{
"query":{
"term":{
"content":"框架"
}
},
"highlight": {
"pre_tags": ["<span style='color:red'>"],
"post_tags": ["</span>"],
"fields": {
"*":{}
}
}
}
多字段高亮 使用
require_field_match
开启多个字段高亮
GET /ems/emp/_search
{
"query":{
"term":{
"content":"框架"
}
},
"highlight": {
"pre_tags": ["<span style='color:red'>"],
"post_tags": ["</span>"],
"require_field_match":false,
"fields": {
"*":{}
}
}
}
12. 多字段查询(multi_match
,会对要查询的词先进行分词,然后再去索引库查找符合的数据)
GET /ems/emp/_search
{
"query": {
"multi_match": {
"query": "中国",
"fields": ["name","content"] #这里写要检索的指定字段
}
}
}
这里推荐在字段中写可以分词的字段
13. 多字段分词查询(query_string
,好处是可以对你要查询的词进行分词处理,然后到索引库去匹配)
GET /dangdang/book/_search
{
"query": {
"query_string": {
"query": "中国声音",
"analyzer": "ik_max_word",
"fields": ["name","content"]
}
}
}
14.匹配查询(match)
如果查询的字段分词,那就将查询的条件也分词去查 如果查询的字段是不分词的,那么查询的时候就是不分词的了
GET ems/_search
{
"query": {
"match": {
"name": "贺宇航"
}
}
}
15.实现单独使用filter的功能(constant_score)
不需要匹配查询,只过滤数据。因为filter不能单独使用,使用constant_score关键字就能单独使用,实现过滤数据的效果
GET /ems/_search
{
"query": {
"constant_score" : {
"filter" : {
"term" : { "user" : "kimchy"}
},
"boost" : 1.2 #一个Float类型的数,用于指定匹配成功的文档的相关度分数。默认值为1.0
}
}
}