0

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"} }]
}

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

Using dates as a filter seems to make more sense after reading about them. If I am not wrong, it's faster to use filters than queries in cases of dates and terms and bool values?
It might be faster. Filters don't participate in scoring (which is, probably, irrelevant in your case, since you sort by date) and they are cached. If you will be reusing this filter in several queries, it might help.
0

Used a bool query like this and it worked.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.