ElasticSearch搜索语法学习(term,filter,bool,terms,range)
ES搜索語法學習
目錄
1. term,filter使用
0. 原始數據(目錄1~2使用)
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" }1. 根據用戶ID搜索帖子
{"query" : {"constant_score" : { "filter" : {"term" : { "userID" : 1}}}} }2. bool組合多個filter條件來搜索數據
1. 搜索發帖日期為2017-01-01,或者帖子ID為XHDK-A-1293-#fJ3的帖子,同時要求帖子的發帖日期絕對不為2017-01-02
2. 搜索帖子ID為XHDK-A-1293-#fJ3,或者是帖子ID為JODL-X-1937-#pV7而且發帖日期為2017-01-01的帖子
select * from forum.article where article_id='XHDK-A-1293-#fJ3' or (article_id='JODL-X-1937-#pV7' and post_date='2017-01-01') GET /forum/article/_search {"query": {"constant_score": {"filter": {"bool": {"should": [{"term": {"articleID": "XHDK-A-1293-#fJ3"}},{"bool": {"must": [{"term":{"articleID": "JODL-X-1937-#pV7"}},{"term": {"postDate": "2017-01-01"}}]}}]}}}} }3. terms搜索多個值以及多值搜索結果優化
0. 原始數據
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"]} }1. 搜索articleID為KDKE-B-9947-#kL5或QQPX-R-3956-#aD8的帖子
GET /forum/article/_search {"query": {"constant_score": {"filter": {"terms": {"articleID": ["KDKE-B-9947-#kL5","QQPX-R-3956-#aD8"]}}}} }2. 搜索tag中包含java的帖子
GET /forum/article/_search {"query" : {"constant_score" : {"filter" : {"terms" : { "tag" : ["java"]}}}} }3. 優化搜索結果,僅僅搜索tag只包含java的帖子
4. 基于range filter來進行范圍過濾
0. 為帖子數據增加瀏覽量的字段
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} }
1. 搜索瀏覽量在30~60之間的帖子
GET /forum/article/_search {"query": {"constant_score": {"filter": {"range": {"view_cnt": {"gt": 30,"lt": 60}}}}} }2. 搜索發帖日期在最近1個月的帖子
5. 手動控制全文檢索結果的精準度
0. 為帖子數據增加標題字段
POST /forum/article/_bulk
{ “update”: { “_id”: “1”} }
{ “doc” : {“title” : “this is java and elasticsearch blog”} }
{ “update”: { “_id”: “2”} }
{ “doc” : {“title” : “this is java blog”} }
{ “update”: { “_id”: “3”} }
{ “doc” : {“title” : “this is elasticsearch blog”} }
{ “update”: { “_id”: “4”} }
{ “doc” : {“title” : “this is java, elasticsearch, hadoop blog”} }
{ “update”: { “_id”: “5”} }
{ “doc” : {“title” : “this is spark blog”} }
1. 搜索標題中包含java或elasticsearch的blog
2. 搜索標題中包含java和elasticsearch的blog
3. 搜索包含java,elasticsearch,spark,hadoop,4個關鍵字中,至少3個的blog
GET /forum/article/_search {"query": {"match": {"title": {"query": "java elasticsearch spark hadoop","minimum_should_match": "75%"}}} }4. 用bool組合多個搜索條件,來搜索title
GET /forum/article/_search {"query": {"bool": {"must": { "match": { "title": "java" }},"must_not": { "match": { "title": "spark" }},"should": [{ "match": { "title": "hadoop" }},{ "match": { "title": "elasticsearch" }}]}} }5. 搜索java,hadoop,spark,elasticsearch,至少包含其中3個關鍵字
6. dis_max實現best fields策略進行多字段搜索
0. 為帖子數據增加content字段
POST /forum/article/_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"} }1. 搜索title或content中包含java或solution的帖子
總結
以上是生活随笔為你收集整理的ElasticSearch搜索语法学习(term,filter,bool,terms,range)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Go常用语法
- 下一篇: ElasticSearch聚合语法学习(