kibana 請(qǐng)求

//1.基本命令
//刪除索引
delete /test
/創(chuàng)建索引/
put /test
{
"settings":{
"number_of_shards":1,
"number_of_replicas":1
}
}

delete /employee
put /employee
{
"settings":{
"number_of_shards":1,
"number_of_replicas":1
}
}
/沒有則創(chuàng)建 有則修改/
put /employee/_doc/1
{
"name":"湯姆",
"age":30
}
/指定字段修改/
post /employee/_doc/6/_update
{
"doc":{
"age":"3"
}
}
/創(chuàng)建/
post /employee/_doc/6/_create/
{
"name":"兄長(zhǎng)",
"age":1
}
/獲取索引記錄/
get /employee/_doc/2
/刪除文檔/
delete /employee/_doc/2
/查詢?nèi)?/em>/
get /employee/_doc/_search

//使用結(jié)構(gòu)化的方式創(chuàng)建索引
put /employee
{
"settings":{
"number_of_shards":1,
"number_of_replicas":1
},
"mappings":{
"_doc":{
"properties":{
"name":{"type":"text"},
"age":{"type":"integer"}
}
}
}
}
}
//不帶條件查詢所有記錄
get /employee/_doc/_search
{
"query":{
"match_all":{}
}
}
//分頁(yè)查詢
get /employee/_doc/_search
{
"query":{
"match_all":{}
},
"from":0,
"size":30
}
//帶關(guān)鍵字查詢
get /employee/_doc/_search
{
"query":{
"match":{"name":"兄長(zhǎng)"}
}
}
//帶排序查詢
get /employee/_doc/_search
{
"query":{
"match":{"name":"兄長(zhǎng)"}
},
"sort":[
{"age":{"order":"desc"}}
]
}
//帶filter 不打分_source都是0.0
get /employee/_doc/_search
{
"query":{
"bool":{
"filter":[
{"term":{"age":30}}
]}
}
}
get /employee/_doc/_search
{
"query":{
"bool":{
"filter":[
{"match":{"name":"兄"}}
]}
}
}
//帶聚合
get /employee/_doc/_search
{
"query":{
"match":{"name":"兄"}
},
"sort":[
{"age":{"order":"desc"}}
],
"aggs":{
"group_by_age":{
"terms":{"field":"age"}
}
}
}
//2.高級(jí)查詢
//新建一個(gè)索引
put /movie/_doc/1
{
"name": "Eating an apple a day & keeps the doctor away"
}
get /movie/_doc/_search
{
"query":{
"match":{
"name":"eat"
}
}
}
//使用analyze api查看分詞狀態(tài)
get /movie/_analyze
{
"field":"name",
"text": "Eating an apple a day & keeps the doctor away"
}
delete /movie
//使用結(jié)構(gòu)化方式創(chuàng)建索引 英文分詞
put /movie
{
"settings":{
"number_of_shards":1,
"number_of_replicas":1
},
"mappings":{
"_doc":{
"properties":{
"name":{"type":"text","analyzer":"english"}
}
}
}
}
//3.類型
//text : 被分析索引的字符串類型
//keyword:不能被分析只能精確匹配的字符串類型
//date:日期類型厘熟,可以配合format一起使用
//數(shù)字類型:long蹭睡、integer傀蓉、short捐韩、double等
//boolean:true false
//array:["one","two"]
//object:json 嵌套
//ip類型
//geo_point:地理位置
//4.tmdb

put /movie
{
"settings":{
"number_of_shards":1,
"number_of_replicas":1
},
"mappings":{
"_doc":{
"properties":{
"title":{"type":"text","analyzer":"english"},
"tagline":{"type":"text","analyzer":"english"},
"release_date":{"type":"date","format":"8yyyy/MM/dd||yyyy/MM/d||yyyy/M/dd||yyyy/M/d"},
"popularity":{"type":"double"},
"overview":{"type":"text","analyzer":"english"},
"cast":{
"type":"object",
"properties":{
"character":{"type":"text", "analyzer":"standard"},
"name":{"type":"text", "analyzer":"standard"}
}
}
}
}
}
}

get /movie/_analyze
{
"field":"title",
"text":"basketball with cartoom aliens"
}
//搜索內(nèi)容 match
get /movie/_doc/_search
{
"query":{
"match":{"title":"steve zissou"}
}
}
//搜索內(nèi)容 term 不進(jìn)行分詞分析直接去索引查詢
get /movie/_doc/_search
{
"query":{
"term":{"title":"steve zissou"}
}
}
//分詞后的and和or的邏輯
get /movie/_doc/_search
{
"query":{
"match":{"title":"basketball with cartoom aliens"}
}
}
//改成and
get /movie/_search
{
"query":{
"match":{
"title":{
"query":"basketball love aliens",
"operator":"and"
}
}
}
}
//最小匹配項(xiàng)
get /movie/_search
{
"query":{
"match":{
"title":{
"query":"basketball love aliens",
"operator":"or",
"minimum_should_match": 2
}
}
}
}
//短語(yǔ)查詢 查詢不做分析分詞
get /movie/_search
{
"query":{
"match_phrase":{"title":"steve zissou"}
}
}

多字段查詢

get /movie/_search
{
"query":{
"multi_match":{
"query":"basketball with cartoom aliens",
"fields":["title", "overview"]
}
}
}

評(píng)分分析

get /movie/_search
{
"explain":true,
"query":{
"multi_match":{
"query":"basketball with cartoom aliens",
"fields":["title", "overview"],
"tie_breaker": 0.3
}
}
}

優(yōu)化多詞查詢

get /movie/_doc/_search
{
"query":{
"multi_match": {
"query":"basketball with cartoom aliens",
"fields":["title^10", "overview"],
"tie_breaker": 0.3
}
}
}

不同的multi_query其實(shí)是有不同的type

best_fields:默認(rèn)的得分方式,取最高的分?jǐn)?shù)作為對(duì)應(yīng)文檔的對(duì)應(yīng)分?jǐn)?shù)

most_fields:考慮絕大多數(shù)(所有的)文檔的字段得分相加箩艺,獲得我們想要的結(jié)果

cross_fields :已分詞為單位計(jì)算總分 適用于詞導(dǎo)向的場(chǎng)景

get /movie/_doc/_search
{
"explain":true,
"query":{
"multi_match": {
"query":"steve zissou",
"fields":["title^10", "overview"],
"type": "best_fields"
}
}
}
get /movie/_doc/_search
{
"explain":true,
"query":{
"multi_match": {
"query":"steve zissou",
"fields":["title^10", "overview^0.3"],
"type": "most_fields"
}
}
}
get /movie/_doc/_search
{
"explain":true,
"query":{
"multi_match": {
"query":"basketball with cartoom aliens",
"fields":["title^10", "overview^0.3"],
"type": "cross_fields"
}
}
}

bool 查詢

must not 必須都是false

must 必須都是true

should 其中有一個(gè)為true即可 為true的越多則得分越高

get /movie/_doc/_search
{
"query":{
"bool":{
"should":[
{"match": {"title":"basketball with cartoom aliens"}},
{"match": {"overview":"basketball with cartoom aliens"}}
]
}
}
}

query string

方便的利用 and or not

get /movie/_doc/_search
{
"query":{
"query_string": {
"fields": ["title"],
"query": "steve OR jobs"
}
}
}

filter 過濾查詢

get /movie/_search
{
"query":{
"bool":{
"filter":{
"term":{"title":"steve"}
}
}
}
}

多條件過濾

get /movie/_search
{
"query":{
"bool":{
"filter":[
{"term":{"title":"steve"}},
{"term":{"cast.name":"gaspard"}},
{"range":{"release_date":{"lte":"2015/01/01"}}},
{"range":{"popularity":{"gte":"25"}}}
]
}

},
"sort":[
{"popularity":{"order":"desc"}}
]
}

帶match打分的filter

get /movie/_search
{
"query":{
"bool":{
"should":[
{"match":{"title":"life"}},
{"multi_match": {
"query":"basketball with cartoom aliens",
"fields":["title^10", "overview^0.3"],
"type": "cross_fields"
}
}
],
"filter":[
{"range":{"release_date":{"lte":"2020/01/01"}}},
{"range":{"popularity":{"gte":"0"}}}
]
}
}
}

自定義score計(jì)算

get /movie/_search
{
"explain":true,
"query":{
"function_score": {
"query": {
"multi_match": {
"query": "steve job",
"fields": ["title", "overview"],
"type":"most_fields"
}
},
"functions": [
{
"field_value_factor": {
"field": "popularity", //對(duì)應(yīng)要處理的字段
"modifier": "log2p",
"factor": 10
}
},
{
"field_value_factor": {
"field": "popularity", //對(duì)應(yīng)要處理的字段
"modifier": "log2p",
"factor": 5
}
}
],
"score_mode":"sum",
"boost_mode":"sum"
}
}
}

測(cè)試ik分詞器

get _analyze
{
"analyzer":"ik_smart",
"text":"中華人民共和國(guó)國(guó)歌"
}

最大化分詞

get _analyze
{
"analyzer":"ik_max_word",
"text":"中華人民共和國(guó)國(guó)歌"
}
get _analyze
{
"analyzer":"english",
"text":"basketball with cartoom aliens"
}

analyzer 指定的是構(gòu)建索引的時(shí)候的分詞

search_analyzer 指定的是搜索關(guān)鍵字時(shí)候的分詞

最佳實(shí)踐: 構(gòu)建索引的時(shí)候使用max_word窜醉,但是查詢的時(shí)候使用smartword

使用距離排序

get /shop/_search
{
"query":{
"match":{"name":"凱越"}
},
"_source":*,
"script_fields":{ //es 腳本
"distance": {
"script":{
"source": "haversin(lat,lon,doc['location'].lat,doc['location'].lon)",
"lang":"expression",
"params":{"lat":23.11,"lon":127.12}
}
}
},
"sort":[
{
"_geo_distance": { //距離計(jì)算排序
"location": {
"lat":23.11,
"lon":127.12
},
"order":"asc",
"unit":"km",
"distance_type":"arc"
}
}
]
}

使用function_score解決排序模板

get /shop/_search
{
"_source":*,
"script_fields":{ //es 腳本
"distance": {
"script":{
"source": "haversin(lat,lon,doc['location'].lat,doc['location'].lon)",
"lang":"expression",
"params":{"lat":23.11,"lon":127.12}
}
}
},
"query":{
"function_score": {
"query": {
"bool":{
"must": [
{ "match":{"name":"凱越"}},
{"term":{"seller_disabled_flag":0}}
]
}

  },
  "functions": [
    {
      "gauss":{
        "location":{
          "origin":"23.11,127.12",
          "offset":"0km",
          "scale":"100km",
          "decay":0.5
        }
      },
      "weight": 9
    },
    {
      "field_value_factor": {
        "field": "remark_score"
      },
      "weight": 0.2,
    },
    {
      "field_value_factor": {
        "field": "seller_remark_score"
      },
      "weight": 0.1
    },
    {
      "filter": { 
        "term": { "name": "*" }
      },
      "weight": 1
    }
  ]
}

}
}


//創(chuàng)建
put /employee2
{
"settings":{
"number_of_shards":1,
"number_of_replicas":1
},
"mappings":{
"_doc":{
"properties":{
"name":{"type":"text"},
"age":{"type":"integer"}
}
}
}
}
}
//添加字段
put /employee2/_mapping/_doc
{
"properties": {
"auditStatus": {
"type": "integer"
}
}
}
//查詢
{"_source":"*",
"script_fields":{
"distance":{
"script":{
"source": "haversin(lat,lon,doc['location'].lat,doc['location'].lon)",
"lang": "expression",
"params":{"lat":39.995125, "lon":116.474152}
}
}
},
"query": {
"function_score": {
"query":{
"bool":{
"should":[
{"match_all":{}}
],
"filter":[{"terms":{"status":[3]}},{"range":{"editTime":{"lte":"2021-12-01 09:33:53"}}}],
"minimum_should_match":1
}
},
"functions":[{"gauss":{"location":{"offset":"0km","origin":"39.995125,116.474152","scale":"500km","decay":0.5}},"weight":0},{"field_value_factor":{"field":"id","modifier":"log2p","factor":2},"weight":0},{"field_value_factor":{"field":"praiseNum","modifier":"log2p","factor":2},"weight":0},{"field_value_factor":{"field":"commentNum","modifier":"log2p","factor":2},"weight":0},{"field_value_factor":{"field":"rewardNum","modifier":"log2p","factor":2},"weight":0},{"field_value_factor":{"field":"reward","modifier":"log2p","factor":2},"weight":0},{"filter":{"match":{"categoryName":"情感"}},"weight":0},{"filter":{"term":{"topStatus":1}},"weight":50000000}],
"score_mode": "sum",
"boost_mode": "sum"
}
},
"sort": [
{
"_score":{
"order": "desc"
},
"updateTimestamp" :{
"order": "desc"
} }
],
"from":0,"size": 10}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市艺谆,隨后出現(xiàn)的幾起案子榨惰,更是在濱河造成了極大的恐慌,老刑警劉巖静汤,帶你破解...
    沈念sama閱讀 206,839評(píng)論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件琅催,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡虫给,警方通過查閱死者的電腦和手機(jī)藤抡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,543評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來抹估,“玉大人缠黍,你說我怎么就攤上這事∫撸” “怎么了瓷式?”我有些...
    開封第一講書人閱讀 153,116評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)语泽。 經(jīng)常有香客問我贸典,道長(zhǎng),這世上最難降的妖魔是什么踱卵? 我笑而不...
    開封第一講書人閱讀 55,371評(píng)論 1 279
  • 正文 為了忘掉前任瓤漏,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘蔬充。我一直安慰自己,他們只是感情好班利,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,384評(píng)論 5 374
  • 文/花漫 我一把揭開白布饥漫。 她就那樣靜靜地躺著,像睡著了一般罗标。 火紅的嫁衣襯著肌膚如雪庸队。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,111評(píng)論 1 285
  • 那天闯割,我揣著相機(jī)與錄音彻消,去河邊找鬼。 笑死宙拉,一個(gè)胖子當(dāng)著我的面吹牛宾尚,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播谢澈,決...
    沈念sama閱讀 38,416評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼煌贴,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了锥忿?” 一聲冷哼從身側(cè)響起牛郑,我...
    開封第一講書人閱讀 37,053評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎敬鬓,沒想到半個(gè)月后淹朋,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,558評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡钉答,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,007評(píng)論 2 325
  • 正文 我和宋清朗相戀三年础芍,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片希痴。...
    茶點(diǎn)故事閱讀 38,117評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡者甲,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出砌创,到底是詐尸還是另有隱情虏缸,我是刑警寧澤,帶...
    沈念sama閱讀 33,756評(píng)論 4 324
  • 正文 年R本政府宣布嫩实,位于F島的核電站刽辙,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏甲献。R本人自食惡果不足惜宰缤,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,324評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧慨灭,春花似錦朦乏、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,315評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至筹陵,卻和暖如春刽锤,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背朦佩。 一陣腳步聲響...
    開封第一講書人閱讀 31,539評(píng)論 1 262
  • 我被黑心中介騙來泰國(guó)打工并思, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人语稠。 一個(gè)月前我還...
    沈念sama閱讀 45,578評(píng)論 2 355
  • 正文 我出身青樓宋彼,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親颅筋。 傳聞我的和親對(duì)象是個(gè)殘疾皇子宙暇,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,877評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容

  • 一、基本屬性介紹 1.包含搜索和聚合兩大功能 2.天生分布式結(jié)構(gòu)议泵,支持水平擴(kuò)展占贫,可以是多個(gè)節(jié)點(diǎn)甚至是幾百個(gè)節(jié)點(diǎn) 3...
    布魯_boy閱讀 4,329評(píng)論 1 1
  • 搜索機(jī)制 搜索的流程圖如下: 1,文檔寫入ES的時(shí)候先口,ES中會(huì)存儲(chǔ)兩份數(shù)據(jù)型奥。一份是文檔的原始數(shù)據(jù),即_source...
    吃火龍果吐芝麻閱讀 2,323評(píng)論 0 2
  • 注意點(diǎn):現(xiàn)在kibana/elasticsearch最新版本為7.0碉京,與5.2.0的語(yǔ)法有出入厢汹,需要注意 語(yǔ)法,自...
    天的安排閱讀 1,925評(píng)論 0 0
  • Hey everyone, Sam Ovens here. And in today's video I'm go...
    宋明亮閱讀 216評(píng)論 0 0
  • 今天感恩節(jié)哎谐宙,感謝一直在我身邊的親朋好友烫葬。感恩相遇!感恩不離不棄凡蜻。 中午開了第一次的黨會(huì)搭综,身份的轉(zhuǎn)變要...
    迷月閃星情閱讀 10,551評(píng)論 0 11