Skip to content
This repository has been archived by the owner on Dec 5, 2020. It is now read-only.

Commit

Permalink
ElasticSearchResults implements SearchResultsPage
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Sep 6, 2017
1 parent 2e24d5a commit 5da435f
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 145 deletions.
6 changes: 1 addition & 5 deletions src/main/java/com/amihaiemil/charles/aws/ElasticSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,9 @@ public void delete(String type, String id) {
//Fake delete; nothing to do.
}

/**
* @todo 261:30min After SearchResultsPage is refactored and broken
* into an interface + smart implmentation, provide a Fake one and return it here.
*/
@Override
public SearchResultsPage search(SearchQuery query) {
return null;
return new SearchResultsPage.Fake();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,16 @@
*/
package com.amihaiemil.charles.aws;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.http.HttpResponse;
import com.amazonaws.http.HttpResponseHandler;
import com.amihaiemil.charles.rest.model.ElasticSearchResult;
import com.amihaiemil.charles.rest.model.SearchResult;
import com.amihaiemil.charles.rest.model.ElasticSearchResults;
import com.amihaiemil.charles.rest.model.SearchResultsPage;

/**
* Response handler that parses the search response into a {@link SearchResultsPage}
* Response handler that parses the search response into a {@link oldSearchResultsPage}
* @author Mihai Andronache ([email protected])
* @version $Id$
* @since 1.0.0
Expand All @@ -50,41 +43,23 @@
public final class SearchResponseHandler implements HttpResponseHandler<SearchResultsPage>{

@Override
public SearchResultsPage handle(HttpResponse response) {
public ElasticSearchResults handle(HttpResponse response) {
int status = response.getStatusCode();
if(status < 200 || status >= 300) {
AmazonServiceException ase = new AmazonServiceException("Unexpected status: " + status);
ase.setStatusCode(status);
throw ase;
}
return this.buildResultsPage(response);
return new ElasticSearchResults(
Json.createReader(
response.getContent()
).readObject()
);
}

@Override
public boolean needsConnectionLeftOpen() {
return false;
}

/**
* Build the search results page
* @param response
* @return
*/
private SearchResultsPage buildResultsPage(HttpResponse response) {
SearchResultsPage page = new SearchResultsPage();
InputStream content = response.getContent();
JsonObject result = Json.createReader(content).readObject();
int totalHits = result.getJsonObject("hits").getInt("total");
if(totalHits != 0) {
List<SearchResult> searchResults = new ArrayList<SearchResult>();
JsonArray hits = result.getJsonObject("hits").getJsonArray("hits");
for(int i=0; i<hits.size(); i++) {
searchResults.add(new ElasticSearchResult(hits.getJsonObject(i)));
}
page.setResults(searchResults);
page.setTotalHits(totalHits);
}
return page;
}

}
16 changes: 8 additions & 8 deletions src/main/java/com/amihaiemil/charles/rest/CharlesResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,28 +158,28 @@ public Response search(
String queryStringFormat = "?kw=%s&ctg=%s&index=%s&size=%s";
String requestUrl = servletRequest.getRequestURL().toString();
if(idx == 0) {
results.setPreviousPage("-");
results = results.withPrevPage("-");
} else {
String queryString = String.format(queryStringFormat, keywords, category, idx - nr, nr);
results.setPreviousPage(requestUrl + queryString);
results = results.withPrevPage(requestUrl + queryString);
}
if(idx + nr >= results.getTotalHits()) {
results.setNextPage("-");
if(idx + nr >= results.totalHits()) {
results = results.withNextPage("-");
} else {
String queryString = String.format(queryStringFormat, keywords, category, idx + nr, nr);
results.setNextPage(requestUrl + queryString);
results = results.withNextPage(requestUrl + queryString);
}
results.setPageNr(idx/nr + 1);
results = results.withPageNr(idx/nr + 1);

int start = 0;
List<String> pagesLinks = new ArrayList<String>();
while(start < results.getTotalHits()) {
while(start < results.totalHits()) {
pagesLinks.add(
requestUrl + String.format(queryStringFormat, keywords, category, start, nr)
);
start += nr;
}
results.setPages(pagesLinks);
results = results.withPages(pagesLinks);

return Response.ok().entity(new ObjectMapper().writeValueAsString(results)).build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/**
* Copyright (c) 2016-2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of charles-rest nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.amihaiemil.charles.rest.model;

import java.util.ArrayList;
import java.util.List;
import javax.json.JsonArray;
import javax.json.JsonObject;

/**
* A page of search results coming from ElasticSearch.
* @author Mihai Andronache ([email protected])
* @version $Id$
* @since 1.0.2
*/
public final class ElasticSearchResults implements SearchResultsPage {

/**
* Search results in JSON.
*/
private JsonObject results;

private int pageNr;
private String nextPage;
private String previousPage;
private List<String> pages;


/**
* Ctor.
* @param hits Results in JSON.
*/
public ElasticSearchResults(final JsonObject results) {
this(results, 0, "", "", new ArrayList<String>());
}

/**
* Ctor.
* @param results Json results from ElasticSearch.
* @param pageNr Number of this page.
* @param nextPage Link to the next page.
* @param preciousPage Link to the previous page.
* @param pages Links to all the pages.
*/
public ElasticSearchResults(
final JsonObject results, final int pageNr,
final String nextPage, final String previousPage,
final List<String> pages
) {
this.results = results;
this.pageNr = pageNr;
this.nextPage = nextPage;
this.previousPage = previousPage;
this.pages = pages;
}

@Override
public List<SearchResult> results() {
final List<SearchResult> res = new ArrayList<>();
final JsonArray hits = this.results.getJsonObject("hits").getJsonArray("hits");
for(int i=0; i<hits.size(); i++) {
res.add(new ElasticSearchResult(hits.getJsonObject(i)));
}
return res;
}

@Override
public int totalHits() {
return this.results.getJsonObject("hits").getInt("total");
}

@Override
public int pageNr() {
return this.pageNr;
}

@Override
public String previousPage() {
return this.previousPage;
}

@Override
public String nextPage() {
return this.nextPage;
}

@Override
public List<String> pages() {
return this.pages;
}

@Override
public SearchResultsPage withPageNr(int pageNr) {
return new ElasticSearchResults(this.results, pageNr, this.nextPage, this.previousPage, this.pages);
}

@Override
public SearchResultsPage withNextPage(String nextPage) {
return new ElasticSearchResults(this.results, this.pageNr, nextPage, this.previousPage, this.pages);
}

@Override
public SearchResultsPage withPrevPage(String prevPage) {
return new ElasticSearchResults(this.results, this.pageNr, this.nextPage, prevPage, this.pages);
}

@Override
public SearchResultsPage withPages(List<String> pages) {
return new ElasticSearchResults(this.results, this.pageNr, this.nextPage, this.previousPage, pages);
}

}
Loading

0 comments on commit 5da435f

Please sign in to comment.