elasticsearch - Elastisearch怎么求查詢結(jié)果的交集,如MYSQL的interset
問題描述
1.查詢20151216-17所有的名字
{'from': 0,'size': 200,'query': { 'bool': {'must': { 'range': {'DATE': { 'from': 20151216, 'to': 2015121617, 'include_lower': true, 'include_upper': true} }} }},'_source': { 'includes': ['NAME' ], 'excludes': []}
2.查詢20151217-18所有的名字
{'from': 0,'size': 200,'query': { 'bool': {'must': { 'range': {'DATE': { 'from': 20151216, 'to': 2015121617, 'include_lower': true, 'include_upper': true} }} }},'_source': { 'includes': ['NAME' ], 'excludes': []}
如果在mysql中可用如下的語句求得這兩天name的交集
SELECT NAME FROM Table1 where DATE between 20151216 and 20151217 interset SELECT NAME FROM Table1 where DATE between 20151217 and 20151218
Elastisearch中怎么做呢?
問題解答
回答1:SELECT NAME FROM Table1 where DATE between 20151216 and 20151217 interset SELECT NAME FROM Table1 where DATE between 20151217 and 20151218
我把樓主的SQL修改一下不知是否滿足樓主的需求,如果滿足我想下面的elasticsearch應(yīng)該也是可以使用的
SELECT NAME FROM Table1 where (DATE between 20151217 and 20151218) OR (DATE between 20151216 and 20151217)GROUP BY NAME
REQUEST:
{ 'query': {'bool': { 'should':[{ 'range': {'createdTime': { 'from': 1477984000, 'to': 1477984695} }},{ 'range': {'createdTime': { 'from': 1477984000, 'to': 1477984835} }} ]} }, 'size': 0, 'aggs': {'my_price': { 'terms': {'field': 'price' }} }}
RESPONSE:
{ 'took': 3, 'timed_out': false, '_shards': { 'total': 5, 'successful': 5, 'failed': 0 }, 'hits': { 'total': 542, 'max_score': 0, 'hits': [] }, 'aggregations': { 'my_price': { 'doc_count_error_upper_bound': 8, 'sum_other_doc_count': 377, 'buckets': [{ 'key': 148, 'doc_count': 24},{ 'key': 98, 'doc_count': 23},{ 'key': 128, 'doc_count': 20},{ 'key': 160, 'doc_count': 20},{ 'key': 108, 'doc_count': 18},{ 'key': 105, 'doc_count': 14},{ 'key': 100, 'doc_count': 13},{ 'key': 118, 'doc_count': 12},{ 'key': 88, 'doc_count': 11},{ 'key': 81, 'doc_count': 10} ] } }}
我這里是使用的數(shù)據(jù)創(chuàng)建時間,查詢其價格,因?yàn)槲业臄?shù)據(jù)中只有價格是重復(fù)的,就采用這個做一個測試。使用 aggs 對數(shù)據(jù)進(jìn)行分組,'size':0是禁止返回hits數(shù)據(jù),這樣直接讀取aggregations.my_price.buckets屬性就是你需要的數(shù)據(jù)集。
回答2:印象中沒有這個語法,對于nosql,查出來的都是json風(fēng)格數(shù)據(jù),用程序做下也很簡單高效。
