How do I write an advanced query with AND, OR and NOT?
The advanced query language lets you combine conditions with $and, $or and $not. Example: articles about Bitcoin from the top 100 business sources, excluding paywalled sources, in English, in a date range:
{
"query": {
"$query": {
"$and": [
{ "conceptUri": "http://en.wikipedia.org/wiki/Bitcoin" },
{ "sourceGroupUri": "business/top100" },
{ "lang": "eng", "dateStart": "2026-01-01", "dateEnd": "2026-01-31" }
],
"$not": { "sourceGroupUri": "paywall/paywalled_sources" }
},
"$filter": { "isDuplicate": "skipDuplicates" }
},
"resultType": "articles",
"articlesSortBy": "date",
"apiKey": "YOUR_API_KEY"
}Rules that are often missed:
- Conditions such as
dateStart,dateEnd,langandsourceLocationUrigo into an object inside the$andarray, not next to it. keywordLocandkeywordSearchModebelong in the same object as thekeywordthey apply to.- The
ignore...parameters (ignoreKeyword, ignoreSourceUri, ignoreSourceGroupUri...) can't be used in the query language. Put the things to exclude inside$not, e.g."$not": { "sourceGroupUri": "paywall/paywalled_sources" }. - Filters that apply to the whole result (duplicates, data type, source rank) go into
$filter.
The easiest way to build such a query is the sandbox, which shows the generated JSON. The full description of the language is in the Python SDK wiki (bottom of the page) and in the documentation.