Elasticsearch API:Search

URI Search

curl -XGET 'http://10.213.10.30:10920/megacorp/employee/_search?pretty&q=last_name:Smith'

$ curl -XGET 'http://10.213.10.30:10920/megacorp/employee/_search?pretty&q=last_name:Smith'
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "2",
        "_score" : 0.2876821,
        "_source" : {
          "first_name" : "John",
          "last_name" : "Smith",
          "age" : 25,
          "about" : "I love to go rock climbing",
          "interests" : [
            "sports",
            "music"
          ]
        }
      },
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "first_name" : "Jane",
          "last_name" : "Smith",
          "age" : 32,
          "about" : "I like to collect rock albums",
          "interests" : [
            "music"
          ]
        }
      }
    ]
  }
}

查看更多用法

Request Body Search

curl -XGET 'http://10.213.10.30:10920/megacorp/employee/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{ "match" : { "last_name" : "Smith" } }
],
"filter": [
{ "range" : { "age" : { "gt" : 10} } }
]
}
}
}
'


######Response

{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [
"sports",
"music"
]
}
},
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests" : [
"music"
]
}
}
]
}
}


>* 分頁 (Pagination)
The **from** parameter defines the **offset** from the first result you want to fetch. The **size** parameter allows you to configure the **maximum amount** of hits to be returned.
Though **from** and **size** can be set as request parameters, they can also be set within the search body. **from** defaults to 0, and **size** defaults to 10.

curl -XGET 'http://10.213.10.30:10920/megacorp/employee/_search?pretty' -d '
{
"from" : 0, "size" : 1,
"query": {
"bool": {
"must": [
{ "match" : { "last_name" : "Smith" } }
],
"filter": [
{ "range" : { "age" : { "gt" : 10} } }
]
}
}
}
'

######Response

{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [
"sports",
"music"
]
}
}
]
}
}


>* 排序 (Sort)
The **from** parameter defines the **offset** from the first result you want to fetch. The **size** parameter allows you to configure the **maximum amount** of hits to be returned.
Though **from** and **size** can be set as request parameters, they can also be set within the search body. **from** defaults to 0, and **size** defaults to 10.
[查看更多用法](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html)

curl -XGET 'http://10.213.10.30:10920/megacorp/employee/_search?pretty' -d '
{
"sort" : [
{ "age" : {"order" : "asc"}},
"_score"
],
"query": {
"bool": {
"must": [
{ "match" : {
"last_name" : "Smith"
}}
],
"filter": [
{ "range" : {
"age" : { "gt" : 10 }
}
}
]
}
}
}
'


##全文搜索
搜索所有喜歡 rock climbing 的員工:
> 

curl -XGET 'http://10.213.10.30:10920/megacorp/employee/_search?pretty' -d '
{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}
'


你會(huì)發(fā)現(xiàn)我們同樣使用了 match 查詢來搜索 about 字段中的 rock climbing英染。我們會(huì)得到兩個(gè)匹配的文檔:
######Response

{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.53484553,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "2",
"_score" : 0.53484553,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [
"sports",
"music"
]
}
},
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_score" : 0.26742277,
"_source" : {
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests" : [
"music"
]
}
}
]
}
}


通常情況下杠览,Elasticsearch 會(huì)通過相關(guān)性來排列順序,第一個(gè)結(jié)果中轰传,John Smith 的 about 字段中明確地寫到 rock climbing幔嫂。而在 Jane Smith 的 about 字段中辆它,提及到了 rock,但是并沒有提及到 climbing履恩,所以后者的 _score 就要比前者的低锰茉。

這個(gè)例子很好地解釋了 Elasticsearch 是如何執(zhí)行全文搜索的。對于 Elasticsearch 來說似袁,相關(guān)性的概念是很重要的洞辣,而這也是它與傳統(tǒng)數(shù)據(jù)庫在返回匹配數(shù)據(jù)時(shí)最大的不同之處。

* 段落搜索
能夠找出每個(gè)字段中的獨(dú)立單詞固然很好昙衅,但是有的時(shí)候你可能還需要去匹配精確的短語或者 段落扬霜。例如,我們只需要查詢到 about 字段只包含 rock climbing 的短語的員工而涉。
為了實(shí)現(xiàn)這個(gè)效果著瓶,我們將對 match 查詢變?yōu)?match_phrase 查詢:

curl -XGET 'http://10.213.10.30:10920/megacorp/employee/_search?pretty' -d '
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
'

######Response

{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.53484553,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "2",
"_score" : 0.53484553,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [
"sports",
"music"
]
}
}
]
}
}


* 高亮我們的搜索
很多程序希望能在搜索結(jié)果中 高亮 匹配到的關(guān)鍵字來告訴用戶這個(gè)文檔是 如何 匹配他們的搜索的。在 Elasticsearch 中找到高亮片段是非常容易的啼县。
讓我們回到之前的查詢材原,但是添加一個(gè) highlight 參數(shù):

curl -XGET 'http://10.213.10.30:10920/megacorp/employee/_search?pretty' -d '
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}
'

當(dāng)我們運(yùn)行這個(gè)查詢后沸久,相同的命中結(jié)果會(huì)被返回,但是我們會(huì)得到一個(gè)新的名叫 highlight 的部分余蟹。在這里包含了 about 字段中的匹配單詞卷胯,并且會(huì)被 <em></em> HTML字符包裹住:
######Response

{
"took" : 30,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.53484553,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "2",
"_score" : 0.53484553,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [
"sports",
"music"
]
},
"highlight" : {
"about" : [
"I love to go <em>rock</em> <em>climbing</em>"
]
}
}
]
}
}


##統(tǒng)計(jì)
Elasticsearch 把這項(xiàng)功能稱作 匯總 (aggregations)威酒,通過這個(gè)功能窑睁,我們可以針對你的數(shù)據(jù)進(jìn)行復(fù)雜的統(tǒng)計(jì)。這個(gè)功能有些類似于 SQL 中的 GROUP BY葵孤,但是要比它更加強(qiáng)大担钮。

例如,讓我們找一下員工中最受歡迎的興趣是什么:

curl -XGET 'http://10.213.10.30:10920/megacorp/employee/_search?pretty' -d '
{
"aggs": {
"all_interests": {
"terms": { "field": "interests" }
}
}
}
'

可能會(huì)出現(xiàn)如下錯(cuò)誤:

{
"error" : {
"root_cause" : [
{
"type" : "illegal_argument_exception",
"reason" : "Fielddata is disabled on text fields by default. Set fielddata=true on [interests] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."
}
],
"type" : "search_phase_execution_exception",
"reason" : "all shards failed",
"phase" : "query",
"grouped" : true,
"failed_shards" : [
{
"shard" : 0,
"index" : "megacorp",
"node" : "qm6aUUoUScO_S16Sod_7Bw",
"reason" : {
"type" : "illegal_argument_exception",
"reason" : "Fielddata is disabled on text fields by default. Set fielddata=true on [interests] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."
}
}
],
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "Fielddata is disabled on text fields by default. Set fielddata=true on [interests] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."
}
},
"status" : 400
}

解決這個(gè)問題需要在該字段上設(shè)置Fileddata=true尤仍,默認(rèn)是禁用的箫津;
[解決方案](https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html#_fielddata_is_disabled_on_literal_text_literal_fields_by_default)

curl -XPUT 'http://10.213.10.30:10920/megacorp/_mapping/employee?pretty' -d'
{
"properties": {
"interests": {
"type": "text",
"fielddata": true
}
}
}
'

#####Response:

{
"acknowledged" : true
}

然后,讓我們重新找一下員工中最受歡迎的興趣是什么:

curl -XGET 'http://10.213.10.30:10920/megacorp/employee/_search?pretty' -d '
{
"aggs": {
"all_interests": {
"terms": { "field": "interests" }
}
}
}

{
"took" : 19,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [
"sports",
"music"
]
}
},
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests" : [
"music"
]
}
},
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about" : "I like to build cabinets",
"interests" : [
"forestry"
]
}
}
]
},
"aggregations" : {
"all_interests" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "music",
"doc_count" : 2
},
{
"key" : "forestry",
"doc_count" : 1
},
{
"key" : "sports",
"doc_count" : 1
}
]
}
}
}
'

文檔:
http://www.cnblogs.com/muniaofeiyu/p/5616316.html
http://blog.csdn.net/ty_0930/article/details/52266611
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末宰啦,一起剝皮案震驚了整個(gè)濱河市苏遥,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌绑莺,老刑警劉巖暖眼,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異纺裁,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)司澎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進(jìn)店門欺缘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人挤安,你說我怎么就攤上這事谚殊。” “怎么了蛤铜?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵嫩絮,是天一觀的道長。 經(jīng)常有香客問我围肥,道長剿干,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任穆刻,我火速辦了婚禮置尔,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘氢伟。我一直安慰自己榜轿,他們只是感情好幽歼,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著谬盐,像睡著了一般甸私。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上飞傀,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天颠蕴,我揣著相機(jī)與錄音,去河邊找鬼助析。 笑死犀被,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的外冀。 我是一名探鬼主播寡键,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼雪隧!你這毒婦竟也來了西轩?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤脑沿,失蹤者是張志新(化名)和其女友劉穎藕畔,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體庄拇,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡注服,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了措近。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片溶弟。...
    茶點(diǎn)故事閱讀 40,561評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖瞭郑,靈堂內(nèi)的尸體忽然破棺而出辜御,到底是詐尸還是另有隱情,我是刑警寧澤屈张,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布擒权,位于F島的核電站,受9級特大地震影響阁谆,放射性物質(zhì)發(fā)生泄漏碳抄。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一笛厦、第九天 我趴在偏房一處隱蔽的房頂上張望纳鼎。 院中可真熱鬧,春花似錦、人聲如沸贱鄙。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽逗宁。三九已至映九,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間瞎颗,已是汗流浹背件甥。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留哼拔,地道東北人引有。 一個(gè)月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像倦逐,于是被迫代替她去往敵國和親譬正。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,573評論 2 359

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

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,572評論 0 23
  • 接著上一篇的安裝后,筆者來介紹下es的簡單用法 首先是一些術(shù)語和基本的概念,這里的數(shù)據(jù)借鑒了es的中文文檔讓我們建...
    MacSam閱讀 39,297評論 4 20
  • 1. 你養(yǎng)過一只蝸牛么? 女兒從幼兒園帶回一只蝸牛健民。 這是我第一次開始養(yǎng)一只蝸牛抒巢。 2. 對養(yǎng)蝸牛這一類事情,我毫...
    申小葵閱讀 1,032評論 0 1
  • 最近項(xiàng)目剛剛結(jié)束秉犹,想把之前的vue撿起來溫習(xí)溫習(xí)蛉谜,順便學(xué)習(xí)下vue2,想搞一搞是如何搭建環(huán)境的凤优,畢竟項(xiàng)目還是要運(yùn)行...
    元遲1閱讀 447評論 0 2
  • 從未走進(jìn)你的生活悦陋,路人甲充當(dāng)了這么久,是該清醒了筑辨。一直都是性格所致,后來才發(fā)覺幸逆,是因?yàn)槲易卟贿M(jìn)你棍辕,
    zjant閱讀 77評論 0 0