简体   繁体   中英

Elastic search - need common tags from different fields

I'm using Elastic Search trying to get the count of each "tags" for a set of questions, Here's a rough picture of the mapping:

schoolId

schoolname

question1

       tags(array) 
                  tagId , tagStr
                  tagid , tagStr
                  tagid , tagStr

question2

       tags(array) 
                  tagId , tagStr
                  tagid , tagStr
                  tagid , tagStr

question3

       tags(array) 
                  tagId , tagStr
                  tagid , tagStr
                  tagid , tagStr

now i need the top common tags from all three fields (question1,question2,question3)

like this

tagStr : clean ,doc_count :6

tagStr : faculty,doc_count :4

tagStr : study,doc_count :2

i am using aggregation on this provided by elastic search . like this

"aggs": {
      "Question1_TAGS": {
         "terms": {
            "field": "question1.tags.tagStr",
            "size": 3
         }
      },
      "Question2_TAGS": {
         "terms": {
            "field": "question2.tags.tagStr",
            "size": 3
         }
      },
      "Question3_TAGS": {
         "terms": {
            "field": "question3.tags.tagStr",
            "size": 3
         }
      }
   }

but it is giving me the tags and its count individually of each question

like this -

 "aggregations": {
      "Question1_TAGS": {

         "buckets": [
            {
               "key": "clean",
               "doc_count": 34
            },
            {
               "key": "faculty",
               "doc_count": 34
            },
            {
               "key": "staff",
               "doc_count": 21
            }
         ]
      },
      "Question3_TAGS": {

         "buckets": [
            {
               "key": "good class",
               "doc_count": 35
            },
            {
               "key": "library",
               "doc_count": 22
            },
            {
               "key": "sports",
               "doc_count": 22
            }
         ]
      },
      "Question2_TAGS": {

         "buckets": [
            {
               "key": "Nice class",
               "doc_count": 40
            },
            {
               "key": "Clean",
               "doc_count": 37
            },
            {
               "key": "faculty",
               "doc_count": 31
            }
         ]
      }

but i need the common tags from all three questions as shown below.

tagStr : clean ,doc_count :6

tagStr : faculty,doc_count :4

tagStr : study,doc_count :2

it would be very thankful if anyone could help me on this . Thanks in advance !! :)

You could try to do this by using a single terms aggregation with a script that joins your three arrays like this

"aggs": {
  "Question_TAGS": {
     "terms": {
        "script": "doc['question1.tags.tagStr'].values + doc['question2.tags.tagStr'].values + doc['question2.tags.tagStr'].values",
        "size": 3
     }
  }

}

That way all tagStr fields will be joined into a single array on which the terms aggregation will run. Give it a try.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM