博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ES terms多值搜索及范围过滤深入剖析-搜索系统线上实战
阅读量:5153 次
发布时间:2019-06-13

本文共 4561 字,大约阅读时间需要 15 分钟。

专注于大数据及容器云核心技术解密,可提供全栈的大数据+云原生平台咨询方案,请持续关注本套博客。QQ邮箱地址:1120746959@qq.com,如有任何学术交流,可随时联系。详情请关注《数据云技术社区》公众号。

1 制造数据

新增索引POST /forum/article/_bulk{ "index": { "_id": 1 }}{ "articleID" : "XHDK-A-1293-#fJ3", "userID" : 1, "hidden": false, "postDate": "2017-01-01" }{ "index": { "_id": 2 }}{ "articleID" : "KDKE-B-9947-#kL5", "userID" : 1, "hidden": false, "postDate": "2017-01-02" }{ "index": { "_id": 3 }}{ "articleID" : "JODL-X-1937-#pV7", "userID" : 2, "hidden": false, "postDate": "2017-01-01" }{ "index": { "_id": 4 }}{ "articleID" : "QQPX-R-3956-#aD8", "userID" : 2, "hidden": true, "postDate": "2017-01-02" }追加新增字段POST /forum/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 /forum/article/_search     {      "query": {        "constant_score": {          "filter": {            "terms": {              "articleID": [                "KDKE-B-9947-#kL5",                "QQPX-R-3956-#aD8"              ]            }          }        }      }    }    GET /forum/article/_search    {        "query" : {            "constant_score" : {                "filter" : {                    "terms" : {                         "tag" : ["java"]                    }                }            }        }    }      "took": 2,  "timed_out": false,  "_shards": {    "total": 5,    "successful": 5,    "failed": 0  },  "hits": {    "total": 3,    "max_score": 1,    "hits": [      {        "_index": "forum",        "_type": "article",        "_id": "2",        "_score": 1,        "_source": {          "articleID": "KDKE-B-9947-#kL5",          "userID": 1,          "hidden": false,          "postDate": "2017-01-02",          "tag": [            "java"          ]        }      },      {        "_index": "forum",        "_type": "article",        "_id": "4",        "_score": 1,        "_source": {          "articleID": "QQPX-R-3956-#aD8",          "userID": 2,          "hidden": true,          "postDate": "2017-01-02",          "tag": [            "java",            "elasticsearch"          ]        }      },      {        "_index": "forum",        "_type": "article",        "_id": "1",        "_score": 1,        "_source": {          "articleID": "XHDK-A-1293-#fJ3",          "userID": 1,          "hidden": false,          "postDate": "2017-01-01",          "tag": [            "java",            "hadoop"          ]        }      }    ]  }}优化搜索结果,仅仅搜索tag只包含java的帖子POST /forum/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 /forum/article/_search{ "query": { "constant_score": { "filter": { "bool": { "must": [ { "term": { "tag_cnt": 1 } }, { "terms": { "tag": ["java"] } } ] } } } }}复制代码

2 范围搜索案例

2.1 制造数据

POST /forum/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.2 搜索浏览量在30~60之间的帖子

GET /forum/article/_search{  "query": {    "constant_score": {      "filter": {        "range": {          "view_cnt": {            "gt": 30,            "lt": 60          }        }      }    }  }}复制代码

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

POST /forum/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 /forum/article/_search {  "query": {    "constant_score": {      "filter": {        "range": {          "postDate": {            "gt": "2017-03-10||-30d"          }        }      }    }  }}GET /forum/article/_search {  "query": {    "constant_score": {      "filter": {        "range": {          "postDate": {            "gt": "now-30d"          }        }      }    }  }}复制代码

3 总结

专注于大数据及容器云核心技术解密,可提供全栈的大数据+云原生平台咨询方案,请持续关注本套博客。QQ邮箱地址:1120746959@qq.com,如有任何学术交流,可随时联系。详情请关注《数据云技术社区》公众号。

转载于:https://juejin.im/post/5d502b2d5188257c7d163ac1

你可能感兴趣的文章
UVa 11383 少林决胜(二分图最佳完美匹配)
查看>>
ISP DSP的不同
查看>>
深入Linux grep指令的详解(实用型)
查看>>
嵌入式根文件系统的移植和制作详解
查看>>
javascript Uncaught ReferenceError: 方法名 is not defined
查看>>
Android 学习心得(2)——Android内置数据库SQLite
查看>>
走进AngularJs(二) ng模板中常用指令的使用方式
查看>>
6-sift特征匹配
查看>>
网页编程技术三(H5中表格的用法)
查看>>
UITextView自定义placeholder功能:用一个label写了文字,然后当检测到长度不为0的时候就把label隐藏...
查看>>
MongoDB 插入、更新、删除
查看>>
arc()
查看>>
【leetcode】Majority Element
查看>>
数组的方法之(Array.prototype.reduce() 方法)
查看>>
itchat库微信自动回复祝福语
查看>>
移动端浏览器touch事件的研究总结
查看>>
pandas—总结(2) 数据读写 (更新中)
查看>>
POJ 2096 Collecting Bugs:期望dp
查看>>
绩效成绩
查看>>
JavaScript多继承(转载)
查看>>