PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 25, 2022

[FIXED] How to group documents by several fields at once?

 October 25, 2022     elasticsearch     No comments   

Issue

I have a simple search using scripts:

{
 "query": {
  "bool": {
   "filter": [
    {
     "exists": {
      "field": "fieldX"
     }
    },
    {
     "exists": {
      "field": "fieldY"
     }
    }
   ]
  }
 },
 "aggs": {
  "groupBy": {
   "terms": {
    "script": {
     "source": "doc['fieldX.keyword'].value+','+doc['fieldY.keyword'].value"
    }
   }
  }
 }
}

Groups are the result of this search:

"buckets" : [
        {
          "key" : "valueX1,valueY1",
          "doc_count" : 165
        },
        {
          "key" : "valueX2,valueY2",
          "doc_count" : 45
        }
]

The main problem here is: If all the fields (fieldX and fieldY) in documents exist - everything will be fine. If at least one field is missing, the search will return nothing.

I tried to rewrite this to normal search using aggs, terms, field but unsuccessfully.

Any idea how I can rewrite this search to keep the original result but avoid the described problem?


Solution

You need to update your script and add some null pointer check as shown below:

{
  "aggs": {
    "groupBy": {
      "terms": {
        "script": {
          "source": """
          if(doc['fieldX.keyword'].size()!=0 &&doc['fieldY.keyword'].size()!=0){
            doc['fieldX.keyword'].value+','+doc['fieldY.keyword'].value
          }else if(doc['fieldX.keyword'].size()==0 && doc['fieldY.keyword'].size()!=0){
            doc['fieldY.keyword'].value
          }else if(doc['fieldY.keyword'].size()==0 && doc['fieldX.keyword'].size()!=0){
            doc['fieldX.keyword'].value
          }
          
          """
        }
      }
    }
  }
}

You can remove else if part if you want to generate aggregation only when both fields value is available. Also, exists query you can remove after updating script.



Answered By - Sagar Patel
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing