DSL語(yǔ)法示范

1掂碱、檢索拇砰、排序

terms表示查詢的內(nèi)容只要滿足任意一個(gè)都被搜索出來(lái)
sort節(jié)點(diǎn)指定排序字段
<pre>
{
"query": {
"terms": {
"content": [
"二季度",
"租房",
"上海"
]
}
},
"sort": {
"ttl": {
"order": "desc"
}
}
}
</pre>

2援雇、//should指的是滿足任意一個(gè)條件缨恒,must指的是滿足全部條件或must_no 排除 這個(gè)條件

<pre>
{
"query": {
"bool": {
"must": [
{ "match": { "content": "上海" } },
{ "match": { "url": "baidu" } },
{ "match": { "ch": 7 } }
]
}
}
}
</pre>

3蜂绎、全匹配 熬词,整個(gè)短語(yǔ)進(jìn)行匹配

<pre>
{
"query": {
"match_phrase": {
"content": "發(fā)生在上海"
}
}
}
</pre>

4旁钧、模糊查詢 ,query的內(nèi)容會(huì)自動(dòng)進(jìn)行分詞拆分

<pre>
{
"query": {
"query_string": {
"default_field": "content",
"query": "上海美國(guó)"
}
}
}
</pre>

5、鄰近,短語(yǔ)之間不超過(guò)多少個(gè)詞

<pre>
{"query": {
"match_phrase": {
"title": {
"query": "quick fox",
"slop": 15
}
}
}
}
</pre>
content包含“反正”和“避讓”互拾,間隔不超10詞
<pre>
{
"query": {
"match_phrase": {
"content": {
"query": "反正 避讓",
"slop": 10
}
}
}
}
</pre>

6歪今、having 聚合

context包含“今年”,按url聚合颜矿,聚合數(shù)>1,order by聚合數(shù)寄猩,時(shí)間范圍15/7/9-16/1/7
<pre>
{
"query" : {
"bool": {
"must":[
{"match": { "content" : "今年" }},
{
"query": {"range" : {
"ttl" : {
"gt" : "2015-07-09",
"lt" : "2016-01-07"
}
}}

   }
   ] 
}

},
"aggs": {
"url": {
"terms": {
"field": "url",
"size": 0,
"order" : { "_count" : "asc"}
},
"aggs": {
"having": {
"bucket_selector": {
"buckets_path": {
"url_count": "_count"
},
"script": {
"lang": "expression",
"inline": " url_count > 1"
}
}
}
}
}
},
"size": 0
}
</pre>
再增加一個(gè)having的示范,對(duì)于得sql語(yǔ)句為:
<pre>
SELECT ipo_year, COUNT(*) AS ipo_count FROM symbol
GROUP BY ipo_year HAVING ipo_count > 200
</pre>
DSL的語(yǔ)法為:
<pre>
{
"aggs": {
"ipo_year": {
"terms": {
"field": "ipo_year",
"size": 0
},
"aggs": {
"having": {
"bucket_selector": {
"buckets_path": {
"ipo_count": "_count"
},
"script": {
"lang": "expression",
"inline": " ipo_count > 200"
}
}
}
}
}
},
"size": 0
}
</pre>

7骑疆、content包含租房田篇,上海加權(quán)因子10,南京加權(quán)因子1

<pre>
{
"query": {
"bool": {
"must": {
"match": {
"content": {
"query": "租房",
"operator": "and"
}
}
},
"should": [
{ "match": { "content": {"query": "上海",
"boost": 10 }}},
{ "match": { "content": {"query": "南京",
"boost": 1} }}
]
}
}}
</pre>

8封断、組合filter

content:"上海","ch": 7,"url" :sina或者baidu
"size": 1表示為僅顯示1條結(jié)果斯辰,通過(guò)filter可以組成很復(fù)雜的查詢條件組合
<pre>
{
"query": {
"bool": {
"filter": [
{
"term": {
"content": "上海"
}
},
{
"term": {
"ch": 7
}
}
,
{
"terms": {
"url" : ["sina.com","baidu.com"]
}
}
]
}
},
"size": 1
}
</pre>

9、join查詢坡疼,需要安裝siren-join插件滿足

<pre>
{
"coordinate_search": {
"actions": [
{
"relations": {
"from": {
"indices": ["index2"],
"types": ["text2"],
"field": "ch"
},
"to": {
"indices": ["index"],
"types": ["text"],
"field": "foreign_key"
}
},
"size": 2,
"size_in_bytes": 20,
"is_pruned": false,
"cache_hit": false,
"terms_encoding" : "long",
"took": 313
}
]
}
}
</pre>
關(guān)于Elasticsearch如何支持join彬呻,這個(gè)slide總結(jié)得很好:http://www.slideshare.net/sirensolutions/searching-relational-data-with-elasticsearch。總體來(lái)說(shuō)有這么幾種方式:
? 完全不join闸氮,把關(guān)聯(lián)表的字段融合到一張表里剪况。當(dāng)然這會(huì)造成數(shù)據(jù)的冗余
? 錄入的時(shí)候join:使用 nested documents(nested document和主文檔是同segment存儲(chǔ)的,對(duì)于一個(gè)symbol蒲跨,幾千萬(wàn)個(gè)quote這樣的場(chǎng)景就不適合了)
? 錄入的時(shí)候join:使用 siren
? 查詢時(shí)join:使用 parent/child (這個(gè)是elasticsearch的特性译断,要求parent/child同shard存在)
? 查詢時(shí)join:使用 siren-joins(就是一個(gè)在服務(wù)端求值的filter,然后把結(jié)果發(fā)布給每個(gè)shard去做二次match)
? 查詢時(shí)join:在客戶端拼裝第二個(gè)查詢(和siren-joins差不多或悲,但是多了一次客戶端到服務(wù)器的來(lái)回)
? 查詢時(shí)join:在coordinate節(jié)點(diǎn)上做兩個(gè)查詢的join合并(https://github.com/NLPchina/elasticsearch-sql
我個(gè)人喜歡的是siren-joins和客戶端拼裝這兩種方案孙咪。這兩種方案都是先做了一次查詢,把查詢結(jié)果再次分發(fā)到每個(gè)分布式節(jié)點(diǎn)上再次去做分布式的聚合巡语。相比在coordinate節(jié)點(diǎn)上去做join合并更scalable翎蹈。

Elasticsearch官方說(shuō)明如下(就算是不推薦,不要使用2個(gè)index就行join):
Joining queriesedit

Performing full SQL-style joins in a distributed system like Elasticsearch is prohibitively expensive. Instead, Elasticsearch offers two forms of join which are designed to scale horizontally.

nested query Documents may contains fields of type nested. These fields are used to index arrays of objects, where each object can be queried (with the nested query) as an independent document. has_child and has_parent queries A parent-child relationship can exist between two document types within a single index. The has_child query returns parent documents whose child documents match the specified query, while the has_parent query returns child documents whose parent document matches the specified query.
Also see the terms-lookup mechanism in the terms query, which allows you to build a terms query from values contained in another document.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末男公,一起剝皮案震驚了整個(gè)濱河市荤堪,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌枢赔,老刑警劉巖澄阳,帶你破解...
    沈念sama閱讀 223,126評(píng)論 6 520
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異踏拜,居然都是意外死亡碎赢,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,421評(píng)論 3 400
  • 文/潘曉璐 我一進(jìn)店門(mén)速梗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)揩抡,“玉大人,你說(shuō)我怎么就攤上這事镀琉。” “怎么了蕊唐?”我有些...
    開(kāi)封第一講書(shū)人閱讀 169,941評(píng)論 0 366
  • 文/不壞的土叔 我叫張陵屋摔,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我替梨,道長(zhǎng)钓试,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 60,294評(píng)論 1 300
  • 正文 為了忘掉前任副瀑,我火速辦了婚禮弓熏,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘糠睡。我一直安慰自己挽鞠,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,295評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著信认,像睡著了一般材义。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上嫁赏,一...
    開(kāi)封第一講書(shū)人閱讀 52,874評(píng)論 1 314
  • 那天其掂,我揣著相機(jī)與錄音,去河邊找鬼潦蝇。 笑死款熬,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的攘乒。 我是一名探鬼主播贤牛,決...
    沈念sama閱讀 41,285評(píng)論 3 424
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼持灰!你這毒婦竟也來(lái)了盔夜?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 40,249評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤堤魁,失蹤者是張志新(化名)和其女友劉穎喂链,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體妥泉,經(jīng)...
    沈念sama閱讀 46,760評(píng)論 1 321
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡椭微,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,840評(píng)論 3 343
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了盲链。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蝇率。...
    茶點(diǎn)故事閱讀 40,973評(píng)論 1 354
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖刽沾,靈堂內(nèi)的尸體忽然破棺而出本慕,到底是詐尸還是另有隱情,我是刑警寧澤侧漓,帶...
    沈念sama閱讀 36,631評(píng)論 5 351
  • 正文 年R本政府宣布锅尘,位于F島的核電站,受9級(jí)特大地震影響布蔗,放射性物質(zhì)發(fā)生泄漏藤违。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,315評(píng)論 3 336
  • 文/蒙蒙 一纵揍、第九天 我趴在偏房一處隱蔽的房頂上張望顿乒。 院中可真熱鬧,春花似錦泽谨、人聲如沸璧榄。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,797評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)犹菱。三九已至拾稳,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間腊脱,已是汗流浹背访得。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,926評(píng)論 1 275
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留陕凹,地道東北人悍抑。 一個(gè)月前我還...
    沈念sama閱讀 49,431評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像杜耙,于是被迫代替她去往敵國(guó)和親搜骡。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,982評(píng)論 2 361

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