你不知道的Elasticsearch检索
1.SearchAPI
es支持两种基本方式检索
1.通过使用rest request uri 发送搜索参数(uri+检索参数)
#查询所有信息,根据用户编号(默认10条记录)
GET bank/_search?q=*&sort=account_number:asc
2.通过使用rest request body 来发送他们(uri+请求体)
GET bank/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"account_number": "asc"
},
{
"balance":"desc"
}
]
}
2.Query DSL
#from,size分页,_source返回部分字段
GET bank/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"balance": {
"order": "desc"
}
}
],
"from": 0,
"size": 5,
"_source": ["balance","firstname"]
}
3.match匹配
#全文检索
GET bank/_search
{
"query": {
"match": {
"address": "kings"
}
}
}
4.match_phrase短语匹配
GET bank/_search
{
"query": {
"match_phrase": {
"address": "mill lane"
}
}
}
#短语匹配与关键字匹配,address.keyword精确匹配,match_phrase会进行分词
GET bank/_search
{
"query": {
"match": {
"address.keyword": "789 Madison"
}
}
}
GET bank/_search
{
"query": {
"match_phrase": {
"address": "789 Madison"
}
}
}
5.multi_match多字段匹配
#"address","city"字段中包含mill或者movicn(支持分词)
GET bank/_search
{
"query": {"multi_match": {
"query": "mill movicn",
"fields": ["address","city"]
}}
}
6.bool复核查询
#must必须满足,must_not必须不满足,should有没有都可以
GET bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"gender": "M"
}
},
{
"match": {
"address": "mill"
}
}
],
"must_not": [
{"match": {
"age": "28"
}}
],
"should": [
{"match": {
"lastname": "Wallace"
}}
]
}
}
}
7.filter结果过滤
#不会计算相关性得分(过滤最后结果)
GET bank/_search
{
"query": {
"bool": {
"filter": {
"range": {
"age": {
"gte": 18,
"lte": 30
}
}
}
}
}
}
8.term(非text(字符串)字段)
#等同于match
GET bank/_search
{
"query": {
"term": {
"age": "28"
}
}
}