Omiron
Omiron

Reputation: 407

Elastic search filtered query, query part being ignored?

I'm building up the following search in code, the idea being that it filters down the set of matches then queries this so I can add score based on certain fields. For some reason the filter part works but whatever I put in the query (i.e. in the below I have no index sdfsdfsdf) it still returns anything matching the filter.

Is the syntax wrong?

 {  
       "query":{  
          "filtered":{  
             "query":{  
                "bool":{  
                   "must":{  
                      "match":{  
                         "sdfsdfsdf":{  
                            "query":"4",
                            "boost":2.0
                         }
                      }
                   }
                },
                "filter":{  
                   "bool":{  
                      "must":[  
                         {  
                            "terms":{  
                               "_id":[  
                                  "55f93ead5df34f1900abc20b",
                                  "55f8ab0226ec4bb216d7c938",
                                  "55dc4e949dcf833308c63d6b"
                               ]
                            }
                         },
                         {  
                            "range":{  
                               "published_date":{  
                                  "lte":"now"
                               }
                            }
                         }
                      ],
                      "must_not":{  
                         "terms":{  
                            "_id":[  
                               "55f0a799acccc28204a5058c"
                            ]
                         }
                      }
                   }
                }
             }
          }
       }
    }

Upvotes: 0

Views: 416

Answers (2)

Val
Val

Reputation: 217314

Your filter is not at the right level. It should not be inside query but at the same level as query like this:

{
  "query": {
    "filtered": {
      "query": {          <--- query and filter at the same level
        "bool": {
          "must": {
            "match": {
              "sdfsdfsdf": {
                "query": "4",
                "boost": 2
              }
            }
          }
        }
      },
      "filter": {         <--- query and filter at the same level
        "bool": {
          "must": [
            {
              "terms": {
                "_id": [
                  "55f93ead5df34f1900abc20b",
                  "55f8ab0226ec4bb216d7c938",
                  "55dc4e949dcf833308c63d6b"
                ]
              }
            },
            {
              "range": {
                "published_date": {
                  "lte": "now"
                }
              }
            }
          ],
          "must_not": {
            "terms": {
              "_id": [
                "55f0a799acccc28204a5058c"
              ]
            }
          }
        }
      }
    }
  }
}

Upvotes: 1

Duc.Duong
Duc.Duong

Reputation: 2790

You need to replace sdfsdfsdf with your existing field name in your type, e.g. title, otherwise I think it will fallback to match_all query.

"match":{  
        "title":{  
              "query": "some text here",
              "boost":2.0
        }
}

Upvotes: 0

Related Questions