Ashish Mishra
Ashish Mishra

Reputation: 155

If Else Elasticsearch

I have two sets of documents, which are joined by fragmentId. I have written a query that pulls both documents, but I am thinking is there any other way to write it.

first set Document - There could be only one document which has type = fragment and fragmentId = 1

{
"fragmentId": "1",
"type" : "fragment"
}

The second kind of documents - There could be multiple such documents, separated by start and end values. In the query, I will be passing a value and only document inside that range should come.

Doc-1

{
"fragmentId" : "1",
"type": "cf",
"start": 1,
"end": 5
}

Doc- 2

{
"fragmentId" : "1",
"type": "cf",
"start": 6,
"end": 10
}

In the result, I want the first set document, then from the second set only the document which has a specific start and end values. Here is the query, which is working for me-

GET test/_search
{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "fragmentId": "1"
                }
              },
              {
                "term": {
                  "type": "fragment"
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "fragmentId": "1"
                }
              },
              {
                "term": {
                  "type": "cf"
                }
              },
              {
                "range" :{
                  "start": {
                    "gte": 1,
                    "lte": 5
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

Is there a way to re-write this query in more simple form, so that first document is always picked, with the range matching document from the second set, basically a join operation on fragmentId?

Upvotes: 0

Views: 893

Answers (1)

Barkha Jain
Barkha Jain

Reputation: 768

Are you looking for something like this?

GET test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "fragmentId": "1"
          }
        },
        {
          "bool": {
            "minimum_should_match": 1, 
            "should": [
              {
                "term": {
                  "type": "fragment"
                }
              },
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "type": "cf"
                      }
                    },
                    {
                      "range": {
                        "start": {
                          "gte": 1,
                          "lte": 5
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

This query translates to :

(fragmentId = 1 AND (type = fragment OR (type = cf AND start is within 1 and 5)))

Upvotes: 1

Related Questions