日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ElasticSearch搜索语法学习(term,filter,bool,terms,range)

發布時間:2024/4/11 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ElasticSearch搜索语法学习(term,filter,bool,terms,range) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ES搜索語法學習


目錄

  • 原始數據
  • term,filter使用
  • bool組合多個filter條件來搜索數據
  • terms搜索多個值以及多值搜索結果優化
  • 基于range filter來進行范圍過濾
  • 手動控制全文檢索結果的精準度
  • dis_max實現best fields策略進行多字段搜索


  • 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}}}} }
  • query:搜索
  • constant_score:默認
  • filter:過濾
  • term filter/query:對搜索文本不分詞,直接拿去倒排索引中匹配,你輸入的是什么,就去匹配什么。比如說,如果對搜索文本進行分詞的話,“helle world” --> “hello”和“world”,兩個詞分別去倒排索引中匹配term,“hello world” --> “hello world”,直接去倒排索引中匹配“hello world”
  • 相當于SQL中的單個where條件
  • 搜索其他字段類似

  • 2. bool組合多個filter條件來搜索數據

    1. 搜索發帖日期為2017-01-01,或者帖子ID為XHDK-A-1293-#fJ3的帖子,同時要求帖子的發帖日期絕對不為2017-01-02
  • SQL實現
  • select * from forum.article where (post_date='2017-01-01' or article_id='XHDK-A-1293-#fJ3') and post_date!='2017-01-02' GET /forum/article/_search {"query": {"constant_score": {"filter": {"bool": {"should": [{"term": { "postDate": "2017-01-01" }},{"term": {"articleID": "XHDK-A-1293-#fJ3"}}],"must_not": {"term": {"postDate": "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"}}]}}]}}}} }
  • bool:組合多個過濾條件
  • must:必須匹配
  • must_not:必須不匹配
  • should:可以匹配其中任意一個即可
  • bool可以嵌套
  • 相當于SQL中的多個and條件

  • 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"]} }
  • term: {“field”: “value”}
  • terms: {“field”: [“value1”, “value2”]}
  • 相當于sql中的in
  • select * from tbl where col in ("value1", "value2")
    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"]}}}} }
  • 此時會將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"]}}]} }
    3. 優化搜索結果,僅僅搜索tag只包含java的帖子
  • 添加字段,標識tag數量
  • 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"]}}]}}}} }
  • 如果tag包含[“java”, “hadoop”, “elasticsearch”],搜索的就是只包含"java", “hadoop”, "elasticsearch"其中一個字符串的數據
  • terms用于多值搜索
  • 優化terms多值搜索的結果
  • 相當于SQL中的in語句

  • 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}}}}} }
  • range:范圍搜索
  • gt:大于
  • gte:大于等于
  • lt:小于
  • lte:小于等于
  • 2. 搜索發帖日期在最近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"}}}}} }
  • range,相當于sql中的between,或者>=,<=,做范圍過濾。

  • 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
  • 這個,就跟之前的那個term query,不一樣了。不是搜索exact value,是進行full text全文檢索。
  • match query,是負責進行全文檢索的。當然,如果要檢索的field,是not_analyzed類型的,那么match query也相當于term query。
  • GET /forum/article/_search {"query": {"match": {"title": "java elasticsearch"}} }
    2. 搜索標題中包含java和elasticsearch的blog
  • 搜索結果精準控制的第一步:靈活使用and關鍵字,如果你是希望所有的搜索關鍵字都要匹配的,那么就用and,可以實現單純match query無法實現的效果
  • GET /forum/article/_search {"query": {"match": {"title": {"query": "java elasticsearch","operator": "and"}}} }
    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個關鍵字
  • 默認情況下,should是可以不匹配任何一個的,比如上面的搜索中,this is java blog,就不匹配任何一個should條件
  • 但是有個例外的情況,如果沒有must的話,那么should中必須至少匹配一個才可以。比如下面的搜索,should中有4個條件,默認情況下,只要滿足其中一個條件,就可以匹配作為結果返回
  • 但是可以精準控制,should的4個條件中,至少匹配幾個才能作為結果返回
  • GET /forum/article/_search {"query": {"bool": {"should": [{ "match": { "title": "java" }},{ "match": { "title": "elasticsearch" }},{ "match": { "title": "hadoop" }},{ "match": { "title": "spark" }}],"minimum_should_match": 3 }} }
  • 全文檢索的時候,進行多個值的檢索,有兩種做法,match query;should
  • 控制搜索結果精準度:and operator,minimum_should_match

  • 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的帖子
  • 下面這個就是multi-field搜索,多字段搜索
  • GET /forum/article/_search {"query": {"bool": {"should": [{ "match": { "title": "java solution" }},{ "match": { "content": "java solution" }}]}} }
  • best fields策略,就是說,搜索到的結果,應該是某一個field中匹配到了盡可能多的關鍵詞,被排在前面;而不是盡可能多的field匹配到了少數的關鍵詞,排在了前面
  • dis_max語法,直接取多個query中,分數最高的那一個query的分數即可
  • GET /forum/article/_search {"query": {"dis_max": {"queries": [{ "match": { "title": "java solution" }},{ "match": { "content": "java solution" }}]}} }

    總結

    以上是生活随笔為你收集整理的ElasticSearch搜索语法学习(term,filter,bool,terms,range)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 久久久久久综合 | 超级黄色录像 | 日批视频 | 青青草激情视频 | 91蝌蚪视频在线观看 | 欧美日韩午夜精品 | 香蕉尹人| 人人爽人人爽人人爽 | 天天插综合网 | 免费无码国产精品 | 高清福利视频 | 在线观看麻豆视频 | 国产超级av | 国产精品久久久久久久妇 | 少妇太紧太爽又黄又硬又爽 | 成人高清网站 | 亚洲AV无码成人精品区东京热 | 欧美少妇18p| 少妇粉嫩小泬喷水视频www | 欧美午夜精品久久久久久蜜 | 免费观看黄色 | 亚洲a影院 | 中文字幕乱码在线人视频 | 台湾性dvd性色av | 亚洲永久在线观看 | 国产精品911| 一级特黄高清 | 亚洲欧美日韩一区二区三区四区 | 日韩高清免费观看 | 日日夜夜亚洲 | 国内激情视频 | 欧美日日 | 99人妻碰碰碰久久久久禁片 | 久久国产亚洲 | 欧美区亚洲区 | 日韩精品一区中文字幕 | 国产精品二区在线观看 | 国产老熟女伦老熟妇露脸 | 亚洲黄色小说网 | 天天摸天天操天天爽 | 丝袜制服中文字幕 | www国产www| 涩涩小网站| 国产黄色av片 | 亚洲精品v天堂中文字幕 | 久久精品精品 | 男生女生羞羞网站 | 亚洲日本网站 | 木下凛凛子av一区二区三区 | 激情综合激情五月 | 亚洲av毛片一区二二区三三区 | 中文字幕人妻色偷偷久久 | 亚洲中字幕 | 国产爆乳无码一区二区麻豆 | 国产福利在线看 | 五月天色综合 | aaa国产| 伦理久久| 性生交大片免费看3p | 第一章婶婶的性事 | 亚洲毛片一区二区 | 精品处破女学生 | 日韩手机看片 | 五月亚洲婷婷 | 777四色| 一区二区在线免费视频 | 香蕉在线视频观看 | 欧美三日本三级少妇三99 | 国产6区| 青青草毛片 | 欧美少妇b | 欧美黄色片视频 | 爽妇网国产精品 | 麻豆爱爱视频 | 亚洲12p | 久久人体视频 | 男人天堂网址 | 每日av更新 | 动漫涩涩免费网站在线看 | 午夜小视频网站 | 久草新在线 | 久久国产精品偷 | 亚洲欧洲在线观看 | 成人在线视频播放 | 亚洲国产精品久久久久婷蜜芽 | 97在线免费视频观看 | 国产精品无码无卡无需播放器 | 日韩三级在线播放 | 欧美色射 | 黑人巨大精品欧美一区免费视频 | 国精品人妻无码一区二区三区喝尿 | 亚洲一区二区三区加勒比 | 少妇又色又爽 | 老子午夜影院 | 日本美女黄色一级片 | 精品国产a线一区二区三区东京热 | 一级大片在线观看 | 在线观看污 | 可以直接看av的网址 |