Gravatar for hussaindotnet@gmail.com

Question by Hussain Shaik, Feb 23, 2017 11:38 AM

Get search results directly by sending query

I need to get the results from coveo JS by sending query parameter. I am using coveo cloud and JS framework. I don't want to hook the event to search box.

Just pass the query and get the results as JSON and then process the results. Any help ?

1 Reply
Gravatar for olamothe@coveo.com

Answer by olamothe, Feb 23, 2017 12:31 PM

You can use the SearchEndpoint class exposed by the UI.

https://coveo.github.io/search-ui/classes/searchendpoint.html

You can use the search method, and build the query.

A good way to build a query correctly is by using a QueryBuilder : https://coveo.github.io/search-ui/classes/querybuilder.html

Example :

var endpoint = Coveo.SearchEndpoint.endpoints['default'];

var queryBuilder = new Coveo.QueryBuilder();
queryBuilder.expression.add('foo');

var query = queryBuilder.build();

endpoint.search(query).then(function(data) { 
   // you can iterate on data.results here;
}) 

There'se a lot of parameters you can set on the query builder (expression, advancedExpression, wildcards, groupBy, etc.). When you call build, you receive an IQuery ( https://coveo.github.io/search-ui/interfaces/iquery.html ) which you can pass to the search method on the endpoint.

The expected data type and return type of each function is documented in the reference website links I added.

Gravatar for hussaindotnet@gmail.com

Comment by Hussain Shaik, Feb 23, 2017 12:36 PM

Excellent.. This helps me lot. Thanks !!!

Ask a question