Issue while creating complex query


(Gautam Vanani) #1

If state of an object is 3 then the object should not be shown but if the state of object is 3 and a particular sell_code=xyz then the object should be returned, also it is not necessary that state of object is 3 it could be 1 or 2 also but the object should be returned only if state of object is 3 and sell_code is xyz.

How to create query for this condition, Can anybody help me in this?


(Zachary Tong) #2

I'm not entirely clear on your requirements (confused by the wording). If you can formulate that into a boolean expression, it'll be relatively easy to convert into a query.

Do you just want the document returned if state:3 AND sell_code:xyz?

If yes, that's pretty simple:

{
  "query": {
    "bool": {
      "filter": [
        { "term" : { "state" : 3 } },
        { "term" : { "sell_code" : "xyz" } },
      ]
    }
  }
]

(Gautam Vanani) #3

Hi @polyfractal,

(shape = 1) &&
(color = 1) &&
( (state != 0) || (state != 6) || ( (state = 3) && (reason_code = 1) ) )


(Gautam Vanani) #4

Hi @polyfractal,
thank you for asking boolean expression,
below query is working fine where I changed the condition like,
!(state=3&&reason_code=0)

{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [
            {
              "terms": {
                "shape": [
                  1
                ]
              }
            },
            {
              "bool": {
                "should": [
                  {
                    "bool": {
                      "must_not": [
                        {
                          "terms": {
                            "stone_state": [
                              0,
                              6
                            ]
                          }
                        },
                        {
                          "bool": {
                            "must": [
                              {
                                "match": {
                                  "stone_state": 3
                                }
                              },
                              {
                                "match": {
                                  "reason_code": 0
                                }
                              }
                            ]
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

and if condition is [quote="Gautam_Vanani, post:3, topic:86192"]
( (state != 0) || (state != 6) || ( (state = 3) && (reason_code = 1) ) )
[/quote]

{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [{
              "terms": {
                "shape":[1,2]
              }
            },
            {
              "bool": {
                "should": [
                  {
                    "bool": {
                      "must_not": [
                        {
                          "terms": {
                            "state": [
                              0,
                              6
                            ]
                          }
                        },
                  {
                    "bool": {
                      "must": [
                        {
                          "match": {
                            "state": 3
                          }
                        },
                        {
                          "match": {
                            "reason_code": 1
                          }
                        }
                      ]
                    }
                  }
                      ]
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

than is returning data of state=3 and reason code is 0 as well
Can you help me in this?


(system) #5

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.