Home > Net >  Is ElasticSearch query valid?
Is ElasticSearch query valid?

Time:01-26

I faced a strange ES query and don't have enough experience to say that's valid or not.

{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "in_stock": "true"
              }
            },
            {
              "terms": {
                "dealer_ids": [
                  20336
                ]
              }
            },
            {
              "bool": {
                "should": [
                  {
                    "bool": {
                      "must": [
                        {
                          "terms": {
                            "is_inventory_type": [
                              1
                            ]
                          }
                        },
                        {
                          "terms": {
                            "make_id": [
                              8
                            ]
                          }
                        }
                      ]
                    }
                  },
                  {
                    "bool": {
                      "must": [
                        {
                          "terms": {
                            "is_inventory_type": [
                              2,
                              3
                            ]
                          }
                        }
                      ]
                    }
                  },
                  {
                    "bool": {
                      "must": [
                        {
                          "terms": {
                            "is_inventory_type": [
                              3
                            ]
                          }
                        },
                        {
                          "terms": {
                            "make_id": [
                              8
                            ]
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          ],
          "must_not": []
        }
      }
    }
  },
  "sort": [
    {
      "year": {
        "order": "desc"
      }
    },
    {
      "inventory_date": {
        "order": "asc"
      }
    },
    "_score"
  ],
  "size": 24
}

Everything looks fine except inherited bool queries (must -> should -> must). It looks abnormally for me and I think that it's just a bug but can't say for sure 'cause ElasticSearch doesn't return any error. Is it valid query or not? And what does it mean if query is valid? Thanks in advance)

CodePudding user response:

If you get no error when running the query, it means it's syntactically valid.

As far as semantics goes, the query you have is

in_stock = true
AND dealer_ids = 20336
AND (
       (is_inventory_type = 1 AND make_id = 8)
    OR (is_inventory_type IN (2, 3))
    OR (is_inventory_type = 3 AND make_id = 8)
)

So only you can tell if this is semantically valid based on your business requirements.

  •  Tags:  
  • Related