Proximity search with NEAR
Find articles with related terms in close proximity
Overview
Proximity search is a powerful technique that lets you find articles where specific terms appear close to each other. This method is particularly useful to ensure that related concepts are discussed in the same context within an article.
News API v3 supports proximity search through the NEAR
operator. This operator
lets you specify two or more terms and the maximum number of words that can
appear between them.
By using the NEAR
operator, you can significantly improve the relevance of
your search results, finding articles where related terms are discussed in close
proximity to each other.
Before you start
Before you begin, ensure you have:
- An active API key for NewsCatcher News API v3
- Basic knowledge of making API requests
- Python or another tool for making HTTP requests (cURL, Postman, or a programming language with HTTP capabilities)
Steps
Understand NEAR syntax
The NEAR
operator uses the following format:
phrase_A
andphrase_B
: The terms or phrases you want to find near each other (max 4 words per phrase).distance
: The maximum number of words that can appear between the phrases (max 100).in_order
: Optional boolean parameter. If true,phrase_B
must appear afterphrase_A
. Defaults to false.
Construct your query
Here are some examples of NEAR
queries:
NEAR("climate change", "renewable energy", 15)
NEAR("artificial intelligence", "healthcare", 20, true)
Note the following limitations:
- Maximum 4 words per phrase
- Maximum 3 phrases per
NEAR
operation - Maximum distance of 100 words
Make an API request
Use the /search
endpoint with your constructed query in the q
parameter.
Here’s a Python code example:
Analyze the results
The API returns a JSON response similar to other search queries. Here’s an example of what you might see:
Refine your query as needed
If you’re not getting the desired results, try adjusting your query:
- Change the distance parameter to narrow or broaden your search
- Add more terms to the
NEAR
operator (up to 3 phrases) - Combine
NEAR
with boolean operators for more complex queries. For example:
See also
Was this page helpful?