Permalink
Browse files

Merge pull request #21 from adrnai/master

Search query params spec
  • Loading branch information...
2 parents 5743b43 + 17271fe commit 04336e9bcd30604ee549e044bd1bc7c780d038b4 @iladriano iladriano committed Jan 19, 2012
Showing with 67 additions and 37 deletions.
  1. +67 −31 embedded-api/com/flaptor/indextank/api/resources/Search.java
  2. +0 −6 pom.xml
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,51 +59,35 @@ 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);
Map<Integer, Double> vars = Maps.newHashMap();
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()) {
View
6 pom.xml
@@ -188,12 +188,6 @@
<scope>test</scope>
</dependency>
</dependencies>
- <repositories>
- <repository>
- <id>sonatype</id>
- <url>https://oss.sonatype.org/content/repositories/releases</url>
- </repository>
- </repositories>
<developers>
<developer>
<id>adrnai</id>

0 comments on commit 04336e9

Please sign in to comment.