Gravatar for slangevin@coveo.com

Question by Simon, Nov 11, 2014 9:54 AM

Setting maximum radius when using distance search.

Hi,

Currently, the example on that page: https://developers.coveo.com/display/SearchREST/Query+Function

Takes only a latitude and longitude value to search the index. There could potentially be hundreds of results returned from the query. Some with distances close by (100-200 meters) and some with distances far away (1000's of meters). If all results are returned (close and far away) we would need to then filter those results based on a selection from the user (ie. "Show me results within 500 meters"). This would have to be done in in JavaScript after results are returned and only displayed if the distance is less than the value returned for the result.

The question is this: When we make the Query Function query can we provide a maximum value for the distance between the lat/long sent to the query and the lat/long of the result? This way only results that meet the "Show me results within 500 meters" query would be returned.

2 Replies
Gravatar for dlavoie@coveo.com

Answer by Daniel Lavoie, Nov 11, 2014 10:05 AM

The query function in your example will produce a distance field, on which you can perform all the operations typically available for a numerical field.

In your case, you could simply add @distance <= 500 to only keep results within 500 meters.

Gravatar for justin.miller@protiviti.com

Comment by sitwalkstand, Nov 14, 2014 9:40 AM

Would that operation on the distance field be performed before or after the results are queried? Is the @distance <= 500 something that would be added to the query function:

http://server/rest/search/?q=restaurants&queryFunctions=[{"function":"dist(@longitude, @latitude, 46.8167, -71.2167)","fieldName":"distance"}]

If the filter is done after the results are returned, then we could have issues with the paging controls not being in sync with the results.

Do you have an example of how that additional filter would look?

Gravatar for justin.miller@protiviti.com

Comment by sitwalkstand, Nov 14, 2014 9:49 AM

Also, is it possible to add the distance function as an expression on the queryBuilder?

I am using the JavaScript UI framework to add expressions like this:

data.queryBuilder.advancedExpression.addFieldExpression("<%= ToCoveoFieldName("state") %>", "*=", ["VA"]);

Gravatar for dlavoie@coveo.com

Comment by Daniel Lavoie, Nov 14, 2014 10:09 AM

The filter is applied by the query engine while it is evaluating the query, so everything will work as if the distance was actually a real field on the document.

So basically, you query (@distance <= 500) as if @distance was a real field, and the query function mechanism does the work in the query engine, and the distance field is available when it's time to match documents. It's not a filter done on the results set.

Gravatar for mlaporte@coveo.com

Comment by Martin Laporte, Nov 14, 2014 11:21 AM

You can inject ranking functions in any query expression, yes. The syntax is:

$qrf(expression: 'the_ranking_function_goes_here')

So in JS you do something like:

queryBuilder.advancedExpression.add("$qrf(...)");

Gravatar for mlaporte@coveo.com

Comment by Martin Laporte, Nov 14, 2014 11:46 AM

Dan informs me that you were referring to Query Functions and not Ranking Functions. But it's pretty similar. The query syntax is:

$qf(function: 'the_query_function', fieldName: '@something')

Gravatar for justin.miller@protiviti.com

Answer by sitwalkstand, Dec 5, 2014 2:05 PM

When I attempt to call our REST Api with this query string:

?q=home&queryFunctions=[{"function":"dist(@longitude,@latitude,46.8167,-71.2167)","fieldName":"distance"}]

I get the following back in the results:

"exception":{"code":"InvalidQueryFunctionSyntax","context":"distance"}

The format is the same that I found in the Query Function example found here: https://developers.coveo.com/display/public/SearchREST/Query+Function

Any ideas what syntax I should be using?

Ask a question