Fatih Aktepe
Fatih Aktepe

Reputation: 601

elasticsearch nested queries

I'm reading my queries from my database and I add some filter in java side but the code doesn't work. I will read the part between stars from my database. Sorry for bad English. how can I make it work ,my query is:

{
  "from": 0,
  "size": 100,
  "query": {
    "filtered": **{
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "text": {
                  "query": "xxx",
                  "slop": 0
                }
              }
            },
            {
              "match": {
                "text": {
                  "query": "xbxxı",
                  "slop": 0
                }
              }
            }
          ],
          "minimum_should_match": 1,
          "boost": 1.0
        }
      }
    }**,
    "query": {
      "myFilter": {
        "filter": {
          "bool": {
            "must": [
              {
                "range": {
                  "date": {
                    "gt": "2015-09-08",
                    "lte": "2015-09-09"
                  }
                }
              },
              {
                "query": {
                  "match": {
                    "page": "1"
                  }
                }
              },
              {
                "range": {
                  "xxxx": {
                    "gt": "0.0"
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
}

Upvotes: 0

Views: 92

Answers (1)

Ryan Huynh.
Ryan Huynh.

Reputation: 306

The filtered you load from database is valid, however the problem is the query you define outside of ** .

"query": {
  "myFilter": {
    "filter": {
      "bool": {
        "must": [
          {
            "range": {
              "date": {
                "gt": "2015-09-08",
                "lte": "2015-09-09"
              }
            }
          },
          {
            "query": {
              "match": {
                "page": "1"
              }
            }
          },
          {
            "range": {
              "xxxx": {
                "gt": "0.0"
              }
            }
          }
        ]
      }
    }
  }
}

If you want to use this filter you have define it right after "query" inside your filtered.

{
"from": 0,
"size": 100,
"query": {
   "filtered":** {
      "query": {
        ...
      }**,
      "filter": {
         "bool": {
            "must": [
               {
                  "range": {
                     "date": {
                        "gt": "2015-09-08",
                        "lte": "2015-09-09"
                     }
                   }
                },
                {
                   "query": {
                       "match": {
                          "page": "1"
                        }
                    }
                 },
                 {  
                    "range": {
                       "xxxx": {
                          "gt": "0.0"
                        }
                    }
                 }
              ]
           }
         }
      }
   }
 }

Upvotes: 1

Related Questions