课程大纲
需求:搜索标题中包含java的帖子,同时呢,如果标题中包含hadoop
或elasticsearch
就优先搜索出来,同时呢,如果一个帖子包含java hadoop
,一个帖子包含java elasticsearch
,包含hadoop
的帖子要比elasticsearch
优先搜索出来
知识点,搜索条件的权重,boost
,可以将某个搜索条件的权重加大,此时当匹配这个搜索条件和匹配另一个搜索条件的document
,计算relevance score
时,匹配权重更大的搜索条件的document
,relevance score
会更高,当然也就会优先被返回回来
默认情况下,搜索条件的权重都是一样的,都是1
GET /forum/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "blog"
}
}
],
"should": [
{
"match": {
"title": {
"query": "java"
}
}
},
{
"match": {
"title": {
"query": "hadoop"
}
}
},
{
"match": {
"title": {
"query": "elasticsearch"
}
}
},
{
"match": {
"title": {
"query": "spark",
"boost": 5
}
}
}
]
}
}
}
示例:
GET /forum/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "java"
}
}
],
"should": [
{
"match": {
"title": {
"query": "hadoop",
"boost": 6
}
}
},
{
"match": {
"title": {
"query": "elasticsearch"
}
}
},
{
"match": {
"title": {
"query": "flink",
"boost": 5
}
}
}
]
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 6.547991,
"hits" : [
{
"_index" : "forum",
"_type" : "_doc",
"_id" : "1",
"_score" : 6.547991,
"_source" : {
"articleID" : "XHDK-A-1293-#fJ3",
"userID" : 1,
"hidden" : false,
"postDate" : "2017-01-01",
"tag" : [
"java",
"hadoop"
],
"tag_cnt" : 2,
"view_cnt" : 30,
"title" : "this is java and hadoop blog"
}
},
{
"_index" : "forum",
"_type" : "_doc",
"_id" : "6",
"_score" : 5.020876,
"_source" : {
"articleID" : "KDKE-B-9947-#kL1",
"userID" : 3,
"hidden" : false,
"postDate" : "2018-01-02",
"tag" : [
"java",
"flink"
],
"tag_cnt" : 5,
"view_cnt" : 50,
"title" : "this is java, flink blog"
}
},
{
"_index" : "forum",
"_type" : "_doc",
"_id" : "4",
"_score" : 2.1105356,
"_source" : {
"articleID" : "QQPX-R-3956-#aD8",
"userID" : 2,
"hidden" : true,
"postDate" : "2017-01-02",
"tag" : [
"java",
"elasticsearch"
],
"tag_cnt" : 2,
"view_cnt" : 40,
"title" : "this is java and elasticsearch blog"
}
},
{
"_index" : "forum",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0946829,
"_source" : {
"articleID" : "KDKE-B-9947-#kL5",
"userID" : 1,
"hidden" : false,
"postDate" : "2017-01-02",
"tag" : [
"java"
],
"tag_cnt" : 1,
"view_cnt" : 50,
"title" : "this is java blog"
}
}
]
}
}