Attaching Reverse_Provided Aggregates in Elasticsearch

Please help me find a mechanism for aggregating by the following domain or to prove that it does not exist in the current API.

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 } }' 

How can I query ES to find statistics on how many contacts graduated from individual universities?

I found one opportunity, but it does not give me the desired result, because it cannot answer the question above regarding contacts, but only on their specific profiles (attached documents):

  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" } } } }' 

What gives me:

  "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 } } ] } } } 

But here I canโ€™t be sure that if the contact found is the same or a different document (parent), respectively, I can not answer my initial question.

Thanks for any advice.

+5
source share
1 answer

It looks like you are trying to fill in a few fields . This is not supported directly in Elasticsearch, but there are ways to get around this and get the results you are looking for.

Take a look at the discussion on Github as well as the documentation .

If I understand correctly whether stanford appears in facebook_profile.education or google_profile.education , you would like contact counted only once in the aggregation.

You should be able to do this in one of two ways:

  • Use a script to concatenate values โ€‹โ€‹stored in fields:

     { "aggs": { "by_education": { "terms": { "script": "doc['contact.facebook_profile.education'].values + doc['contact.google_profile.education'].values" } } } } 
  • You can create a new highlighted field at index time that contains values โ€‹โ€‹from both fields using the copy_to option. Then aggregate in one field. For example, you can copy the contents of both fields to a new field called 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" } } } } } 

    Then just aggregate to education_combined :

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

Source: https://habr.com/ru/post/1240853/


All Articles