Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

elasticsearch - Elastic Search : Unwind an array attribute in a document

I have the below JSON documents in elastic search.

 Document 1
 {
  "name":"abc",
  "class": "First"
  "marks": [
      {"subject": "science", "total": 100},
      {"subject": "maths", "total": 100}   
  ]
}
 Document 2
 {
  "name":"cde",
  "class": "First"
  "marks": [
      {"subject": "science", "total": 100},
      {"subject": "maths", "total": 100}   
  ]
}

When I query for class 1, I need to get the results like below.

[
   {
      "name":"abc",
      "class": "First",
       "subject": "science", 
       "total": 100
},
{
      "name":"abc",
      "class": "First",
       "subject": "maths", 
       "total": 100
},
 {
      "name":"cde",
      "class": "First",
       "subject": "science", 
       "total": 100
},
{
      "name":"cde",
      "class": "First",
       "subject": "maths", 
       "total": 100
}
]

Is this possible to get the results in this format or close to this format in elastic search? Do we need nested types to achieve this? I am trying to avoid processing the result and creating the flat structure in the code. Elastic search version is 7.9

question from:https://stackoverflow.com/questions/65617292/elastic-search-unwind-an-array-attribute-in-a-document

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The closest you can get to this kind of flattening is by "hijacking" script fields:

POST index_name/_search
{
  "script_fields": {
    "docs": {
      "script": {
        "source": """
          def shared = ["name":params._source.name, "class":params._source.class];
          def enriched_marks = [];
          for (def mark : params._source.marks) {
            def tmp = [:];
            tmp.putAll(shared);
            tmp.putAll(mark);
            enriched_marks.add(tmp);
          }
          
          return enriched_marks;
        """
      }
    }
  }
}

yielding:

"hits" : {
  ...
  "hits" : [
    {
     ...
      "fields" : {
        "docs" : [
          {
            "name" : "abc",
            "total" : 100,
            "class" : "First",
            "subject" : "science"
          },
          {
            "name" : "abc",
            "total" : 100,
            "class" : "First",
            "subject" : "maths"
          }
        ]
      }
    },
    {
     ...
      "fields" : {
        "docs" : [
          {
            "name" : "cde",
            "total" : 100,
            "class" : "First",
            "subject" : "science"
          },
          {
            "name" : "cde",
            "total" : 100,
            "class" : "First",
            "subject" : "maths"
          }
        ]
      }
    }
  ]
}

But you'll still need to flatten these pre-flattened objects further down the line.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...