From 1cfddea57835e068479b5ab73d9bda4388429ccf Mon Sep 17 00:00:00 2001 From: Michael Peter Christen Date: Tue, 16 Dec 2014 13:18:49 +0100 Subject: [PATCH] added (very experimental) Solr response writer for snapshot image results --- .../responsewriter/HTMLResponseWriter.java | 4 +- .../SnapshotImagesReponseWriter.java | 74 +++++++++++++++++++ .../yacy/http/servlets/SolrSelectServlet.java | 2 + 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 source/net/yacy/cora/federate/solr/responsewriter/SnapshotImagesReponseWriter.java diff --git a/source/net/yacy/cora/federate/solr/responsewriter/HTMLResponseWriter.java b/source/net/yacy/cora/federate/solr/responsewriter/HTMLResponseWriter.java index 5b49a7c02..c3bf84719 100644 --- a/source/net/yacy/cora/federate/solr/responsewriter/HTMLResponseWriter.java +++ b/source/net/yacy/cora/federate/solr/responsewriter/HTMLResponseWriter.java @@ -52,7 +52,7 @@ import org.apache.solr.search.SolrIndexSearcher; public class HTMLResponseWriter implements QueryResponseWriter { private static final Set DEFAULT_FIELD_LIST = null; - private static final Pattern dqp = Pattern.compile("\""); + public static final Pattern dqp = Pattern.compile("\""); public HTMLResponseWriter() { super(); @@ -180,7 +180,7 @@ public class HTMLResponseWriter implements QueryResponseWriter { writer.write("\n"); } - static final LinkedHashMap translateDoc(final IndexSchema schema, final Document doc) { + public static final LinkedHashMap translateDoc(final IndexSchema schema, final Document doc) { List fields = doc.getFields(); int sz = fields.size(); int fidx1 = 0, fidx2 = 0; diff --git a/source/net/yacy/cora/federate/solr/responsewriter/SnapshotImagesReponseWriter.java b/source/net/yacy/cora/federate/solr/responsewriter/SnapshotImagesReponseWriter.java new file mode 100644 index 000000000..f2b4c5c26 --- /dev/null +++ b/source/net/yacy/cora/federate/solr/responsewriter/SnapshotImagesReponseWriter.java @@ -0,0 +1,74 @@ +package net.yacy.cora.federate.solr.responsewriter; + +import java.io.IOException; +import java.io.Writer; +import java.util.HashSet; +import java.util.Set; + +import net.yacy.search.schema.CollectionSchema; + +import org.apache.lucene.document.Document; +import org.apache.solr.common.util.NamedList; +import org.apache.solr.request.SolrQueryRequest; +import org.apache.solr.response.QueryResponseWriter; +import org.apache.solr.response.ResultContext; +import org.apache.solr.response.SolrQueryResponse; +import org.apache.solr.search.DocIterator; +import org.apache.solr.search.DocList; +import org.apache.solr.search.SolrIndexSearcher; + +/** + * this writer is supposed to be used to generate iframes. It generates links for the /api/snapshot.jpg servlet. + */ +public class SnapshotImagesReponseWriter implements QueryResponseWriter { + + private static final Set DEFAULT_FIELD_LIST = new HashSet<>(); + + static { + DEFAULT_FIELD_LIST.add(CollectionSchema.id.getSolrFieldName()); + DEFAULT_FIELD_LIST.add(CollectionSchema.sku.getSolrFieldName()); + } + + public SnapshotImagesReponseWriter() { + super(); + } + + @Override + public String getContentType(SolrQueryRequest arg0, SolrQueryResponse arg1) { + return "text/html"; + } + + @Override + public void init(@SuppressWarnings("rawtypes") NamedList n) { + } + + @Override + public void write(final Writer writer, final SolrQueryRequest request, final SolrQueryResponse rsp) throws IOException { + NamedList values = rsp.getValues(); + assert values.get("responseHeader") != null; + assert values.get("response") != null; + + writer.write("\n"); + writer.write("\n"); + NamedList paramsList = request.getOriginalParams().toNamedList(); + paramsList.remove("wt"); + + DocList response = ((ResultContext) values.get("response")).docs; + final int sz = response.size(); + if (sz > 0) { + SolrIndexSearcher searcher = request.getSearcher(); + DocIterator iterator = response.iterator(); + while (iterator.hasNext()) { + int id = iterator.nextDoc(); + Document doc = searcher.doc(id, DEFAULT_FIELD_LIST); + String urlhash = doc.getField(CollectionSchema.id.getSolrFieldName()).stringValue(); + String url = doc.getField(CollectionSchema.sku.getSolrFieldName()).stringValue(); + writer.write("\""\n"); + } + } + + writer.write("\n"); + + } + +} diff --git a/source/net/yacy/http/servlets/SolrSelectServlet.java b/source/net/yacy/http/servlets/SolrSelectServlet.java index 979943342..0d71ec82a 100644 --- a/source/net/yacy/http/servlets/SolrSelectServlet.java +++ b/source/net/yacy/http/servlets/SolrSelectServlet.java @@ -49,6 +49,7 @@ import net.yacy.cora.federate.solr.responsewriter.GSAResponseWriter; import net.yacy.cora.federate.solr.responsewriter.GrepHTMLResponseWriter; import net.yacy.cora.federate.solr.responsewriter.HTMLResponseWriter; import net.yacy.cora.federate.solr.responsewriter.OpensearchResponseWriter; +import net.yacy.cora.federate.solr.responsewriter.SnapshotImagesReponseWriter; import net.yacy.cora.federate.solr.responsewriter.YJsonResponseWriter; import net.yacy.search.Switchboard; import net.yacy.search.SwitchboardConstants; @@ -98,6 +99,7 @@ public class SolrSelectServlet extends HttpServlet { RESPONSE_WRITER.put("xslt", xsltWriter); // try i.e. http://localhost:8090/solr/select?q=*:*&start=0&rows=10&wt=xslt&tr=json.xsl RESPONSE_WRITER.put("exml", new EnhancedXMLResponseWriter()); RESPONSE_WRITER.put("html", new HTMLResponseWriter()); + RESPONSE_WRITER.put("snapshots", new SnapshotImagesReponseWriter()); RESPONSE_WRITER.put("grephtml", new GrepHTMLResponseWriter()); RESPONSE_WRITER.put("rss", opensearchResponseWriter); //try http://localhost:8090/solr/select?wt=rss&q=olympia&hl=true&hl.fl=text_t,h1,h2 RESPONSE_WRITER.put("opensearch", opensearchResponseWriter); //try http://localhost:8090/solr/select?wt=rss&q=olympia&hl=true&hl.fl=text_t,h1,h2