2015-01-16 103 views
1

這是一張人們以及他們說什麼語言的表格。 我需要人類(HID)誰知道只有一種語言, 用於測試(ENG和GER) 我想在Oracle SQL本會(HID 3)和(HID 4)查詢彈性搜索(如sql「having」)

PUT test/huml/1 
{"hid":1,"lang":"eng"} 
PUT test/huml/2 
{"hid":1,"lang":"ger"} 
PUT test/huml/3 
{"hid":1,"lang":"fr"} 
PUT test/huml/4 
{"hid":2,"lang":"eng"} 
PUT test/huml/5 
{"hid":2,"lang":"jap"} 
PUT test/huml/6 
{"hid":3,"lang":"eng"} 
PUT test/huml/7 
{"hid":4,"lang":"ger"} 
PUT test/huml/8 
{"hid":5,"lang":"eng"} 
PUT test/huml/9 
{"hid":5,"lang":"ger"} 
PUT test/huml/10 
{"hid":6,"lang":"eng"} 
PUT test/huml/111 
{"hid":6,"lang":"jap"} 

就像這樣:

with 
    t as (
    select 1 hid, 'eng' l from dual union all 
    select 1, 'ger' from dual union all 
    select 1, 'fr' from dual union all  
    select 2, 'eng' from dual union all 
    select 2, 'jap' from dual union all 
    select 3, 'eng' from dual union all 
    select 4, 'ger' from dual union all 
    select 5, 'eng' from dual union all 
    select 5, 'ger' from dual union all 
    select 6, 'eng' from dual union all 
    select 6, 'jap' from dual 
) 
select hid,max(l) 
from t 
group by hid,l 
having count (distinct case when l in ('eng','ger') then l end) = 1 
    and count(1) =1 

回答

1

我不認爲有一種方法可以做你直接問什麼,但(雖然見GitHub的問題herehere)。你或許可以用scripted metric aggregation一起破解一些東西,雖然這也不是很理想(我假設不會很好地擴展,儘管我沒有嘗試過)。

與你貼什麼,它可以找出很多語言用戶如何說話很輕鬆:

POST /test_index/_search?search_type=count 
{ 
    "aggs": { 
     "humans": { 
      "terms": { "field": "hid" }, 
      "aggs": { 
       "num_of_langs": { 
        "value_count": { "field": "lang" } 
       } 
      } 
     } 
    } 
} 

但是,這似乎並沒有真正的是你在問什麼。

但是,如果稍微修改架構,則可以使用boolhas_child過濾器的組合來解決問題(或多或少)。這是一種方法。

我拿走了您發佈的文檔,併爲每個"hid"提取了「父」對象。我用的是建立了一個父子關係的映射,然後批量索引的文檔:

DELETE /test_index 

PUT /test_index 
{ 
    "settings": { 
     "number_of_shards": 1, 
     "number_of_replicas": 0 
    }, 
    "mappings": { 
     "human": { 
     "properties": { 
      "hid": { "type": "long" } 
     } 
     }, 
     "has_lang": { 
     "_parent": { "type": "human" }, 
     "properties": { 
      "hid": { "type": "long" }, 
      "lang": { "type": "string" } 
     } 
     } 
    } 
} 

PUT /test_index/_bulk 
{"index":{"_index":"test_index", "_type":"human", "_id":1}} 
{"hid":1} 
{"index":{"_index":"test_index", "_type":"human", "_id":2}} 
{"hid":2} 
{"index":{"_index":"test_index", "_type":"human", "_id":3}} 
{"hid":3} 
{"index":{"_index":"test_index", "_type":"human", "_id":4}} 
{"hid":4} 
{"index":{"_index":"test_index", "_type":"human", "_id":5}} 
{"hid":5} 
{"index":{"_index":"test_index", "_type":"human", "_id":6}} 
{"hid":6} 

PUT /test_index/_bulk 
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":1, "_id":1}} 
{"hid":1,"lang":"eng"} 
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":1, "_id":2}} 
{"hid":1,"lang":"ger"} 
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":1, "_id":3}} 
{"hid":1,"lang":"fr"} 
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":2, "_id":4}} 
{"hid":2,"lang":"eng"} 
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":2, "_id":5}} 
{"hid":2,"lang":"jap"} 
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":3, "_id":6}} 
{"hid":3,"lang":"eng"} 
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":4, "_id":7}} 
{"hid":4,"lang":"ger"} 
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":5, "_id":8}} 
{"hid":5,"lang":"eng"} 
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":5, "_id":9}} 
{"hid":5,"lang":"ger"} 
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":6, "_id":10}} 
{"hid":6,"lang":"eng"} 
{"index":{"_index":"test_index", "_type":"has_lang", "_parent":6, "_id":11}} 
{"hid":6,"lang":"jap"} 

然後我就可以查詢誰講某種語言的人,但沒有其他,具體如下:

POST /test_index/human/_search 
{ 
    "filter": { 
     "bool": { 
     "must": [ 
      { 
       "has_child": { 
        "type": "has_lang", 
        "filter": { "term": { "lang": "ger" } } 
       } 
      } 
     ], 
     "must_not": [ 
      { 
       "has_child": { 
        "type": "has_lang", 
        "filter": { 
        "not": { 
         "filter": { "term": { "lang": "ger" } } 
        } 
        } 
       } 
      } 
     ] 
     } 
    } 
} 
... 
{ 
    "took": 4, 
    "timed_out": false, 
    "_shards": { 
     "total": 1, 
     "successful": 1, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 1, 
     "max_score": 1, 
     "hits": [ 
     { 
      "_index": "test_index", 
      "_type": "human", 
      "_id": "4", 
      "_score": 1, 
      "_source": { 
       "hid": 4 
      } 
     } 
     ] 
    } 
} 

你仍然需要爲每種語言做到這一點,所以這種方法可能並不理想,但希望它能讓你更接近。

我也嘗試使用聚合來獲得你想要的答案,但從來沒有找到一種方法來使它工作。如果/當減速器集合得到實施時,如果我正確理解了這個想法,那麼大概會解決這類問題。

這裏是我使用的代碼:

http://sense.qbox.io/gist/0615ec52346ae6e547988b156b221484dbfde50c

+0

複雜的解決方案,我想以取代快速elasticsearch統計經典的關係數據庫,但似乎難以查詢數據 –