Elasticsearch进阶笔记第三篇

228 阅读2分钟

Elasticsearch高手进阶篇(5)

结构化搜索_在案例中实战使用terms搜索多个值以及多值搜索结果优化

  • term: {"field": "value"}
  • terms: {"field": ["value1", "value2"]}

sql中的in

 select * from tbl where col in ("value1", "value2")

1、为帖子数据增加tag字段

 POST /waws/article/_bulk
 { "update": { "_id": "1"} }
 { "doc" : {"tag" : ["java", "hadoop"]}}
 { "update": { "_id": "2"} }
 { "doc" : {"tag" : ["java"]}}
 { "update": { "_id": "3"} }
 { "doc" : {"tag" : ["hadoop"]}}
 { "update": { "_id": "4"} }
 { "doc" : {"tag" : ["java", "elasticsearch"]}}

2、搜索articleID为KDKE-B-9947-#kL5或QQPX-R-3956-#aD8的帖子,搜索tag中包含java的帖子

 GET /waws/article/_search 
 {
   "query": {
     "constant_score": {
       "filter": {
         "terms": {
           "articleID.keyword": [
             "KDKE-B-9947-#kL5",
             "QQPX-R-3956-#aD8"
           ]
         }
       }
     }
   }
 }
 
 {
   "took": 2,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 2,
     "max_score": 1,
     "hits": [
       {
         "_index": "waws",
         "_type": "article",
         "_id": "2",
         "_score": 1,
         "_source": {
           "articleID": "KDKE-B-9947-#kL5",
           "userID": 1,
           "hidden": false,
           "postDate": "2017-01-02",
           "tag": [
             "java"
           ]
         }
       },
       {
         "_index": "waws",
         "_type": "article",
         "_id": "4",
         "_score": 1,
         "_source": {
           "articleID": "QQPX-R-3956-#aD8",
           "userID": 2,
           "hidden": true,
           "postDate": "2017-01-02",
           "tag": [
             "java",
             "elasticsearch"
           ]
         }
       }
     ]
   }
 }
 
 GET /waws/article/_search
 {
     "query" : {
         "constant_score" : {
             "filter" : {
                 "terms" : { 
                     "tag" : ["java"]
                 }
             }
         }
     }
 }
 
 {
   "took": 1,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 3,
     "max_score": 1,
     "hits": [
       {
         "_index": "waws",
         "_type": "article",
         "_id": "2",
         "_score": 1,
         "_source": {
           "articleID": "KDKE-B-9947-#kL5",
           "userID": 1,
           "hidden": false,
           "postDate": "2017-01-02",
           "tag": [
             "java"
           ]
         }
       },
       {
         "_index": "waws",
         "_type": "article",
         "_id": "4",
         "_score": 1,
         "_source": {
           "articleID": "QQPX-R-3956-#aD8",
           "userID": 2,
           "hidden": true,
           "postDate": "2017-01-02",
           "tag": [
             "java",
             "elasticsearch"
           ]
         }
       },
       {
         "_index": "waws",
         "_type": "article",
         "_id": "1",
         "_score": 1,
         "_source": {
           "articleID": "XHDK-A-1293-#fJ3",
           "userID": 1,
           "hidden": false,
           "postDate": "2017-01-01",
           "tag": [
             "java",
             "hadoop"
           ]
         }
       }
     ]
   }
 }

3、优化搜索结果,仅仅搜索tag只包含java的帖子

 POST /waws/article/_bulk
 { "update": { "_id": "1"} }
 { "doc" : {"tag_cnt" : 2} }
 { "update": { "_id": "2"} }
 { "doc" : {"tag_cnt" : 1} }
 { "update": { "_id": "3"} }
 { "doc" : {"tag_cnt" : 1} }
 { "update": { "_id": "4"} }
 { "doc" : {"tag_cnt" : 2} }

搜索数据

 GET /waws/article/_search
 {
   "query": {
     "constant_score": {
       "filter": {
         "bool": {
           "must": [
             {
               "term": {
                 "tag_cnt": 1
               }
             },
             {
               "terms": {
                 "tag": ["java"]
               }
             }
           ]
         }
       }
     }
   }
 }
 
 {
   "took": 1,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 1,
     "max_score": 1,
     "hits": [
       {
         "_index": "waws",
         "_type": "article",
         "_id": "2",
         "_score": 1,
         "_source": {
           "articleID": "KDKE-B-9947-#kL5",
           "userID": 1,
           "hidden": false,
           "postDate": "2017-01-02",
           "tag": [
             "java"
           ],
           "tag_cnt": 1
         }
       }
     ]
   }
 }

Elasticsearch高手进阶篇(6)

结构化搜索_在案例中实战基于range filter来进行范围过滤

1、为帖子数据增加浏览量的字段

 POST /waws/article/_bulk
 { "update": { "_id": "1"}}
 { "doc" : {"view_cnt" : 30}}
 { "update": { "_id": "2"} }
 { "doc" : {"view_cnt" : 50}}
 { "update": { "_id": "3"} }
 { "doc" : {"view_cnt" : 100}}
 { "update": { "_id": "4"} }
 { "doc" : {"view_cnt" : 80}}

2、搜索浏览量在30~60之间的帖子

 GET /forum/article/_search
 {
   "query": {
     "constant_score": {
       "filter": {
         "range": {
           "view_cnt": {
             "gt": 30,
             "lt": 60
           }
         }
       }
     }
   }
 }
 
 {
   "took": 13,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 1,
     "max_score": 1,
     "hits": [
       {
         "_index": "waws",
         "_type": "article",
         "_id": "2",
         "_score": 1,
         "_source": {
           "articleID": "KDKE-B-9947-#kL5",
           "userID": 1,
           "hidden": false,
           "postDate": "2017-01-02",
           "tag": [
             "java"
           ],
           "tag_cnt": 1,
           "view_cnt": 50
         }
       }
     ]
   }
 }
  • gte 大于等于
  • lte 小于等于
  • gt 大于
  • lt 小于

3、搜索发帖日期在最近1个月的帖子

 POST /waws/article/_bulk
 {"index": { "_id": 5 }}
 {"articleID" : "DHJK-B-1395-#Ky5", "userID" : 3, "hidden": false, "postDate": "2017-03-01", "tag": ["elasticsearch"], "tag_cnt": 1, "view_cnt": 10}

搜索数据

 GET /waws/article/_search 
 {
   "query": {
     "constant_score": {
       "filter": {
         "range": {
           "postDate": {
             "gt": "2017-03-10||-30d"
           }
         }
       }
     }
   }
 }
 
 {
   "took": 1,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 1,
     "max_score": 1,
     "hits": [
       {
         "_index": "waws",
         "_type": "article",
         "_id": "5",
         "_score": 1,
         "_source": {
           "articleID": "DHJK-B-1395-#Ky5",
           "userID": 3,
           "hidden": false,
           "postDate": "2017-03-01",
           "tag": [
             "elasticsearch"
           ],
           "tag_cnt": 1,
           "view_cnt": 10
         }
       }
     ]
   }
 }
 GET /forum/article/_search 
 {
   "query": {
     "constant_score": {
       "filter": {
         "range": {
           "postDate": {
             "gt": "now-30d"
           }
         }
       }
     }
   }
 }