2016-12-21 2 views
0

나는이처럼 보이는 중첩 된 문서와 인덱스가 있다고 가정는 집계 elasticsearch에 두 개의 키를 가질 수

{ 
    "mappings": { 
    "assignment": { 
     "properties":{ 
     "id": { 
     "type": "string" 
     }, 
     "location": { 
     "type": "string" 
     }, 
     "typeOfLoss":{ 
     "type": "string" 
     }, 
     "lineItems": { 
     "type": "nested", 
     "properties": { 
      "categoryCode":{ 
      "type": "string" 
      }, 
      "selectorCode":{ 
      "type": "string" 
      }, 
      "roomType": { 
      "type": "string" 
      } 
     } 
} 

내가 지금 selectorCode를 반환 "lineItems"문서의 카운트 집계를 얻고 싶은와 categoryType 여기서 roomType은 검색 쿼리와 일치합니다. 나는 elasticsearch에 새로운 오전 SQL

SELECT COUNT(*) as theCount, ln.category_code, ln.selector_code 
FROM line_items as ln, assignment 
WHERE assignment.location = "84043" 
AND assignment.typeOfLoss = "Fire" 
AND ln.roomType = "kitchen" 
GROUP BY ln.category_code, ln.selector_code 
ORDER BY theCount DESC; 

나는 NEST 쿼리를 시작했지만 몇 가지 문제가 오전에 내 쿼리를 작성할 수있는 사람이 올바른 방향으로 날 지점 수 있습니다 기대하고있다.

var typeOfLossQuery = new TermQuery 
{ 
    Field = "typeOfLoss", 
    Value = typeOfLoss 
}; 

var locationQuery = new TermQuery 
{ 
    Field = "location", 
    Value = location 
}; 

var roomTypeQuery = new TermQuery 
{ 
    Field = "roomType", 
    Value = roomType 
}; 

var result = client.Search<LineItem>(s => s 
    .From(0) 
    .Size(numberOfItems) 
    .Query(q => q.HasParent<Assignment>(a => a 
    .Query(x =>x 
     .MatchAll() && typeOfLossQuery && locationQuery 
    ) 
) && q.MatchAll() && roomTypeQuery 
)); 

답변

1

실제로 ElasticSearch를 사용하면이 작업을 수행 할 수 있지만 SQL처럼 깨끗하지는 않습니다. Nested Aggregations으로이 작업을 수행 할 수 있습니다. |

categoryCode : 당신은 SQL에서 다음과 같은 동등한 결과를 얻을 것입니다 수 있도록

설정

나는 설정에 데이터를거야 selectorCode | 백

c1 | s1 | 1

c1 | s2 |

  1. categoryCode

  2. 에 의해 나에게 데이터

  3. 집계 (SQL에서 그룹) where roomType = 'r1'을 보여 2

    PUT test1 
    
    PUT test1/_mapping/type1 
    { 
        "properties": { 
        "id": { 
         "type": "string" 
        }, 
        "location": { 
         "type": "string" 
        }, 
        "typeOfLoss": { 
         "type": "string" 
        }, 
        "lineItems": { 
         "type": "nested", 
         "properties": { 
         "categoryCode": { 
          "type": "string", 
          "fielddata": true 
         }, 
         "selectorCode": { 
          "type": "string", 
          "fielddata": true 
         }, 
         "roomType": { 
          "type": "string" 
         } 
         } 
        } 
        } 
    } 
    
    POST test1/type1 
    { 
        "location":"l1", 
        "lineItems": 
        { 
         "categoryCode": "c1", 
         "selectorCode": "s1", 
         "roomType": "r1" 
        } 
    } 
    
    POST test1/type1 
    { 
        "location":"l1", 
        "lineItems": 
        { 
         "categoryCode": "c1", 
         "selectorCode": "s2", 
         "roomType": "r1" 
        } 
    } 
    
    POST test1/type1 
    { 
        "location":"l1", 
        "lineItems": 
        { 
         "categoryCode": "c1", 
         "selectorCode": "s2", 
         "roomType": "r1" 
        } 
    } 
    

    쿼리

    GET test1/type1/_search 
    { 
        "size": 0, 
        "query": { 
        "nested": { 
         "path": "lineItems", 
         "query": { 
         "term": { 
          "lineItems.roomType": { 
          "value": "r1" 
          } 
         } 
         } 
        } 
        }, 
        "aggs": { 
        "nestedAgg": { 
         "nested": { 
         "path": "lineItems" 
         }, 
         "aggs": { 
         "byCategory": { 
          "terms": { 
          "field": "lineItems.categoryCode", 
          "size": 10 
          }, 
          "aggs": { 
          "bySelector": { 
           "terms": { 
           "field": "lineItems.selectorCode", 
           "size": 10 
           } 
          } 
          } 
         } 
         } 
        } 
        } 
    } 
    

    내 쿼리는 다음과 같은 말을한다,451,515,

  4. 는 "selectorCode"그래서 결과가 집계 목록을 반환

    { 
        "took": 6, 
        "timed_out": false, 
        "_shards": { 
        "total": 5, 
        "successful": 5, 
        "failed": 0 
        }, 
        "hits": { 
        "total": 3, 
        "max_score": 0, 
        "hits": [] 
        }, 
        "aggregations": { 
        "nestedAgg": { 
         "doc_count": 3, 
         "byCategory": { 
         "doc_count_error_upper_bound": 0, 
         "sum_other_doc_count": 0, 
         "buckets": [ 
          { 
          "key": "c1", 
          "doc_count": 3, 
          "bySelector": { 
           "doc_count_error_upper_bound": 0, 
           "sum_other_doc_count": 0, 
           "buckets": [ 
           { 
            "key": "s2", 
            "doc_count": 2 
           }, 
           { 
            "key": "s1", 
            "doc_count": 1 
           } 
           ] 
          } 
          } 
         ] 
         } 
        } 
        } 
    } 
    

결과에 "중첩"또는 "하위"집계를 만들었습니다. 집합 내부에는 "양동이"가 있습니다. byCategory의 외부 버킷은 doc_count이 3임을 나타냅니다. 이는 DB에 일치하는 3 개의 레코드가 있기 때문입니다.

그 다음에 중첩 된 bySelector 버킷은 이고 s1doc_count이고 각각 ​​2와 1입니다.

도움이 되었으면 좋겠어.이 모든 것을 네스트 쿼리로 바꾸도록하겠습니다.

+0

모든 노력에 감사드립니다! 나는이 질문에 당신이 두 개의 열쇠를 가질 수는 없지만 나의 문제는 해결하지 못한다고 대답한다고 생각합니다. C1과 C2의 두 범주 코드가 있고 C1의 내부에 SelectorCode : S1, S1, S2 및 S3이 있으면 C2에서 SelectorCodes : S1, S1, S1을가집니다. CategoryCode 및 SelectorCode 쌍이 가장 많이 발생하기 때문에 C2, S1을 첫 번째 항목으로 가져오고 싶습니다. 그러나 예를 들어 C1, S1, C1, S2, C1, S3를 얻으므로 CategoryCode, SelectorCode . –

+0

나는 네가하는 말을 듣는다. 모든 데이터가 있지만 원하는대로 정렬하려면 약간의 작업이 필요합니다. 당신은 단지 '주문 (1) DESC'만으로는 충분하지 않습니다. – jhilden

관련 문제