2016-12-21 9 views
0

に2つのキーを持つことができ、私はこのようになり、ネストされたドキュメントのインデックスがあるとします。私は今selectorCodeを返します「つまり、lineItems」文書のカウント集計を取得したいは集約が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" 
      } 
     } 
} 

をし、 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で次の同等の結果を取得したいよう

セットアップ

私はセットアップにデータを行きますよセレクタコード|カウント

c1 | s1 | 1

c1 | s2 | 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 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

私のクエリは次のように言っている:categoryCode

によって

  1. のみ

  2. where roomType = 'r1'

  3. 私のデータを示して集計(SQLのグループ)
  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を示しています。

うまくいけば、これをすべてNESTクエリにすることができれば助かります。

+0

すべての努力をいただきありがとうございます!私はこれがあなたが2つのキーを持つことはできませんが、私の問題を解決しないという私の質問に答えると思います。 CategoryCodes:C1とC2の2つがあり、C1の中にSelectorCode:S1、S1、S2とS3がある場合、C2にはSelectorCodes:S1、S1、S1があります。 CategoryCodeとSelectorCodeのペアが最も多く発生するため、C2、S1を最初のアイテムとして取得したいと思いますが、例ではC1、S1、C1、S2、C1、S3を取得します。 。 –

+0

私はあなたが言っていることを聞いています。すべてのデータはそこにありますが、あなたが望むようにそれを手配するには少しの作業が必要です。あなたは単に「注文(1)DESCで注文する」ことはできません – jhilden

関連する問題