Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added API spec parameters fetch, snippet

  • Loading branch information...
commit 5a742dd463c2766be0951c5dddae9ae088431c55 1 parent d12a8d9
@ghosthack ghosthack authored
Showing with 67 additions and 31 deletions.
  1. +67 −31 embedded-api/com/flaptor/indextank/api/resources/Search.java
View
98 embedded-api/com/flaptor/indextank/api/resources/Search.java
@@ -22,6 +22,8 @@
import java.util.Map.Entry;
import java.util.logging.Logger;
+import javax.servlet.http.HttpServletResponse;
+
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@@ -35,13 +37,10 @@
import com.flaptor.indextank.search.SearchResult;
import com.flaptor.indextank.search.SearchResults;
import com.ghosthack.turismo.action.Action;
-import com.ghosthack.turismo.servlet.Env;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multiset;
-import javax.servlet.http.HttpServletResponse;
-
public class Search extends Action {
/**
@@ -60,6 +59,10 @@ public void run() {
}
String q = params("q");
+ String fetchVariables = params("fetch_variables");
+ String fetchCategories = params("fetch_categories");
+ String fetch = params("fetch");
+ String snippet = params("snippet");
int start = QueryHelper.parseIntParam(params("start"), 0);
int len = QueryHelper.parseIntParam(params("end"), 10);
int function = QueryHelper.parseIntParam(params("function"), 0);
@@ -67,44 +70,24 @@ public void run() {
List<CategoryFilter> facetFilters = Lists.newArrayList();
List<RangeFilter> variableRangeFilters = Lists.newArrayList();
List<RangeFilter> functionRangeFilters = Lists.newArrayList();
- Map<String, String> extras = Maps.newHashMap();
- extras.put("fetch_variables", "*");
- extras.put("fetch_categories", "*");
- extras.put("fetch_fields", "*");
- //extras.put("snippet_fields", "*");
- //extras.put("snippet_type", "*");
+ Map<String, String> extras = createExtraParameters(fetch, snippet,
+ fetchVariables, fetchCategories);
try {
long t0 = System.currentTimeMillis();
SearchResults results = api.search(q, start, len, function, vars, facetFilters, variableRangeFilters, functionRangeFilters, extras);
long t1 = System.currentTimeMillis();
double searchTime = (t1 - t0) / 1000;
+ int matches = results.getMatches();
+ Map<String, Map<String, Integer>> facets = toFacets(results.getFacets());
+ String didYouMean = results.getDidYouMean();
JSONArray ja = new JSONArray();
for(SearchResult result: results.getResults()) {
- JSONObject document = new JSONObject();
- document.putAll(result.getFields());
- document.put("docid", result.getDocId());
- document.put("query_relevance_score", result.getScore());
- for(Entry<Integer, Double> entry: result.getVariables().entrySet()) {
- document.put("variable_" + entry.getKey(), entry.getValue());
- }
- for(Entry<String, String> entry: result.getCategories().entrySet()) {
- document.put("category_" + entry.getKey(), entry.getValue());
- }
- ja.add(document);
+ addResult(ja, result);
}
-
- JSONObject jo = new JSONObject();
- jo.put("query", q);
- jo.put("results", ja);
- jo.put("matches", results.getMatches());
- jo.put("facets", toFacets(results.getFacets()));
- String didYouMean = results.getDidYouMean();
- if(didYouMean != null) {
- jo.put("didyoumean", didYouMean);
- }
- jo.put("search_time", String.format("%.3f", searchTime));
+
+ JSONObject jo = createResponse(q, searchTime, ja, matches, facets, didYouMean);
print(jo.toJSONString());
return;
@@ -125,6 +108,59 @@ public void run() {
}
@SuppressWarnings("unchecked")
+ private JSONObject createResponse(String q, double searchTime,
+ JSONArray ja, int matches,
+ Map<String, Map<String, Integer>> facets, String didYouMean) {
+ JSONObject jo = new JSONObject();
+ jo.put("query", q);
+ jo.put("results", ja);
+ jo.put("matches", matches);
+ jo.put("facets", facets);
+ if(didYouMean != null) {
+ jo.put("didyoumean", didYouMean);
+ }
+ jo.put("search_time", String.format("%.3f", searchTime));
+ return jo;
+ }
+
+ private Map<String, String> createExtraParameters(String fetch,
+ String snippet, String fetchVariables, String fetchCategories) {
+ Map<String, String> extras = Maps.newHashMap();
+ if("true".equalsIgnoreCase(fetchVariables) || "*".equals(fetchVariables)) {
+ if(LOG_ENABLED) LOG.fine("Fetch variables: all");
+ extras.put("fetch_variables", "*");
+ }
+ if("true".equalsIgnoreCase(fetchCategories) || "*".equals(fetchCategories)) {
+ if(LOG_ENABLED) LOG.fine("Fetch categories: all");
+ extras.put("fetch_categories", "*");
+ }
+ if(fetch != null) {
+ if(LOG_ENABLED) LOG.fine("Fetch fields: " + fetch);
+ extras.put("fetch_fields", fetch);
+ }
+ if(snippet != null) {
+ if(LOG_ENABLED) LOG.fine("Fetch snippets: " + snippet);
+ extras.put("snippet_fields", snippet);
+ }
+ return extras;
+ }
+
+ @SuppressWarnings("unchecked")
+ private void addResult(JSONArray ja, SearchResult result) {
+ JSONObject document = new JSONObject();
+ document.putAll(result.getFields());
+ document.put("docid", result.getDocId());
+ document.put("query_relevance_score", result.getScore());
+ for(Entry<Integer, Double> entry: result.getVariables().entrySet()) {
+ document.put("variable_" + entry.getKey(), entry.getValue());
+ }
+ for(Entry<String, String> entry: result.getCategories().entrySet()) {
+ document.put("category_" + entry.getKey(), entry.getValue());
+ }
+ ja.add(document);
+ }
+
+ @SuppressWarnings("unchecked")
private static Map<String, Map<String, Integer>> toFacets(Map<String, Multiset<String>> facets) {
JSONObject results = new JSONObject();
for (Entry<String, Multiset<String>> entry : facets.entrySet()) {
Please sign in to comment.
Something went wrong with that request. Please try again.