Elasticsearch第10節(jié) 基本查詢

一、英文檢索

1. term和terms查詢

term query 會(huì)去 倒排索引中尋找 確切的term,它并 不知道分詞器的存在(指對(duì)查詢參數(shù)值進(jìn)行分詞)。這種查詢適合keyword、numeric、date類型數(shù)據(jù)的查詢

//
#term:查詢某個(gè)字段里含有**某個(gè)**關(guān)鍵詞的文檔:
GET /myindex/_search
{
  "query":{
    "term":{"intrest":"xi"}
  }
}
#terms:查詢某個(gè)字段里含有**多個(gè)**關(guān)鍵詞的文檔
GET /myindex/_search
{
  "query":{
    "terms":{"intrest":["xi", "huan"]} # contains anyone
  }
}
# 分頁(yè)展示,并且顯示版本號(hào)
GET /myindex/_search
{
  "from":0,
  "size":3,
  "version":true,      #顯示版本號(hào)
  "query":{
    "terms":{
      "intrest":["xi", "huan"]
    } 
  }
}

2. match查詢

match查詢知道分詞器的存在,會(huì)對(duì)查詢條件的字段值先進(jìn)行分詞,然后再查詢。

//
GET /myindex/_search
{
  "from":0,
  "size":3,
  "version":true,   
  "query":{
    "match":{
      "intrest":"basketball running"  #查詢包含basketball 或 running的
    } 
  }
}

mach_all 查所有

GET /myindex/_search
{
  "from":0,
  "size":3,
  "version":true,   
  "query":{
    "match_all":{ } 
  }
}
# content 或 intrest字段中包含"tool"的就會(huì)被查到
GET /myindex/_search
{
  "query":{
    "multi_match": {
      "query": "tool",
      "fields": ["content","intrest"]
    } 
  }
}

#短語(yǔ)匹配查詢
# 必須包含"is a new",順序不能變
GET /myindex/_search
{
  "query":{
    "match_phrase": {
      "content": "is a new" 
    } 
  }
}

#前綴匹配查詢(不區(qū)分大小寫(xiě))
GET /myindex/_search
{
  "query":{
    "match_phrase_prefix": {
      "content": "Java" 
    } 
  }
}

# _source:限定查詢結(jié)果的字段
GET /myindex/_search
{
  "_source": ["tile","post_date"], 
  "query":{
    "match_all":{ } 
  }
}


# _source includes excludes:限定查詢結(jié)果的字段 包含誰(shuí)和 不包含誰(shuí),可以用通配符
GET /myindex/_search
{
  "_source": {
    "includes": ["conte*"],
    "excludes": ["post_date"]
  }, 
  "query":{
    "match_all": {}
  }
}



# sort:排序 
GET /myindex/_search
{
  "query":{
    "match_all": {}
  }, "sort": [
    {
      "post_date": {
        "order": "desc"
      }
    }
  ]
}


# range 范圍查詢
GET /myindex/_search
{
  "query":{
    "range": {
      "post_date": {
        "gte": "2019-06-10",
        "lte": "2019-06-15",
        "include_lower":true,
        "include_upper":false
      }
    }
  }
}
wildcard:通配符查詢
//
# wildcard:通配符查詢 
GET /myindex/_search
{
  "query":{
    "wildcard": {
      "content": {
        "value": "like" # li?e   li*
      }
    }
  }
}

fuzzy實(shí)現(xiàn)模糊查詢

value:查詢的關(guān)鍵字
boost:查詢的權(quán)值,默認(rèn)是1
min_similarity:設(shè)置匹配的最小相似度,默認(rèn)值是0.5,對(duì)于字符串,取[0-1],對(duì)于數(shù)值,取值可能大于1,對(duì)于日期型,1d(1天) 1m (1分鐘)1h (1小時(shí))

prefix_length:指明區(qū)分詞項(xiàng)的共同前綴長(zhǎng)度,默認(rèn)是0
max_expansions:查詢中的詞項(xiàng)可以擴(kuò)展的數(shù)目,默認(rèn)可以無(wú)限大

//
#fuzzy模糊查詢,baksketball卻個(gè)s:baketball,但是也查出來(lái)了
GET /myindex/_search
{
  "query":{
    "fuzzy": {
      "intrest": "baketball"
    }
  }
}

#fuzzy模糊查詢第二種寫(xiě)法
GET /myindex/_search
{
  "query":{
    "fuzzy": {
      "intrest": {"value":"baketball"}
    }
  }
}

highlight : 高亮顯示

//
#highlight : 高亮顯示
GET /myindex/_search
{
  "query":{
    "fuzzy": {
      "intrest": {"value":"baketball"}
    }
  },
  "highlight": {
    "fields": {"intrest":{}}
  }
}


二、 中文檢索


  1. 安裝中文分詞器插件:elasticsearch-analysis-ik(第5節(jié) 倒排索引、分詞器)
  2. 搜索方法的調(diào)用和上面的一樣,只不過(guò)將搜索關(guān)鍵字換為中文。
#ik帶有2個(gè)分詞器
#ik_max_word : 會(huì)將文本做最細(xì)粒度的拆分;盡可能多的拆分出詞語(yǔ)。
#ik_smart:會(huì)做最粗粒度的拆分;已被分出的詞語(yǔ)將不會(huì)再次被其它詞語(yǔ)占有。
//手動(dòng)創(chuàng)建mapping
DELETE lib5
#手動(dòng)創(chuàng)建mapping
PUT /lib5
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 0
  },
  "mappings": {
    "properties": {
      "id":{"type" : "long"},
      "age" : {"type" : "integer"},
      "birthday" : {"type" : "date","index": false},  #不希望建立倒排索引
      "name" : {"type" : "text", "analyzer" : "ik_max_word" },
      "address" : {"type":"text","analyzer" : "ik_max_word"},
      "price" : {"type" : "double"},
    }
  }
}
GET /lib5/_mapping

最后編輯于
?著作權(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)容

  • Neil Zhu,簡(jiǎn)書(shū)ID Not_GOD,University AI 創(chuàng)始人 & Chief Scientist...
    朱小虎XiaohuZhu閱讀 13,287評(píng)論 0 5
  • 搜索機(jī)制 搜索的流程圖如下: 1,文檔寫(xiě)入ES的時(shí)候,ES中會(huì)存儲(chǔ)兩份數(shù)據(jù)。一份是文檔的原始數(shù)據(jù),即_source...
    吃火龍果吐芝麻閱讀 2,450評(píng)論 0 2
  • 為了演示不同類型的 ElasticSearch 的查詢,我們將使用書(shū)文檔信息的集合(有以下字段:title(標(biāo)題)...
    jinshan閱讀 1,075評(píng)論 0 2
  • 1.準(zhǔn)備數(shù)據(jù) PUT /lib4 ik分為兩個(gè)分詞器:ik_max_word:會(huì)將文本做最細(xì)粒度的拆分;盡可能的拆...
    Shaw_Young閱讀 256評(píng)論 0 0
  • 結(jié)構(gòu)化搜索 在結(jié)構(gòu)化查詢中,我們得到的結(jié)果 總是 非是即否,要么存于集合之中,要么存在集合之外。結(jié)構(gòu)化查詢不關(guān)心文...
    2fc2a81494ac閱讀 1,870評(píng)論 0 0

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