一、英文檢索
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":{}}
}
}
二、 中文檢索
- 安裝中文分詞器插件:elasticsearch-analysis-ik(第5節(jié) 倒排索引、分詞器)
- 搜索方法的調(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