Skip to content

Commit

Permalink
SRCH-1406 - Update routed query feature in the API (#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrae authored May 11, 2020
1 parent aa97428 commit b0549ac
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 126 deletions.
11 changes: 6 additions & 5 deletions app/controllers/api/v2/searches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,18 @@ def affiliate_docs_search_class
end

def handle_query_routing
return unless search_params[:query].present? and query_routing_is_enabled?
affiliate = @search_options.site
routed_query = affiliate.routed_queries
.joins(:routed_query_keywords)
.where(routed_query_keywords:{keyword: search_params[:query]})
.first
respond_with({ redirect: routed_query[:url] }, { status: 200 }) unless routed_query.nil?
end

def query_routing_is_enabled?
search_params[:routed] == 'true'
return unless routed_query

RoutedQueryImpressionLogger.log(affiliate,
@search_options.query, request)

respond_with({ route_to: routed_query[:url] }, { status: 200 })
end

def search_params
Expand Down
40 changes: 40 additions & 0 deletions app/views/sites/api_instructions/_show_web_md.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
:markdown
<!-- Reminder to update https://open.gsa.gov/api/searchgov-results/ with any changes here. -->

This API exposes all relevant results “modules” in a single JSON call, including:


Expand Down Expand Up @@ -369,6 +371,44 @@



## Routed Queries


If you have [routed queries](https://search.gov/manual/routed-queries.html) set in your Admin page, then any matching query terms will change the API response.

For example, if you set queries for `example` to route to `https://search.gov` then the following API call:

`#{api_scheme_and_host}/api/v2/search?affiliate=#{h(@site.name)}&access_key=#{h(@site.api_access_key)}&query=example`

Will then return this response:

`{"route_to":"https://search.gov"}`

With this response you can redirect your users to the url. Here is an example with javascript:

```javascript
var request = new XMLHttpRequest();
var apiUrl = '#{api_scheme_and_host}/api/v2/search?affiliate=#{h(@site.name)}&access_key=#{h(@site.api_access_key)}&query=example'

request.open('GET', apiUrl, true);

request.onload = function() {
if (this.status >= 200 && this.status < 400) {
// Success!
var data = JSON.parse( this.response );
window.location.replace( data.route_to );
} else {
// We reached our target server, but it returned an error
}
};

request.onerror = function() {
// There was a connection error of some sort
};

request.send();
```


## I14y API

Expand Down
Loading

0 comments on commit b0549ac

Please sign in to comment.