接着…
为帖子数据增加content字段
POST /post/_doc/_bulk
{ "update": { "_id": "1"} }
{ "doc" : {"content" : "i like to write best elasticsearch article"} }
{ "update": { "_id": "2"} }
{ "doc" : {"content" : "i think java is the best programming language"} }
{ "update": { "_id": "3"} }
{ "doc" : {"content" : "i am only an elasticsearch beginner"} }
{ "update": { "_id": "4"} }
{ "doc" : {"content" : "elasticsearch and hadoop are all very good solution, i am a beginner"} }
{ "update": { "_id": "5"} }
{ "doc" : {"content" : "spark is best big data solution based on scala ,an programming language similar to java"} }
搜索title或content中包含java或solution的帖子
GET /post/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "java solution"
}
},
{
"match": {
"content": "java solution"
}
}
]
}
}
}
结果如下:
{
"took" : 15,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.6075432,
"hits" : [
{
"_index" : "post",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.6075432,
"_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",
"content" : "i think java is the best programming language"
}
},
{
"_index" : "post",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.3448961,
"_source" : {
"articleID" : "QQPX-R-3956-#aD8",
"userID" : 2,
"hidden" : true,
"postDate" : "2017-01-02",
"tag" : [
"java",
"elasticsearch"
],
"tag_cnt" : 2,
"view_cnt" : 80,
"title" : "this is java, elasticsearch, hadoop blog",
"content" : "elasticsearch and hadoop are all very good solution, i am a beginner"
}
},
{
"_index" : "post",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.32969955,
"_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 elasticsearch blog",
"content" : "i like to write best elasticsearch article"
}
}
]
}
}
best fields策略,dis_max
best fields策略,就是说,搜索到的结果,应该是某一个field中匹配到了尽可能多的关键词,被排在前面;而不是尽可能多的field匹配到了少数的关键词,排在了前面
dis_max语法,直接取多个query中,分数最高的那一个query的分数即可
GET /post/_search
{
"query": {
"dis_max": {
"queries": [
{
"match": {
"title": "java solution"
}
},
{
"match": {
"content": "java solution"
}
}
]
}
}
}