2016-01-18 19 views
5

次のドメインに集約するメカニズムを見つけたり、現在のAPIに存在しないことを証明してください。Elasticsearchのreverse_nested集約の結合

curl -XDELETE 127.0.0.1:9200/test_index 

    curl -XPUT 127.0.0.1:9200/test_index -d '{ 
     "mappings": { 
      "contact": { 
       "properties": { 
        "facebook_profile": { 
         "type": "nested", 
         "properties": { 
          "education": { 
           "type": "string" 
          }, 
          "year": { 
           "type": "integer" 
          } 
         } 
        }, 
        "google_profile": { 
         "type": "nested", 
         "properties": { 
          "education": { 
           "type": "string" 
          }, 
          "year": { 
           "type": "integer" 
          } 
         } 
        } 
       } 
      } 
     } 
    }' 

    curl -XPUT 127.0.0.1:9200/test_index/contact/contact1 -d '{ 
     "google_profile": { 
      "education": "stanford", "year": 1990 
     } 
    }' 

    curl -XPUT 127.0.0.1:9200/test_index/contact/contact2 -d ' 
    { 
     "facebook_profile": { 
      "education": "stanford", "year": 1990 
     } 
    }' 

特定の大学を卒業した連絡先の数について統計情報を検索するにはどのようにしてESをクエリできますか?

は、私は一つの可能​​性を見つけたが、それは接点に関して上記の質問に答えることができないので、それは、私に必要な結果を与えるものではありませんが、唯一の彼らの特定のプロファイル(ネストされたドキュメント)へ:

curl -XPOST '127.0.0.1:9200/test_index/_search?search_type=count&pretty=true' -d '{ 
     "aggs": { 
      "facebook_educations": { 
       "aggs": { 
        "field": { 
         "terms": { 
          "field": "contact.facebook_profile.education" 
         }, 
         "aggs": { 
          "reverse": { 
           "reverse_nested": { 
           } 
          } 
         } 
        } 
       }, 
       "nested": { 
        "path": "contact.facebook_profile" 
       } 
      }, 
      "google_educations": { 
       "aggs": { 
        "field": { 
         "terms": { 
          "field": "contact.google_profile.education" 
         }, 
         "aggs": { 
          "reverse": { 
           "reverse_nested": { 
           } 
          } 
         } 
        } 
       }, 
       "nested": { 
        "path": "contact.google_profile" 
       } 
      } 
     } 
    }' 

"aggregations" : { 
     "facebook_educations" : { 
      "doc_count" : 1, 
      "field" : { 
      "doc_count_error_upper_bound" : 0, 
      "sum_other_doc_count" : 0, 
      "buckets" : [ { 
       "key" : "stanford", 
       "doc_count" : 1, 
       "reverse" : { 
       "doc_count" : 1 
       } 
      } ] 
      } 
     }, 
     "google_educations" : { 
      "doc_count" : 1, 
      "field" : { 
      "doc_count_error_upper_bound" : 0, 
      "sum_other_doc_count" : 0, 
      "buckets" : [ { 
       "key" : "stanford", 
       "doc_count" : 1, 
       "reverse" : { 
       "doc_count" : 1 
       } 
      } ] 
      } 
     } 
    } 

しかし、ここに私は1つの見つかった接触がDOC(親)は、同一でも異なっている場合、それぞれ、私は私の最初の質問に答えることができないことを確認することはできません。私に与えるもの

ありがとうございました。

答えて

0

aggregate on multiple fieldsのように聞こえます。これはElasticsearchでは直接サポートされていませんが、これを回避し、探している結果を得る方法があります。

discussion on Githubdocumentationをご覧ください。

「スタンフォード」がfacebook_profile.educationまたはgoogle_profile.educationに表示されているかどうかを正しく理解している場合は、contactを集計で1回だけカウントすることをお勧めします。

次のいずれかの方法でこれを行うことができるはず:

  1. は、フィールドに格納された値を連結するためのスクリプトを使用します。

    { 
        "aggs": { 
        "by_education": { 
         "terms": { 
         "script": "doc['contact.facebook_profile.education'].values + doc['contact.google_profile.education'].values" 
         } 
        } 
        } 
    } 
    
  2. あなたは新しいを作成作成することができます両方のフィールドの値を含むインデックス時の専用フィールド.copy_toオプションを使用します。その後、単一のフィールドに集約します。たとえば、両方のフィールドの内容をeducation_combinedという新しいフィールドにコピーできます。

    { 
        "mappings":{ 
        "contact":{ 
         "properties":{ 
         "facebook_profile":{ 
          "type":"nested", 
          "properties":{ 
          "education":{ 
           "type":"string", 
           "copy_to":"education_combined" 
          }, 
          "year":{ 
           "type":"integer" 
          } 
          } 
         }, 
         "google_profile":{ 
          "type":"nested", 
          "properties":{ 
          "education":{ 
           "type":"string", 
           "copy_to":"education_combined" 
          }, 
          "year":{ 
           "type":"integer" 
          } 
          } 
         }, 
         "education_combined":{ 
          "type":"string" 
         } 
         } 
        } 
        } 
    } 
    

    その後、単純集計education_combined上:ネストされたドキュメントのためにあなたが_sourceフィールドにアクセスする必要があるため

    { 
        "aggs": { 
        "by_education": { 
         "terms": { "field": "education_combined" } 
        } 
        } 
    } 
    
+0

あなたの最初の提案は、単に、動作しません。 2番目のオプションはうまくいくかもしれませんが、残念なことに私のケースでは、子ドキュメントにラップされたネストされたドキュメントがあり、何らかの種類のhas_parent集約を実行しようとしています。これは私が今持っているものですhttp://stackoverflow.com/questions/35061945/match-query-inside-script-elasticsearch。とにかくありがとうございました – Serj

関連する問題