使用方法
URL查询:GET/索引/类型/_search?参数
DSL查询:GET/索引/类型/_search{}
#1.QueryString方式查询,查询所有q=* 排序sort 分页form size
GET /ems/emp/_search?q=*&sort=age:desc&size=3&from=1
#2.QueryDSL DSL特定领域的查询语言
#(1)查询所有
GET /ems/emp/_search
{
"query": {
"match_all": {}
}
}
#(2)查询所有 并排序
GET /ems/emp/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "desc"
},
"bir": {
"order": "desc"
}
}
]
}
#(3)分页查询
GET /ems/emp/_search
{
"query": {
"match_all": {}
},
"size":"3",
"from":"0",
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
#(4)指定字段 _source
GET /ems/emp/_search
{
"query": {
"match_all": {}
},
"_source": ["name","age","bir"]
}
#(5)term _基于关键词查询
GET ems/emp/_search
{
"query": {
"term": {
"content": {
"value": "spring"
}
}
}
}
1. type: 只有text 类型分词,keyword,integer,date等不分词
2. ES中默认使用的分词器是标准分词器strandard 中文拆成单字,英文拆成单词。
#(6)range范围查询
GET ems/emp/_search
{
"query": {
"range": {
"age": {
"gte": 5,
"lte": 20
}
}
}
}
#(7)前缀查询,基于关键词前缀prefix
GET ems/emp/_search
{
"query": {
"prefix": {
"address": {
"value": "北"
}
}
}
}
#(8)通配符查询,wildcard ,?匹配一个,*匹配多个
GET ems/emp/_search
{
"query": {
"wildcard": {
"address": {
"value": "北?"
}
}
}
}
#(9)多个id查询 ids
GET ems/emp/_search
{
"query": {
"ids": {
"values": ["spwyWXQByawye3YgOghx","uJwyWXQByawye3Yg3Qiz"]
}
}
}
#(10)模糊查询 fuzzy
#搜索关键词长度为2,不允许存在模糊0
#搜索关键词长度为3-5,允许存在一次模糊0-1
#搜索关键词长度大于5,允许最大2模糊
GET ems/emp/_search
{
"query": {
"fuzzy": {
"content": "elasticseoach"
}
}
}
#(11)布尔查询bool must(&&),shoud(||),must_not(!)
GET ems/emp/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"age": {
"value": "23"
}
}
},
{"term": {
"address": {
"value": "北京"
}
}}
]
}
}
}
#(12)高亮查询 highlight 对查询结果做二次渲染
GET ems/emp/_search
{
"query": {
"term": {
"content": "redis"
}
},
"highlight": {
"pre_tags": ["<span style='color:red'>"],
"post_tags": ["</span>"],
"fields": {
"content": {}
}
}
}
#(13)多个字段查询 multi_match
#如果搜索字段分词,先对query分词再搜索,否则直接搜索
GET ems/emp/_search
{
"query": {
"multi_match": {
"query": "redis",
"fields": ["address","content"]
}
}
}
#(14)多个字段分词查询 query_string
GET ems/emp/_search
{
"query": {
"query_string": {
"default_field": "content",
"query": "redis是一个开源框架"
}
}
}
GET ems/emp/_search
{
"query": {
"query_string": {
"query": "小北",
"fields": ["name","content"]
}
}
}