Gabbar
Gabbar

Reputation: 4066

ElasticSearch Query issue

I have this query with range, text and sort in it. When I use range with sort or text with sort it works but when I try to use all three together it throws an error:

"Parse failure: failed to parse source"

I am not sure why it's not working as the error is not very clear. I checked the JSON using jsonlint.com and it's valid. Can I not use a combination of text and range query?

{
   "query" : {       
       "text" : {
          "Content" : "fight"
       },
       "range" : {
         "UpdateTime" : {
            "from" : "20120601T000000",
            "to" : "20120630T235959"
         }
       }        
    },
    "sort" : [{ "UpdateTime" : {"order" : "desc"} }]
}

Upvotes: 0

Views: 631

Answers (2)

imotov
imotov

Reputation: 30163

The query element can contain only a singe query inside. If you would like to limit your search by both range and text, you need to combine these two queries into a single query, or use one of them as a query and another one as a filter.

Combining these two queries using boolean query would look like this:

{
    "query" : {       
        "bool" : {
            "must" : [
                {
                    "text" : {
                        "Content" : "fight"
                    }                    
                },
                {
                    "range" : {
                        "UpdateTime" : {
                            "from" : "20120601T000000",
                            "to" : "20120630T235959"
                        }
                    }
                }
            ]
        }
    },
    "sort" : [{ "UpdateTime" : {"order" : "desc"} }]
}'

Using filter it would look like this:

{
    "query" : {       
        "text" : {
            "Content" : "fight"
        }                    
    },
    "filter" : {
        "range" : {
            "UpdateTime" : {
                "from" : "20120601T000000",
                "to" : "20120630T235959"
            }
        }
    },
    "sort" : [{ "UpdateTime" : {"order" : "desc"} }]
}

Using filter will affect the way facets are calculated. See Filter page of elasticsearch documentation for more detailed

Upvotes: 4

Gabbar
Gabbar

Reputation: 4066

Used a bool query like this and it worked.

{
    "query": {
        "bool": {
            "must": {     
                "range": {
                    "UpdateTime": {
                        "from": "20120601T000000",
                        "to": "20120630T235959"
                    }
                }                
            },
            "must_not": { 
               "text": {
                    "Content": "fight"
                }
           }
        }
    }
}

Upvotes: 0

Related Questions