ES中高級(jí)檢索(Query)

10.1 檢索方式 _search

ES官方提供了兩中檢索方式:一種是通過(guò) URL 參數(shù)進(jìn)行搜索,另一種是通過(guò) DSL(Domain Specified Language) 進(jìn)行搜索官方更推薦使用第二種方式第二種方式是基于傳遞JSON作為請(qǐng)求體(request body)格式與ES進(jìn)行交互,這種方式更強(qiáng)大,更簡(jiǎn)潔。

image-20200701225025975.png
  • 使用語(yǔ)法

    URL查詢: GET /索引/類型/_search?參數(shù)

    DSL查詢: GET /索引/類型/_search {}

10.2 測(cè)試數(shù)據(jù)

1.刪除索引
DELETE /ems

2.創(chuàng)建索引并指定類型
PUT /ems
{
  "mappings":{
    "emp":{
      "properties":{
        "name":{
          "type":"text"
        },
        "age":{
          "type":"integer"
        },
        "bir":{
          "type":"date"
        },
        "content":{
          "type":"text"
        },
        "address":{
          "type":"keyword"
        }
      }
    }
  }
}

3.插入測(cè)試數(shù)據(jù)
PUT /ems/emp/_bulk
  {"index":{}}
  {"name":"小黑","age":23,"bir":"2012-12-12","content":"為開(kāi)發(fā)團(tuán)隊(duì)選擇一款優(yōu)秀的MVC框架是件難事兒,在眾多可行的方案中決擇需要很高的經(jīng)驗(yàn)和水平","address":"北京"}
  {"index":{}}
  {"name":"王小黑","age":24,"bir":"2012-12-12","content":"Spring 框架是一個(gè)分層架構(gòu),由 7 個(gè)定義良好的模塊組成。Spring 模塊構(gòu)建在核心容器之上,核心容器定義了創(chuàng)建、配置和管理 bean 的方式","address":"上海"}
  {"index":{}}
  {"name":"張小五","age":8,"bir":"2012-12-12","content":"Spring Cloud 作為Java 語(yǔ)言的微服務(wù)框架,它依賴于Spring Boot,有快速開(kāi)發(fā)、持續(xù)交付和容易部署等特點(diǎn)。Spring Cloud 的組件非常多,涉及微服務(wù)的方方面面,井在開(kāi)源社區(qū)Spring 和Netflix 、Pivotal 兩大公司的推動(dòng)下越來(lái)越完善","address":"無(wú)錫"}
  {"index":{}}
  {"name":"win7","age":9,"bir":"2012-12-12","content":"Spring的目標(biāo)是致力于全方位的簡(jiǎn)化Java開(kāi)發(fā)。 這勢(shì)必引出更多的解釋, Spring是如何簡(jiǎn)化Java開(kāi)發(fā)的?","address":"南京"}
  {"index":{}}
  {"name":"梅超風(fēng)","age":43,"bir":"2012-12-12","content":"Redis是一個(gè)開(kāi)源的使用ANSI C語(yǔ)言編寫(xiě)、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫(kù),并提供多種語(yǔ)言的API","address":"杭州"}
  {"index":{}}
  {"name":"張無(wú)忌","age":59,"bir":"2012-12-12","content":"ElasticSearch是一個(gè)基于Lucene的搜索服務(wù)器。它提供了一個(gè)分布式多用戶能力的全文搜索引擎,基于RESTful web接口","address":"北京"}

10.2 URL檢索

GET /ems/emp/_search?q=&sort=age:asc*

_search 搜索的API
      q=*     匹配所有文檔
      sort    以結(jié)果中的指定字段排序

GET /ems/emp/_search?q=*&sort=age:desc&size=5&from=0&_source=name,age,bir

10.3 DSL檢索

NOTE: 以下重點(diǎn)講解DSL語(yǔ)法

GET /ems/emp/_search
{
    "query": {"match_all": {}},
    "sort": [
        {
            "age": {
                "order": "desc"
            }
        }
    ]
}

10.4 DSL高級(jí)檢索(Query)

0. 查詢所有(match_all)

match_all關(guān)鍵字: 返回索引中的全部文檔

GET /ems/emp/_search
{
    "query": { "match_all": {} }
}   

1. 查詢結(jié)果中返回指定條數(shù)(size)

size 關(guān)鍵字: 指定查詢結(jié)果中返回指定條數(shù)。 默認(rèn)返回值10條

GET /ems/emp/_search
{
    "query": { "match_all": {} },
    "size": 1
}   

2. 分頁(yè)查詢(from)

from 關(guān)鍵字: 用來(lái)指定起始返回位置,和size關(guān)鍵字連用可實(shí)現(xiàn)分頁(yè)效果

GET /ems/emp/_search
{
      "query": {"match_all": {}},
      "sort": [
        {
          "age": {
            "order": "desc"
          }
        }
      ],
      "size": 2, 
      "from": 1
}

3. 查詢結(jié)果中返回指定字段(_source)

_source 關(guān)鍵字: 是一個(gè)數(shù)組,在數(shù)組中用來(lái)指定展示那些字段

GET /ems/emp/_search
{
      "query": { "match_all": {} },
      "_source": ["account_number", "balance"]
}

4. 關(guān)鍵詞查詢(term)

term 關(guān)鍵字: 用來(lái)使用關(guān)鍵詞查詢

GET /ems/emp/_search
{
  "query": {
    "term": {
      "address": {
        "value": "北京"
      }
    }
  }
}

NOTE1: 通過(guò)使用term查詢得知ES中默認(rèn)使用分詞器為標(biāo)準(zhǔn)分詞器(StandardAnalyzer),標(biāo)準(zhǔn)分詞器對(duì)于英文單詞分詞,對(duì)于中文單字分詞。

NOTE2: 通過(guò)使用term查詢得知,在ES的Mapping Type 中 keyword , date ,integer, long , double , boolean or ip 這些類型不分詞,只有text類型分詞。

練習(xí):


PUT /ems
{
  "mappings":{
    "emp":{
      "properties":{
        "name":{
          "type":"text"
        },
        "age":{
          "type":"integer"
        },
        "bir":{
          "type":"date"
        },
        "content":{
          "type":"text"
        },
        "address":{
          "type":"keyword"
        }
      }
    }
  }
}

PUT /ems/emp/_bulk
  {"index":{}}
  {"name":"小黑","age":23,"bir":"2012-12-12","content":"為開(kāi)發(fā)團(tuán)隊(duì)選擇一款優(yōu)秀的MVC框架是件難事兒,在眾多可行的方案中決擇需要很高的經(jīng)驗(yàn)和水平","address":"北京"}
  {"index":{}}
  {"name":"王小黑","age":24,"bir":"2012-12-12","content":"Spring 框架是一個(gè)分層架構(gòu),由 7 個(gè)定義良好的模塊組成。Spring 模塊構(gòu)建在核心容器之上,核心容器定義了創(chuàng)建、配置和管理 bean 的方式","address":"上海"}
  {"index":{}}
  {"name":"張小五","age":8,"bir":"2012-12-12","content":"Spring Cloud 作為Java 語(yǔ)言的微服務(wù)框架,它依賴于Spring Boot,有快速開(kāi)發(fā)、持續(xù)交付和容易部署等特點(diǎn)。Spring Cloud 的組件非常多,涉及微服務(wù)的方方面面,井在開(kāi)源社區(qū)Spring 和Netflix 、Pivotal 兩大公司的推動(dòng)下越來(lái)越完善","address":"無(wú)錫"}
  {"index":{}}
  {"name":"win7","age":9,"bir":"2012-12-12","content":"Spring的目標(biāo)是致力于全方位的簡(jiǎn)化Java開(kāi)發(fā)。 這勢(shì)必引出更多的解釋, Spring是如何簡(jiǎn)化Java開(kāi)發(fā)的?","address":"南京"}
  {"index":{}}
  {"name":"梅超風(fēng)","age":43,"bir":"2012-12-12","content":"Redis是一個(gè)開(kāi)源的使用ANSI C語(yǔ)言編寫(xiě)、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫(kù),并提供多種語(yǔ)言的API","address":"杭州"}
  {"index":{}}
  {"name":"張無(wú)忌","age":59,"bir":"2012-12-12","content":"ElasticSearch是一個(gè)基于Lucene的搜索服務(wù)器。它提供了一個(gè)分布式多用戶能力的全文搜索引擎,基于RESTful web接口","address":"北京"}
  
  
  
ES中高級(jí)查詢 QueryString QueryDSL


一、QueryString方式查詢 查詢所有q=* 排序 sort 分頁(yè) from size
GET /ems/emp/_search?q=*&sort=age:desc&size=5&from=0&_source=name,age,bir


二、 QueryDSL 方式查詢 特定領(lǐng)域的查詢語(yǔ)言

1、查詢所有
GET /ems/emp/_search
{
  "query": {
    "match_all": {}
  }
}

2、查詢所有并排序 sort
GET /ems/emp/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    },
    {
      "address":{
        "order":"desc"
      }
    }
  ]
}

3、分頁(yè)查詢 size from
GET /ems/emp/_search
{
  "query": {
    "match_all": {}
  },
  "size": "2",
  "from":"0",
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}

4、指定查詢結(jié)果中返回指定字段 _source
GET /ems/emp/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["name","age","bir"]
}

5、QueryDSL中term查詢, 基于關(guān)鍵詞進(jìn)行查詢
GET /ems/emp/_search
{
  "query": {
    "term": {
      "content": {
        "value": "spring"
      }
    }
  }
}

1.type:  text 類型分詞 其他類型如keyword integer等不分詞
2.ES中默認(rèn)使用的分詞器是 標(biāo)準(zhǔn)分詞器
strandard 中文--->單字分詞 英文---->單詞分詞

GET /_analyze
{
  "text":"java 是一個(gè)好語(yǔ)言"
}

GET /_analyze
{
  "text":"nono is a good person"
}
3.ES中索引的庫(kù)的底層原理.jpg

索引區(qū):name:[張:0:1,張:1:1]在0號(hào)文檔中,出現(xiàn)1次,在1號(hào)文檔中出現(xiàn)1次。

5. 范圍查詢(range)

range 關(guān)鍵字: 用來(lái)指定查詢指定范圍內(nèi)的文檔

GET /ems/emp/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 8,
        "lte": 30
      }
    }
  }
}

6. 前綴查詢(prefix)

prefix 關(guān)鍵字: 用來(lái)檢索含有指定前綴的關(guān)鍵詞的相關(guān)文檔

GET /ems/emp/_search
{
  "query": {
    "prefix": {
      "content": {
        "value": "redis"
      }
    }
  }
}

7. 通配符查詢(wildcard)

wildcard 關(guān)鍵字: 通配符查詢 ? 用來(lái)匹配一個(gè)任意字符 * 用來(lái)匹配多個(gè)任意字符

GET /ems/emp/_search
{
  "query": {
    "wildcard": {
      "content": {
        "value": "re*"
      }
    }
  }
}

8. 多id查詢(ids)

ids 關(guān)鍵字 : 值為數(shù)組類型,用來(lái)根據(jù)一組id獲取多個(gè)對(duì)應(yīng)的文檔

GET  /ems/emp/_search
{
  "query": {
    "ids": {
      "values": ["lg5HwWkBxH7z6xax7W3_","lQ5HwWkBxH7z6xax7W3_"]
    }
  }
}

9. 模糊查詢(fuzzy)

fuzzy 關(guān)鍵字: 用來(lái)模糊查詢含有指定關(guān)鍵字的文檔

GET /ems/emp/_search
{
  "query": {
    "fuzzy": {
      "content":"spring"
    }
  }
}

fuzzy 模糊查詢  最大模糊錯(cuò)誤 必須在0-2之間
# 搜索關(guān)鍵詞長(zhǎng)度為 2 不允許存在模糊 0
# 搜索關(guān)鍵詞長(zhǎng)度為3-5 允許一次模糊 0 1 
# 搜索關(guān)鍵詞長(zhǎng)度大于5 允許最大2模糊

10. 布爾查詢(bool)

bool 關(guān)鍵字: 用來(lái)組合多個(gè)條件實(shí)現(xiàn)復(fù)雜查詢

must: 相當(dāng)于&& 同時(shí)成立

should: 相當(dāng)于|| 成立一個(gè)就行

must_not: 相當(dāng)于! 不能滿足任何一個(gè)

GET /ems/emp/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "age": {
              "gte": 0,
              "lte": 30
            }
          }
        }
      ],
      "must_not": [
        {"wildcard": {
          "content": {
            "value": "redi?"
          }
        }}
      ]
    }
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}

11. 高亮查詢(highlight)

highlight 關(guān)鍵字: 可以讓符合條件的文檔中的關(guān)鍵詞高亮

GET /ems/emp/_search
{
  "query": {
    "term": {
      "content": {
        "value": "redis"
      }
    }
  },
  "highlight": {
    "fields": {
      "*": {}
    }
  }
}

自定義高亮html標(biāo)簽: 可以在highlight中使用pre_tagspost_tags

GET /ems/emp/_search
{
  "query":{
    "term":{
      "content":"框架"
    }
  },
  "highlight": {
    "pre_tags": ["<span style='color:red'>"],
    "post_tags": ["</span>"],
    "fields": {
      "*":{}
    }
  }
}

多字段高亮 使用require_field_match開(kāi)啟多個(gè)字段高亮

 GET /ems/emp/_search
{
  "query":{
    "term":{
      "content":"框架"
    }
  },
  "highlight": {
    "pre_tags": ["<span style='color:red'>"],
    "post_tags": ["</span>"],
    "require_field_match":false,
    "fields": {
      "*":{}
    }
  }
}

12. 多字段查詢(multi_match)

GET /ems/emp/_search
{
  "query": {
    "multi_match": {
      "query": "中國(guó)",
      "fields": ["name","content"] #這里寫(xiě)要檢索的指定字段
    }
  }
}

13. 多字段分詞查詢(query_string)

GET /dangdang/book/_search
{
  "query": {
    "query_string": {
      "query": "中國(guó)聲音",
      "analyzer": "ik_max_word", 
      "fields": ["name","content"]
    }
  }
}

練習(xí):


6、范圍查詢 range  gte大于等于   gt大于  lte小于等于   lt小于
GET /ems/emp/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 8,
        "lte": 9
      }
    }
  }
}


7、前綴查詢 基于關(guān)鍵詞前綴查詢prefix
GET /ems/emp/_search
{
  "query": {
    "prefix": {
      "address": {
        "value": "北"
      }
    }
  }
}

8、通配符查詢 wildcard ?只匹配一個(gè)任意 *匹配0到任意多個(gè)
GET /ems/emp/_search
{
  "query": {
    "wildcard": {
      "address": {
        "value": "北*"
      }
    }
  }
}


9、多個(gè)id查詢 ids 查詢
GET /ems/emp/_search
{
  "query": {
    "ids": {
      "values": ["XUhDGHcBbQMu0BWMWNNT","YkhDGHcBbQMu0BWMWNNT"]
    }
  }
}

10、模糊查詢 fuzzy  最大模糊錯(cuò)誤在0-2之間
搜索關(guān)鍵字長(zhǎng)度為2 不允許存在模糊 0
搜索關(guān)鍵字長(zhǎng)度為3-5 允許一次模糊 0-1
搜索關(guān)鍵字長(zhǎng)度大于5  允許最大2次模糊
GET /ems/emp/_search
{
  "query": {
    "fuzzy": {
      "content":"sproog"
    }
  }
}



11、布爾查詢 bool  must(兩者條件必須滿足)  shoud(滿足其中一個(gè)條件)  must_not(兩個(gè)條件都不滿足的)
GET /ems/emp/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "age": {
              "value": "23"
            }
          }
        },
        {
            "range": {
            "FIELD": {
              "gte": 0,
             "lte": 30
            }
          }
        }
      ]
    }
  }
}


12、高亮查詢 highlight 查詢結(jié)果做二次渲染 高亮
 GET /ems/emp/_search
{
  "query":{
    "term":{
      "content":"redis"
    }
  },
  "highlight": {
    "pre_tags": ["<span style='color:red'>"],
    "post_tags": ["</span>"],
    "require_field_match":false,
    "fields": {
      "*":{}
    }
  }
}

13、多字段查詢 multi_match搜索比較智能
1、如果搜索的字段分詞 它會(huì)對(duì)query先進(jìn)行分詞 再搜索
2、如果搜索的字段比分詞,它會(huì)使用query整體進(jìn)行該字段搜索

GET /ems/emp/_search
{
  "query": {
    "multi_match": {
      "query": "中國(guó)",
      "fields": ["name","content"] 
    }
  }
}


14、多字段分詞查詢 query_string
GET /ems/emp/_search
{
  "query": {
    "query_string": {
      "query": "小redis",
      "fields": ["name","content"],
      "analyzer": "ik_max_word"
    }
  }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容