一,simple string query和string query
string query 中query 字符串中可以使用 and or not等逻辑连接词,simple string query 中不行,会将其作为普通字符串处理。
在simple string query中可以指定"default_operator"指定逻辑运算符,也可以字query 字符串中用特殊符号表示逻辑运算关系,如 “beautiful +mind” 表示"beautiful and mind"。
# 可以在query字符串中指定查询字段,AND要全大写,default_field可以省略
GET movies/_search
{
"query": {
"query_string": {
"default_field": "title",
"query": "title:Beautiful AND Mind"
}
}
}
# "query": "Beautiful +Mind",
GET movies/_search
{
"query": {
"simple_query_string": {
"query": "Beautiful -Mind",
"fields": ["title"]
}
}
}
一,聚合查询
1,bucket
类似于sql的group
2,metric
类似于sql的count
POST kibana_sample_data_flights/_search
{
"aggs": {
"ml": {
"terms": {
"field":"DestCountry"
},
"aggs": {
"ag_et": {
"avg": {
"field":"AvgTicketPrice"
}
},
"max_t":{
"max":{
"field":"AvgTicketPrice"
}
}
}
}
}
}
二,结构化查询
对数字、时间、数据字典式文本的查询称之为结构化查询。
1,term查询
termd查询,对查询条件不分词,对关键词进行模糊查询
POST lcy_test/_search
{
"query": {
"term": {
"name": {
"value": "lcy"
}
}
}
}
不算分filter查询,提高效率
filter缓存原理bitset
POST lcy_test/_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"name": "lcy"
}
},
"boost": 1.2
}
}
}
2,range查询
数值的range查询。
POST kibana_sample_data_ecommerce/_search
{
"query": {
"range": {
"taxful_total_price": {
"gte": 10,
"lte": 20
}
}
},
"from":1,
"size": 2
}
时间的range查询。
POST kibana_sample_data_ecommerce/_search
{
"query": {
"range": {
"order_date": {
"gte": "now-1M"
}
}
},
"from":1,
"size": 2
}
exists查询。
// exists查询
POST kibana_sample_data_ecommerce/_search
{
"query": {
"exists": {
"field": "name"
}
}
}
多值查询。用关键字去查一个有多个值的字段时,只用多值字段包含关键字,就能匹配成功。
三,全文搜索
先分词,在查询,再算分
四,bool查询
POST blog/_bulk
{"index":{"_id":1}}
{"content":"Apple employee loves apple juice and apple pie"}
{"index":{"_id":2}}
{"content":"apple mac"}
{"index":{"_id":3}}
{"content":"apple phone and apple pad"}
bool查询
GET blog/_search
POST blog/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"_id": {
"value": "1"
}
}
}
]
}
}
}
must 和must not查询
POST blog/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"content": "apple"
}
}
],
"must_not": [
{
"match": {
"content": "pie"
}
}
]
}
}
}
bool boosting查询,调整算分参数
POST blog/_search
{
"query": {
"boosting": {
"positive": {
"match": {
"content": "apple"
}
},
"negative": {
"match": {
"content": "pie"
}
},
"negative_boost": 0.2
}
}
}
单字段多值查询 disjuncition max query 、 tie breaker
// disjunction query
POST blog/_search
{
"query": {
"dis_max": {
"queries": [
{"match":{
"title":"cat love"
}
} ,
{"match":{
"title":"cat love"
}
}
],
"tie_breaker": 0.7
}
}
}
多字段单值查询 disjuncition max query 、 tie breaker
// disjunction query
POST blog/_search
{
"query": {
"dis_max": {
"queries": [
{"match":{
"title":"cat love"
}
} ,
{"match":{
"title":"cat love"
}
}
],
"tie_breaker": 0.7
}
}
}
多字段单值查询 multi query
multi query 分为best_fields,most_fields,cross_fields
POST blog/_search
{
"explain": true,
"query": {
"multi_match": {
"query": "love cat",
"type": "best_fields",
"fields": ["title","content"]
}
}
}