How to create a CQL query using API that searches for an exact phrase

In the UI, if I want to search for the exact phrase “red line”, I just enclose it in quotes.

However, this does not seem to work from the REST API. No matter what I do, I get back all results that have either the word ‘red’ OR ‘line’ … and that’s way too many results.

I’ve tried:
?cql=text~%22red+line%22
?cql=text~%22red+AND+line%22
?cql=text~%27red+line%27
?cql=text~%27red+AND+line%27
?cql=text~red+AND+text~line

Hi,
Did you ever get an answer to this, I want to be able to do the same thing using REST Search API - but as you have mentioned, it AND’s all the words by default, and does not do an exact phrase match :frowning:

Sadly, no. No reply.

But the problem is that it OR’s all the words by default.

Hi @fbunting,

My apologies that it took a while for you to get an answer on this!

It looks like you encountered a bug with the text query CQL behavior that was fixed in late September 2020. Now if you try your query ?cql=text~%22red+line%22, you should only get results that contain both “red” and “line”. Also, for future reference, if you want to chain multiple CQL conditions with an AND, you can do that with syntax like ?cql=text~red+and+text~line

If you want to go back to red OR line, ?cql=text~red+or+text~line will work.

Hope that helps, let me know if you run into any more troubles with CQL.
Nora

1 Like

I just ran into this problem and the answer seems to be that two sets of quotes are needed—one to wrap the text query and another to wrap the exact-phrase search.

So, your CQL query needs to look something like "text ~ '\"red line\"'".

It helped me to see an example with text outside the exact match. The search text needs to look like 'hello "red line" world' (or "hello "red line" world"). We’re just doing the exact match without the extra words around it, but the quotes still need to be there.