目录
filter过滤查询
单条件过滤
GET /movie/_search
{
"query":{
"bool":{
"filter":{
"term":{"title":"steve"}
}
}
}
}
多条件过滤
GET /movie/_search
{
"query":{
"bool":{
"filter":[
{"term":{"title":"steve"}},
{"term":{"cast.name":"gaspard"}},
{"range": { "release_date": { "lte": "2015/01/01" }}},
{"range": { "popularity": { "gte": "25" }}}
]
}
},
"sort":[
{"popularity":{"order":"desc"}}
]
}
带match打分的的filter
GET /movie/_search
{
"query":{
"bool":{
"must": [
{ "match": { "title": "Search" }},
{ "match": { "tagline": "Elasticsearch" }}
],
"filter":[
{"term":{"title":"steve"}},
{"term":{"cast.name":"gaspard"}},
{"range": { "release_date": { "lte": "2015/01/01" }}},
{"range": { "popularity": { "gte": "25" }}}
]
}
}
}
返回0结果
GET /movie/_search
{
"query":{
"bool":{
"should": [
{ "match": { "title": "Search" }},
{ "match": { "tagline": "Elasticsearch" }}
],
"filter":[
{"term":{"title":"steve"}},
{"term":{"cast.name":"gaspard"}},
{"range": { "release_date": { "lte": "2015/01/01" }}},
{"range": { "popularity": { "gte": "25" }}}
]
}
}
}
有结果,但是返回score为0,因为bool中若有filter的话,即便should都不满足,只是返回为0分而已修改为
GET /movie/_search
{
"query":{
"bool":{
"should": [
{ "match": { "title": "life" }},
{ "match": { "tagline": "Elasticsearch" }}
],
"filter":[
{"term":{"title":"steve"}},
{"term":{"cast.name":"gaspard"}},
{"range": { "release_date": { "lte": "2015/01/01" }}},
{"range": { "popularity": { "gte": "25" }}}
]
}
}
}
优秀的搜索引擎必备
查全率:正确的结果有n个,查询出来正确的有m 则 m/n
查准率:查出的n个文档有m个正确,则m/n
两者都需要提高,但一般不可兼得,可以通过调整排序位置,将正确的结果排在上面以提高用户体验
function score自定义打分
GET /movie/_search
{
"query":{
"function_score": {
//原始查询得到oldscore
"query": {
"multi_match":{
"query":"steve job",
"fields":["title","overview"],
"operator": "or",
"type":"most_fields"
}
},
"functions": [
{"field_value_factor": {
"field": "popularity", //对应要处理的字段
"modifier": "log2p", //将字段值+2后,计算对数
"factor": 10 //字段预处理*10
}
}
],
"score_mode": "sum", //不同的field value之间的得分相加
"boost_mode": "sum" //最后在与old value相加
}
}
}