文章目录
- 一,url search
- 二,url search
- 三,不指定字段
- 四,指定字段
- 五,phase query
- 六,keyword query
- 七,term_query
- 八,逻辑查询 AND or %2b
- 九,逻辑查询 NOT or %2b
- 十,数学符号
- 十一,范围查询
- 十二,通配符查询
- 十三,模糊查询
- 十四,排序 url sort
- 十五, url 分页
- 十六,脚本字段,将多个字段拼接
- 十七,query_string
- 十八, simple_query_string
- 十九,控制查询打分 boosting
- 二十,bool查询
- 二十,通过boost增加查询字段的权重
- 二十一,dis_max_query + tie_break
- 二十二,multi_query
- 二十三
- 二十四 function_score 控制评分
一,url search
// url search
GET /nba,test1/_search
二,url search
GET /test1/_search
三,不指定字段
GET /nba,test1/_search?q=老鹰&from=2&size=1
{
"profile":"true"
}
四,指定字段
GET /nba,test1/_search?q=teamName:老鹰&from=2&size=1
{
"profile":"true"
}
五,phase query
关键词是多个单词组成的词语构成时,关键字要加双引号 ""
GET /nba,test1/_search?q=teamName:"老鹰"&from=2&size=1
{
"profile":"true"
}
六,keyword query
和上面phase query的效果一样,由于查询关键字是两个单词,要加括号
GET /nba,test1/_search?q=displayNameEn.keyword:(Vince Carter)&from=0&size=3
{
"profile":"true"
}
七,term_query
当关键词是一个短语时,关键字要加括号才会当作一个词去匹配
GET /nba,test1/_search?q=displayNameEn:(Vince Carter)&from=0&size=3
{
"profile":"true"
}
八,逻辑查询 AND or %2b
GET /nba,test1/_search?q=displayNameEn:(Vince AND Carter)&from=0&size=3
{
"profile":"true"
}
九,逻辑查询 NOT or %2b
GET /nba,test1/_search?q=displayNameEn:(Vince NOT Carter)&from=0&size=3
{
"profile":"true"
}
十,数学符号
GET /nba,test1/_search?q=age:>=38
十一,范围查询
注意左边是大括号,右边是中括号
GET /nba,test1/_search?q=age:{30 TO 31]
{
"profile":"true"
}
十二,通配符查询
GET /nba,test1/_search?q=displayNameEn:V*&from=0&size=3
{
"profile":"true"
}
十三,模糊查询
近似度查询1:单词写错也能查,注意后面的波浪线符号**
GET /nba,test1/_search?q=code:vincent_edwads1~2
{
"profile":"true"
}
近似度查询2:phase查询,中间缺单词也能查,注意后面的波浪线符号
GET /nba,test1/_search?q=displayAffiliation:"Purdue States"~1
{
"profile":"true"
}
十四,排序 url sort
POST nba/_search
{
"sort":["teamName"],
"query": {
"match_all": {
}
}
}
十五, url 分页
POST nba/_search
{
"_source":["jerseyNo"],
"query": {
"match_all": {
}
}
, "from": 0
, "size": 20
}
十六,脚本字段,将多个字段拼接
POST nba/_search
{
"script_fields": {
"new_field":{
"script":{
"lang":"painless",
"source": "doc['teamName'].value + 'd'"
}
}
},
"query": {
"match_all": {}
}
}
十七,query_string
将AND OR NOT 写在查询语句中,实现逻辑查询
POST nba/_search
{
"query":{
"query_string": {
"default_field": "teamName",
"query": "老 AND 鹰"
}
}
}
十八, simple_query_string
相对于query_string,只支持一个操作费,或者是and,或者是or
POST nba/_search
{
"query":{
"simple_query_string": {
"fields": ["teamName"],
"query": "湖人",
"default_operator": "AND"
}
}
}
十九,控制查询打分 boosting
GET nba/_search
{
"explain": true,
"profile": "true",
"query": {
"boosting": {
"negative": {
"match": {
"displayNameEn": "Jevon"
}
},
"positive": {
"match": {
"displayNameEn": "Vince Carter"
}
},
"negative_boost": 0.2
}
}
}
二十,bool查询
bool查询包括 must,must not,should,filter
GET nba/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"teamName.keyword": {
"value": "老鹰"
}
}
}
],
"filter": {
"range": {
"age": {
"gte": 35,
"lte": 42
}
}
}
}
}
}
二十,通过boost增加查询字段的权重
GET nba/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"teamName.keyword": {
"value": "老鹰"
}
}
},
{
"term": {
"teamName.keyword": {
"value": "湖人",
"boost": 12
}
}
}
],
"filter": {
"range": {
"age": {
"gte": 35,
"lte": 42
}
}
}
}
}
}
二十一,dis_max_query + tie_break
存在多个查询条件时,会将多个查询条件匹配到的文档的评分进行平均。
dis_max_query会阻止这种平均,将评分最高的文档排在前面。
tie_break会对??????
GET nba/_search
{
"query": {
"dis_max": {
"tie_breaker": 0.7,
"boost": 1.2,
"queries": [
{
"match": {
"teamName": "湖人"
}
} ,
{
"match": {
"playYear": "6"
}
}
]
}
}
}
二十二,multi_query
// multi query,一个关键字在多个字段出现的查询
GET nba/_search
{
"query": {
"multi_match": {
"type": "best_fields",
"query": "雷吉",
"fields": ["teamName","displayName"]
}
}
}
// multi query,most_fields + operator
GET nba/_search
{
"query": {
"multi_match": {
"type": "most_fields",
"operator": "and",
"query": "雷吉",
"fields": ["teamName","displayName"]
}
}
}
// multi query,cross_fields + operator
GET nba/_search
{
"query": {
"multi_match": {
"type": "cross_fields",
"operator": "and",
"query": "雷吉",
"fields": ["teamName","displayName"]
}
}
}
二十三
// 模板
POST _scripts/nba
{
"script":{
"lang": "mustache",
"source": {
"_source":[
"teamName",
"cityName"
],
"size":20,
"query":{
"multi_match":{
"query":"{{q}}",
"fields":["teamName","cityName"]
}
}
}
}
}
GET _scripts/nba
GET nba/_search/template
{
"id":"nba",
"params": {
"q":"湖人"
}
}
二十四 function_score 控制评分
GET nba/_search
{
"query": {
"function_score": {
"query": {
"multi_match": {
"query": "湖人",
"fields": ["teamName"]
}
},
"field_value_factor": {
"field": "age",
"modifier": "log1p"
},
"boost_mode": "multiply",
"max_boost": 10
}
}
}