Skip to content

Commit

Permalink
added default facet fields for json response format (stub)
Browse files Browse the repository at this point in the history
  • Loading branch information
Orbiter committed Sep 14, 2012
1 parent 2f218df commit 975bc95
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 18 deletions.
2 changes: 1 addition & 1 deletion htroot/gsa/searchresult.java
Expand Up @@ -149,7 +149,7 @@ public static serverObjects respond(final RequestHeader header, final serverObje
if (connector == null) return null;

// do the solr request
SolrQueryRequest req = connector.request(post.toSolrParams());
SolrQueryRequest req = connector.request(post.toSolrParams(null));
SolrQueryResponse response = null;
Exception e = null;
try {response = connector.query(req);} catch (SolrException ee) {e = ee;}
Expand Down
11 changes: 8 additions & 3 deletions htroot/solr/select.java
Expand Up @@ -38,11 +38,13 @@
import net.yacy.kelondro.logging.Log;
import net.yacy.search.Switchboard;
import net.yacy.search.SwitchboardConstants;
import net.yacy.search.index.YaCySchema;
import net.yacy.search.query.AccessTracker;
import net.yacy.search.query.SnippetProcess;

import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.FastWriter;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.core.SolrCore;
Expand Down Expand Up @@ -154,7 +156,9 @@ public static serverObjects respond(final RequestHeader header, final serverObje
"") : env.getConfig(SwitchboardConstants.GREETING, "");
((OpensearchResponseWriter) responseWriter).setTitle(promoteSearchPageGreeting);
}
if (responseWriter instanceof OpensearchResponseWriter) {

// if this is a call to YaCys special search formats, enhance the query with field assignments
if (responseWriter instanceof JsonResponseWriter || responseWriter instanceof OpensearchResponseWriter) {
// add options for snippet generation
post.put("hl", "true");
post.put("hl.fl", "text_t,h1,h2");
Expand All @@ -167,8 +171,9 @@ public static serverObjects respond(final RequestHeader header, final serverObje
EmbeddedSolrConnector connector = (EmbeddedSolrConnector) sb.index.fulltext().getLocalSolr();
if (connector == null) return null;

// do the solr request
SolrQueryRequest req = connector.request(post.toSolrParams());
// do the solr request, generate facets if we use a special YaCy format
SolrParams params = post.toSolrParams(responseWriter instanceof JsonResponseWriter ? new YaCySchema[]{YaCySchema.host_s, YaCySchema.url_file_ext_s, YaCySchema.url_protocol_s} : null);
SolrQueryRequest req = connector.request(params);
SolrQueryResponse response = null;
Exception e = null;
try {response = connector.query(req);} catch (SolrException ee) {e = ee;}
Expand Down
8 changes: 7 additions & 1 deletion source/de/anomic/server/serverObjects.java
Expand Up @@ -470,7 +470,7 @@ public String toString() {
return param.toString();
}

public SolrParams toSolrParams() {
public SolrParams toSolrParams(YaCySchema[] facets) {
// check if all required post fields are there
if (!this.containsKey(CommonParams.DF)) this.put(CommonParams.DF, YaCySchema.text_t.name()); // set default field to all fields
if (!this.containsKey(CommonParams.START)) this.put(CommonParams.START, "0"); // set default start item
Expand All @@ -480,6 +480,12 @@ public SolrParams toSolrParams() {
for (Map.Entry<String, String> e: this.entrySet()) {
m.put(e.getKey(), new String[]{e.getValue()});
}
if (facets != null && facets.length > 0) {
m.put("facet", new String[]{"true"});
String[] fs = new String[facets.length];
for (int i = 0; i < facets.length; i++) fs[i] = facets[i].name();
m.put("facet.field", fs);
}
final SolrParams solrParams = new MultiMapSolrParams(m);
return solrParams;
}
Expand Down
Expand Up @@ -67,15 +67,16 @@ public void init(@SuppressWarnings("rawtypes") NamedList n) {
@Override
public void write(final Writer writer, final SolrQueryRequest request, final SolrQueryResponse rsp) throws IOException {
writer.write(XML_START);

assert rsp.getValues().get("responseHeader") != null;
assert rsp.getValues().get("response") != null;
NamedList<?> values = rsp.getValues();

assert values.get("responseHeader") != null;
assert values.get("response") != null;

@SuppressWarnings("unchecked")
SimpleOrderedMap<Object> responseHeader = (SimpleOrderedMap<Object>) rsp.getResponseHeader();
DocSlice response = (DocSlice) rsp.getValues().get("response");
DocSlice response = (DocSlice) values.get("response");
@SuppressWarnings("unchecked")
SimpleOrderedMap<Object> highlighting = (SimpleOrderedMap<Object>) rsp.getValues().get("highlighting");
SimpleOrderedMap<Object> highlighting = (SimpleOrderedMap<Object>) values.get("highlighting");
writeProps(writer, "responseHeader", responseHeader); // this.writeVal("responseHeader", responseHeader);
writeDocs(writer, request, response); // this.writeVal("response", response);
writeProps(writer, "highlighting", highlighting);
Expand Down
Expand Up @@ -82,14 +82,21 @@ public void init(@SuppressWarnings("rawtypes") NamedList n) {

@Override
public void write(final Writer writer, final SolrQueryRequest request, final SolrQueryResponse rsp) throws IOException {
assert rsp.getValues().get("responseHeader") != null;
assert rsp.getValues().get("response") != null;

NamedList<?> values = rsp.getValues();

assert values.get("responseHeader") != null;
assert values.get("response") != null;

@SuppressWarnings("unchecked")
SimpleOrderedMap<Object> responseHeader = (SimpleOrderedMap<Object>) rsp.getResponseHeader();
DocSlice response = (DocSlice) rsp.getValues().get("response");
DocSlice response = (DocSlice) values.get("response");
@SuppressWarnings("unchecked")
SimpleOrderedMap<Object> facetCounts = (SimpleOrderedMap<Object>) values.get("facet_counts");
@SuppressWarnings("unchecked")
SimpleOrderedMap<Object> highlighting = (SimpleOrderedMap<Object>) rsp.getValues().get("highlighting");
SimpleOrderedMap<Object> facetFields = facetCounts == null || facetCounts.size() == 0 ? null : (SimpleOrderedMap<Object>) facetCounts.get("facet_fields");
@SuppressWarnings("unchecked")
SimpleOrderedMap<Object> highlighting = (SimpleOrderedMap<Object>) values.get("highlighting");
Map<String, List<String>> snippets = OpensearchResponseWriter.highlighting(highlighting);

// parse response header
Expand Down Expand Up @@ -202,6 +209,14 @@ public void write(final Writer writer, final SolrQueryRequest request, final Sol
}
writer.write("]\n".toCharArray());
writer.write(",\n\"navigation\":[\n");

@SuppressWarnings("unchecked")
NamedList<Integer> hosts = facetFields == null ? null : (NamedList<Integer>) facetFields.get(YaCySchema.host_s.name());
@SuppressWarnings("unchecked")
NamedList<Integer> exts = facetFields == null ? null : (NamedList<Integer>) facetFields.get(YaCySchema.url_file_ext_s.name());
@SuppressWarnings("unchecked")
NamedList<Integer> prots = facetFields == null ? null : (NamedList<Integer>) facetFields.get(YaCySchema.url_protocol_s.name());

writer.write("{\"facetname\":\"filetypes\",\"displayname\":\"Filetypes\",\"type\":\"String\",\"min\":\"0\",\"max\":\"0\",\"mean\":\"0\",\"elements\":[]},\n".toCharArray());
writer.write("{\"facetname\":\"protocols\",\"displayname\":\"Protocol\",\"type\":\"String\",\"min\":\"0\",\"max\":\"0\",\"mean\":\"0\",\"elements\":[]},\n".toCharArray());
writer.write("{\"facetname\":\"domains\",\"displayname\":\"Domains\",\"type\":\"String\",\"min\":\"0\",\"max\":\"0\",\"mean\":\"0\",\"elements\":[]},\n".toCharArray());
Expand Down
Expand Up @@ -94,14 +94,17 @@ public void init(@SuppressWarnings("rawtypes") NamedList n) {

@Override
public void write(final Writer writer, final SolrQueryRequest request, final SolrQueryResponse rsp) throws IOException {
assert rsp.getValues().get("responseHeader") != null;
assert rsp.getValues().get("response") != null;

NamedList<?> values = rsp.getValues();

assert values.get("responseHeader") != null;
assert values.get("response") != null;

@SuppressWarnings("unchecked")
SimpleOrderedMap<Object> responseHeader = (SimpleOrderedMap<Object>) rsp.getResponseHeader();
DocSlice response = (DocSlice) rsp.getValues().get("response");
DocSlice response = (DocSlice) values.get("response");
@SuppressWarnings("unchecked")
SimpleOrderedMap<Object> highlighting = (SimpleOrderedMap<Object>) rsp.getValues().get("highlighting");
SimpleOrderedMap<Object> highlighting = (SimpleOrderedMap<Object>) values.get("highlighting");
Map<String, List<String>> snippets = highlighting(highlighting);

// parse response header
Expand Down

0 comments on commit 975bc95

Please sign in to comment.