1.根据主键查询
curl -X GET /test/employee/1
2.全文查询
curl -X GET /test/employee/_search (默认返回前10条)
3.关键字查询(url查询)
curl -X GET /test/employee/_search?q=last_name:Smith (关键字段,全文检索,不区分大小写)
4.关键字查询(json格式查询)
curl -X GET /test/employee/_search
{
"query": {
"match": {
"about": "like"
}
}
}
5.查询匹配clim、rock(只要about字段包含,clim,rock都能命中查询,但是单词必须一致)
curl -X GET /test/employee/_search
{
"query": {
"match": {
"about": "clim rock"
}
}
}
6.短语搜索(精确匹配rock climbing,单词必须存在,且顺序一致)
curl -X GET /test/employee/_search
{
"query": {
"match_phrase": {
"about": "rock climbing"
}
}
}
7.多字段查询(并且高亮)
curl -X GET /test/employee/_search
{
"query": {
"multi_match": {
"query": "rock smith",
"fields": ["about", "last_name"]
}
},
"highlight": {
"fields": {
"last_name": {},
"about": {}
}
}
}
8.复杂查询
8.1 term精确匹配查询(相当于mysql中的=)
curl -X GET /test/employee/_search
{
"query": {
"filtered": {
"query": {
"match": {
"last_name": "smith"
}
},
"filter": {
"term": {
"age": 25
}
}
}
}
}
8.2 terms精确匹配查询(相当于mysql中的in)
curl -X GET /test/employee/_search
{
"query": {
"filtered": {
"query": {
"match": {
"last_name": "smith"
}
},
"filter": {
"terms": {
"age": [
"25",
"32"
]
}
}
}
}
}
8.3 range 指定范围查找(相当于mysql中的>,<,>=,<=)
curl -X GET /test/employee/_search
{
"query": {
"filtered": {
"query": {
"match": {
"last_name": "smith"
}
},
"filter": {
"range": {
"age": {
"gt": 10
}
}
}
}
}
}
8.4 exists 和 missing(判断字段是否存在)
curl -X GET /test/employee/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"exists": {
"field": "age"
}
}
}
}
}
curl -X GET /test/employee/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"missing": {
"field": "music"
}
}
}
}
}
8.5 bool 过滤可以用来合并多个过滤条件查询结果的布尔逻辑,它包含一下操作符:
must :: 多个查询条件的完全匹配,相当于 and。
must_not :: 多个查询条件的相反匹配,相当于 not。
should :: 至少有一个查询条件匹配, 相当于 or。
GET /test/employee/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{"range": {
"age": {
"gt": 30
}
}},
{
"term": {
"last_name": "smith"
}
}
]
}
}
}
}
}