List of usage examples for org.apache.solr.search DocList offset
public int offset();
From source file:com.mindquarry.search.solr.request.JSONWriter.java
License:Open Source License
public void writeDocList(String name, DocList ids, Set<String> fields, Map otherFields) throws IOException { boolean includeScore = false; if (fields != null) { includeScore = fields.contains("score"); if (fields.size() == 0 || (fields.size() == 1 && includeScore) || fields.contains("*")) { fields = null; // null means return all stored fields }/*from w ww. j av a 2 s.c o m*/ } int sz = ids.size(); writer.write('{'); incLevel(); writeKey("numFound", false); writeInt(null, ids.matches()); writer.write(','); writeKey("start", false); writeInt(null, ids.offset()); if (includeScore) { writer.write(','); writeKey("maxScore", false); writeFloat(null, ids.maxScore()); } writer.write(','); // indent(); writeKey("docs", false); writer.write('['); incLevel(); boolean first = true; DocIterator iterator = ids.iterator(); for (int i = 0; i < sz; i++) { int id = iterator.nextDoc(); Document doc = searcher.doc(id); if (first) { first = false; } else { writer.write(','); } indent(); writeDoc(null, doc, fields, (includeScore ? iterator.score() : 0.0f), includeScore); } decLevel(); writer.write(']'); if (otherFields != null) { writeMap(null, otherFields, true, false); } decLevel(); indent(); writer.write('}'); }
From source file:com.mindquarry.search.solr.request.QuickSearchSearchJSONWriter.java
License:Open Source License
public void writeDocList(String name, DocList ids, Set<String> fields, Map otherFields) throws IOException { boolean includeScore = false; if (fields != null) { includeScore = fields.contains("score"); //$NON-NLS-1$ if (fields.size() == 0 || (fields.size() == 1 && includeScore) || fields.contains("*")) { //$NON-NLS-1$ fields = null; // null means return all stored fields }//from w ww. j av a2 s.c o m } int sz = ids.size(); writer.write('{'); incLevel(); writeKey("numFound", false); //$NON-NLS-1$ writeInt(null, ids.matches()); writer.write(','); writeKey("start", false); //$NON-NLS-1$ writeInt(null, ids.offset()); if (includeScore) { writer.write(','); writeKey("maxScore", false); //$NON-NLS-1$ writeFloat(null, ids.maxScore()); } writer.write(','); writeKey("docs", false); //$NON-NLS-1$ writer.write('{'); incLevel(); HashMap<String, List<DocWithScore>> sets = new HashMap<String, List<DocWithScore>>(); DocIterator iterator = ids.iterator(); for (int i = 0; i < sz; i++) { int id = iterator.nextDoc(); Document doc = searcher.doc(id); for (Fieldable ff : (List<Fieldable>) doc.getFields()) { String fname = ff.name(); String fval = ff.stringValue(); if (fname.equals("type")) { //$NON-NLS-1$ String type = fval; if (type.equals("wiki")) { //$NON-NLS-1$ type = type.replaceFirst("w", "W"); //$NON-NLS-1$ //$NON-NLS-2$ } else if (type.equals("tasks")) { //$NON-NLS-1$ type = type.replaceFirst("t", "T"); //$NON-NLS-1$ //$NON-NLS-2$ } if (!sets.containsKey(type)) { sets.put(type, new ArrayList<DocWithScore>()); } if (includeScore) { sets.get(type).add(new DocWithScore(doc, iterator.score())); } else { sets.get(type).add(new DocWithScore(doc, 0.0f)); } } } } boolean firstSet = true; Set<String> keys = sets.keySet(); Iterator<String> kIt = keys.iterator(); while (kIt.hasNext()) { String key = kIt.next(); List<DocWithScore> docs = sets.get(key); if (firstSet) { firstSet = false; } else { writer.write(','); } indent(); writeKey(key, false); writer.write('['); incLevel(); boolean firstDoc = true; for (Iterator iter = docs.iterator(); iter.hasNext();) { DocWithScore doc = (DocWithScore) iter.next(); if (firstDoc) { firstDoc = false; } else { writer.write(','); } writer.write('{'); boolean firstEntry = true; for (Fieldable ff : (List<Fieldable>) doc.getDoc().getFields()) { String fname = ff.name(); if ((fname.equals("location")) || (fname.equals("title"))) { //$NON-NLS-1$ //$NON-NLS-2$ if (firstEntry) { firstEntry = false; } else { writer.write(','); } indent(); writeKey(fname, false); if (fname.equals("location")) { //$NON-NLS-1$ writeStr(null, transformToWebPath(ff.stringValue()), true); } else { writeStr(null, ff.stringValue(), true); } } } // write score if (includeScore) { writer.write(','); indent(); writeKey("score", false); //$NON-NLS-1$ Float score = doc.getScore() * 100; int absScore = score.intValue(); writeInt(null, absScore); } indent(); writer.write('}'); } writer.write(']'); } decLevel(); indent(); writer.write('}'); if (otherFields != null) { writeMap(null, otherFields, true, false); } decLevel(); indent(); writer.write('}'); }
From source file:net.yacy.cora.federate.solr.connector.EmbeddedSolrConnector.java
License:Open Source License
/** * conversion from a SolrQueryResponse (which is a solr-internal data format) to SolrDocumentList (which is a solrj-format) * The conversion is done inside the solrj api using the BinaryResponseWriter and a very complex unfolding process * via org.apache.solr.common.util.JavaBinCodec.marshal. * @param request// ww w . jav a 2 s.c o m * @param sqr * @return */ public SolrDocumentList SolrQueryResponse2SolrDocumentList(final SolrQueryRequest req, final SolrQueryResponse rsp) { SolrDocumentList sdl = new SolrDocumentList(); NamedList<?> nl = rsp.getValues(); ResultContext resultContext = (ResultContext) nl.get("response"); DocList response = resultContext == null ? new DocSlice(0, 0, new int[0], new float[0], 0, 0.0f) : resultContext.docs; sdl.setNumFound(response == null ? 0 : response.matches()); sdl.setStart(response == null ? 0 : response.offset()); String originalName = Thread.currentThread().getName(); if (response != null) { try { SolrIndexSearcher searcher = req.getSearcher(); final int responseCount = response.size(); DocIterator iterator = response.iterator(); for (int i = 0; i < responseCount; i++) { int docid = iterator.nextDoc(); Thread.currentThread() .setName("EmbeddedSolrConnector.SolrQueryResponse2SolrDocumentList: " + docid); Document responsedoc = searcher.doc(docid, (Set<String>) null); SolrDocument sordoc = doc2SolrDoc(responsedoc); sdl.add(sordoc); } } catch (IOException e) { ConcurrentLog.logException(e); } } Thread.currentThread().setName(originalName); return sdl; }
From source file:net.yacy.cora.federate.solr.responsewriter.EnhancedXMLResponseWriter.java
License:Open Source License
private static final void writeDocs(final Writer writer, final SolrQueryRequest request, final DocList response) throws IOException { boolean includeScore = false; final int sz = response.size(); writer.write("<result"); writeAttr(writer, "name", "response"); writeAttr(writer, "numFound", Long.toString(response.matches())); writeAttr(writer, "start", Long.toString(response.offset())); if (includeScore) { writeAttr(writer, "maxScore", Float.toString(response.maxScore())); }/* www . jav a2 s .c om*/ if (sz == 0) { writer.write("/>"); return; } writer.write('>'); writer.write(lb); SolrIndexSearcher searcher = request.getSearcher(); DocIterator iterator = response.iterator(); includeScore = includeScore && response.hasScores(); IndexSchema schema = request.getSchema(); for (int i = 0; i < sz; i++) { int id = iterator.nextDoc(); Document doc = searcher.doc(id, DEFAULT_FIELD_LIST); writeDoc(writer, schema, null, doc.getFields(), (includeScore ? iterator.score() : 0.0f), includeScore); } writer.write("</result>"); writer.write(lb); }
From source file:net.yacy.cora.federate.solr.responsewriter.GSAResponseWriter.java
License:Open Source License
@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; long start = System.currentTimeMillis(); SimpleOrderedMap<Object> responseHeader = (SimpleOrderedMap<Object>) rsp.getResponseHeader(); DocList response = ((ResultContext) rsp.getValues().get("response")).docs; @SuppressWarnings("unchecked") SimpleOrderedMap<Object> highlighting = (SimpleOrderedMap<Object>) rsp.getValues().get("highlighting"); Map<String, LinkedHashSet<String>> snippets = OpensearchResponseWriter.highlighting(highlighting); Map<Object, Object> context = request.getContext(); // parse response header ResHead resHead = new ResHead(); NamedList<?> val0 = (NamedList<?>) responseHeader.get("params"); resHead.rows = Integer.parseInt((String) val0.get(CommonParams.ROWS)); resHead.offset = response.offset(); // equal to 'start' resHead.numFound = response.matches(); //resHead.df = (String) val0.get("df"); //resHead.q = (String) val0.get("q"); //resHead.wt = (String) val0.get("wt"); //resHead.status = (Integer) responseHeader.get("status"); //resHead.QTime = (Integer) responseHeader.get("QTime"); //resHead.maxScore = response.maxScore(); // write header writer.write(XML_START);/*from w ww.j a v a2 s .c o m*/ String query = request.getParams().get("originalQuery"); String site = getContextString(context, "site", ""); String sort = getContextString(context, "sort", ""); String client = getContextString(context, "client", ""); String ip = getContextString(context, "ip", ""); String access = getContextString(context, "access", ""); String entqr = getContextString(context, "entqr", ""); OpensearchResponseWriter.solitaireTag(writer, "TM", Long.toString(System.currentTimeMillis() - start)); OpensearchResponseWriter.solitaireTag(writer, "Q", query); paramTag(writer, "sort", sort); paramTag(writer, "output", "xml_no_dtd"); paramTag(writer, "ie", "UTF-8"); paramTag(writer, "oe", "UTF-8"); paramTag(writer, "client", client); paramTag(writer, "q", query); paramTag(writer, "site", site); paramTag(writer, "start", Integer.toString(resHead.offset)); paramTag(writer, "num", Integer.toString(resHead.rows)); paramTag(writer, "ip", ip); paramTag(writer, "access", access); // p - search only public content, s - search only secure content, a - search all content, both public and secure paramTag(writer, "entqr", entqr); // query expansion policy; (entqr=1) -- Uses only the search appliance's synonym file, (entqr=1) -- Uses only the search appliance's synonym file, (entqr=3) -- Uses both standard and local synonym files. // body introduction final int responseCount = response.size(); writer.write("<RES SN=\"" + (resHead.offset + 1) + "\" EN=\"" + (resHead.offset + responseCount) + "\">"); writer.write(lb); // The index (1-based) of the first and last search result returned in this result set. writer.write("<M>" + resHead.numFound + "</M>"); writer.write(lb); // The estimated total number of results for the search. writer.write("<FI/>"); writer.write(lb); // Indicates that document filtering was performed during this search. int nextStart = resHead.offset + responseCount; int nextNum = Math.min(resHead.numFound - nextStart, responseCount < resHead.rows ? 0 : resHead.rows); int prevStart = resHead.offset - resHead.rows; if (prevStart >= 0 || nextNum > 0) { writer.write("<NB>"); if (prevStart >= 0) { writer.write("<PU>"); XML.escapeCharData("/gsa/search?q=" + request.getParams().get(CommonParams.Q) + "&site=" + site + "&lr=&ie=UTF-8&oe=UTF-8&output=xml_no_dtd&client=" + client + "&access=" + access + "&sort=" + sort + "&start=" + prevStart + "&sa=N", writer); // a relative URL pointing to the NEXT results page. writer.write("</PU>"); } if (nextNum > 0) { writer.write("<NU>"); XML.escapeCharData("/gsa/search?q=" + request.getParams().get(CommonParams.Q) + "&site=" + site + "&lr=&ie=UTF-8&oe=UTF-8&output=xml_no_dtd&client=" + client + "&access=" + access + "&sort=" + sort + "&start=" + nextStart + "&num=" + nextNum + "&sa=N", writer); // a relative URL pointing to the NEXT results page. writer.write("</NU>"); } writer.write("</NB>"); } writer.write(lb); // parse body SolrIndexSearcher searcher = request.getSearcher(); DocIterator iterator = response.iterator(); String urlhash = null; for (int i = 0; i < responseCount; i++) { int id = iterator.nextDoc(); Document doc = searcher.doc(id, SOLR_FIELDS); List<IndexableField> fields = doc.getFields(); // pre-scan the fields to get the mime-type String mime = ""; for (IndexableField value : fields) { String fieldName = value.name(); if (CollectionSchema.content_type.getSolrFieldName().equals(fieldName)) { mime = value.stringValue(); break; } } // write the R header for a search result writer.write("<R N=\"" + (resHead.offset + i + 1) + "\"" + (i == 1 ? " L=\"2\"" : "") + (mime != null && mime.length() > 0 ? " MIME=\"" + mime + "\"" : "") + ">"); writer.write(lb); //List<String> texts = new ArrayList<String>(); List<String> descriptions = new ArrayList<String>(); List<String> collections = new ArrayList<String>(); int size = 0; boolean title_written = false; // the solr index may contain several; we take only the first which should be the visible tag in <title></title> String title = null; for (IndexableField value : fields) { String fieldName = value.name(); // apply generic matching rule String stag = field2tag.get(fieldName); if (stag != null) { OpensearchResponseWriter.solitaireTag(writer, stag, value.stringValue()); continue; } // if the rule is not generic, use the specific here if (CollectionSchema.id.getSolrFieldName().equals(fieldName)) { urlhash = value.stringValue(); continue; } if (CollectionSchema.sku.getSolrFieldName().equals(fieldName)) { OpensearchResponseWriter.solitaireTag(writer, GSAToken.U.name(), value.stringValue()); OpensearchResponseWriter.solitaireTag(writer, GSAToken.UE.name(), value.stringValue()); continue; } if (CollectionSchema.title.getSolrFieldName().equals(fieldName) && !title_written) { title = value.stringValue(); OpensearchResponseWriter.solitaireTag(writer, GSAToken.T.name(), highlight(title, query)); //texts.add(value.stringValue()); title_written = true; continue; } if (CollectionSchema.description_txt.getSolrFieldName().equals(fieldName)) { descriptions.add(value.stringValue()); //texts.adds(description); continue; } if (CollectionSchema.last_modified.getSolrFieldName().equals(fieldName)) { Date d = new Date(Long.parseLong(value.stringValue())); writer.write("<FS NAME=\"date\" VALUE=\"" + HeaderFramework.formatGSAFS(d) + "\"/>\n"); //OpensearchResponseWriter.solitaireTag(writer, GSAToken.CACHE_LAST_MODIFIED.getSolrFieldName(), HeaderFramework.formatRFC1123(d)); //texts.add(value.stringValue()); continue; } if (CollectionSchema.load_date_dt.getSolrFieldName().equals(fieldName)) { Date d = new Date(Long.parseLong(value.stringValue())); OpensearchResponseWriter.solitaireTag(writer, GSAToken.CRAWLDATE.name(), HeaderFramework.formatRFC1123(d)); //texts.add(value.stringValue()); continue; } if (CollectionSchema.size_i.getSolrFieldName().equals(fieldName)) { size = value.stringValue() != null && value.stringValue().length() > 0 ? Integer.parseInt(value.stringValue()) : -1; continue; } if (CollectionSchema.collection_sxt.getSolrFieldName().equals(fieldName)) { collections.add(value.stringValue()); continue; } //System.out.println("superfluous field: " + fieldName + ": " + value.stringValue()); // this can be avoided setting the enableLazyFieldLoading = false in solrconfig.xml } // compute snippet from texts LinkedHashSet<String> snippet = urlhash == null ? null : snippets.get(urlhash); OpensearchResponseWriter.removeSubsumedTitle(snippet, title); OpensearchResponseWriter.solitaireTag(writer, GSAToken.S.name(), snippet == null || snippet.size() == 0 ? (descriptions.size() > 0 ? descriptions.get(0) : "") : OpensearchResponseWriter.getLargestSnippet(snippet)); OpensearchResponseWriter.solitaireTag(writer, GSAToken.GD.name(), descriptions.size() > 0 ? descriptions.get(0) : ""); String cols = collections.toString(); if (collections.size() > 0) OpensearchResponseWriter.solitaireTag(writer, "COLS" /*SPECIAL!*/, collections.size() > 1 ? cols.substring(1, cols.length() - 1).replaceAll(" ", "") : collections.get(0)); writer.write("<HAS><L/><C SZ=\""); writer.write(Integer.toString(size / 1024)); writer.write("k\" CID=\""); writer.write(urlhash); writer.write("\" ENC=\"UTF-8\"/></HAS>\n"); if (YaCyVer == null) YaCyVer = yacyVersion.thisVersion().getName() + "/" + Switchboard.getSwitchboard().peers.mySeed().hash; OpensearchResponseWriter.solitaireTag(writer, GSAToken.ENT_SOURCE.name(), YaCyVer); OpensearchResponseWriter.closeTag(writer, "R"); } writer.write("</RES>"); writer.write(lb); writer.write(XML_STOP); }
From source file:net.yacy.cora.federate.solr.responsewriter.OpensearchResponseWriter.java
License:Open Source License
@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; SimpleOrderedMap<Object> responseHeader = (SimpleOrderedMap<Object>) rsp.getResponseHeader(); DocList response = ((ResultContext) values.get("response")).docs; @SuppressWarnings("unchecked") SimpleOrderedMap<Object> facetCounts = (SimpleOrderedMap<Object>) values.get("facet_counts"); @SuppressWarnings("unchecked") 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, LinkedHashSet<String>> snippets = highlighting(highlighting); // parse response header ResHead resHead = new ResHead(); NamedList<?> val0 = (NamedList<?>) responseHeader.get("params"); resHead.rows = Integer.parseInt((String) val0.get("rows")); resHead.offset = response.offset(); // equal to 'start' resHead.numFound = response.matches(); //resHead.df = (String) val0.get("df"); //resHead.q = (String) val0.get("q"); //resHead.wt = (String) val0.get("wt"); //resHead.status = (Integer) responseHeader.get("status"); //resHead.QTime = (Integer) responseHeader.get("QTime"); //resHead.maxScore = response.maxScore(); // write header writer.write(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<?xml-stylesheet type='text/xsl' href='/yacysearch.xsl' version='1.0'?>\n" + "<rss version=\"2.0\"\n" + " xmlns:yacy=\"http://www.yacy.net/\"\n" + " xmlns:opensearch=\"http://a9.com/-/spec/opensearch/1.1/\"\n" + " xmlns:media=\"http://search.yahoo.com/mrss/\"\n" + " xmlns:atom=\"http://www.w3.org/2005/Atom\"\n" + " xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n" + " xmlns:geo=\"http://www.w3.org/2003/01/geo/wgs84_pos#\"\n" + ">\n").toCharArray()); openTag(writer, "channel"); solitaireTag(writer, "opensearch:totalResults", Integer.toString(resHead.numFound)); solitaireTag(writer, "opensearch:startIndex", Integer.toString(resHead.offset)); solitaireTag(writer, "opensearch:itemsPerPage", Integer.toString(resHead.rows)); solitaireTag(writer, RSSMessage.Token.title.name(), this.title); writer.write(// w w w .j a va 2 s .c o m "<atom:link rel=\"search\" href=\"/opensearchdescription.xml\" type=\"application/opensearchdescription+xml\"/>"); solitaireTag(writer, "description", "Search Result"); //solitaireTag(writer, "link", ""); //solitaireTag(writer, "image", ""); // parse body final int responseCount = response.size(); SolrIndexSearcher searcher = request.getSearcher(); DocIterator iterator = response.iterator(); String urlhash = null; for (int i = 0; i < responseCount; i++) { openTag(writer, "item"); int id = iterator.nextDoc(); Document doc = searcher.doc(id, SOLR_FIELDS); List<IndexableField> fields = doc.getFields(); int fieldc = fields.size(); List<String> texts = new ArrayList<String>(); List<String> descriptions = new ArrayList<String>(); String title = ""; for (int j = 0; j < fieldc; j++) { IndexableField value = fields.get(j); String fieldName = value.name(); // apply generic matching rule String stag = field2tag.get(fieldName); if (stag != null) { solitaireTag(writer, stag, value.stringValue()); continue; } // take apart the url if (CollectionSchema.sku.getSolrFieldName().equals(fieldName)) { String u = value.stringValue(); solitaireTag(writer, RSSMessage.Token.link.name(), u); try { MultiProtocolURL url = new MultiProtocolURL(u); solitaireTag(writer, YaCyMetadata.host.getURIref(), url.getHost()); solitaireTag(writer, YaCyMetadata.path.getURIref(), url.getPath()); solitaireTag(writer, YaCyMetadata.file.getURIref(), url.getFileName()); } catch (final MalformedURLException e) { } continue; } // if the rule is not generic, use the specific here if (CollectionSchema.id.getSolrFieldName().equals(fieldName)) { urlhash = value.stringValue(); solitaireTag(writer, RSSMessage.Token.guid.name(), urlhash, "isPermaLink=\"false\""); continue; } if (CollectionSchema.title.getSolrFieldName().equals(fieldName)) { title = value.stringValue(); texts.add(title); continue; } if (CollectionSchema.last_modified.getSolrFieldName().equals(fieldName)) { Date d = new Date(Long.parseLong(value.stringValue())); solitaireTag(writer, RSSMessage.Token.pubDate.name(), HeaderFramework.formatRFC1123(d)); continue; } if (CollectionSchema.description_txt.getSolrFieldName().equals(fieldName)) { String description = value.stringValue(); descriptions.add(description); solitaireTag(writer, DublinCore.Description.getURIref(), description); texts.add(description); continue; } if (CollectionSchema.text_t.getSolrFieldName().equals(fieldName)) { texts.add(value.stringValue()); continue; } if (CollectionSchema.size_i.getSolrFieldName().equals(fieldName)) { int size = value.numericValue().intValue(); solitaireTag(writer, YaCyMetadata.size.getURIref(), Integer.toString(size)); solitaireTag(writer, YaCyMetadata.sizename.getURIref(), RSSMessage.sizename(size)); continue; } if (CollectionSchema.h1_txt.getSolrFieldName().equals(fieldName) || CollectionSchema.h2_txt.getSolrFieldName().equals(fieldName) || CollectionSchema.h3_txt.getSolrFieldName().equals(fieldName) || CollectionSchema.h4_txt.getSolrFieldName().equals(fieldName) || CollectionSchema.h5_txt.getSolrFieldName().equals(fieldName) || CollectionSchema.h6_txt.getSolrFieldName().equals(fieldName)) { // because these are multi-valued fields, there can be several of each texts.add(value.stringValue()); continue; } } // compute snippet from texts solitaireTag(writer, RSSMessage.Token.title.name(), title.length() == 0 ? (texts.size() == 0 ? "" : texts.get(0)) : title); LinkedHashSet<String> snippet = urlhash == null ? null : snippets.get(urlhash); String tagname = RSSMessage.Token.description.name(); if (snippet == null || snippet.size() == 0) { writer.write("<"); writer.write(tagname); writer.write('>'); for (String d : descriptions) { XML.escapeCharData(d, writer); } writer.write("</"); writer.write(tagname); writer.write(">\n"); } else { removeSubsumedTitle(snippet, title); solitaireTag(writer, tagname, getLargestSnippet(snippet)); // snippet may be size=0 } solitaireTag(writer, DublinCore.Subject.getURIref(), doc.get(CollectionSchema.keywords.getSolrFieldName())); closeTag(writer, "item"); } openTag(writer, "yacy:navigation"); // the facets can be created with the options &facet=true&facet.mincount=1&facet.field=host_s&facet.field=url_file_ext_s&facet.field=url_protocol_s&facet.field=author_sxt @SuppressWarnings("unchecked") NamedList<Integer> domains = facetFields == null ? null : (NamedList<Integer>) facetFields.get(CollectionSchema.host_s.getSolrFieldName()); @SuppressWarnings("unchecked") NamedList<Integer> filetypes = facetFields == null ? null : (NamedList<Integer>) facetFields.get(CollectionSchema.url_file_ext_s.getSolrFieldName()); @SuppressWarnings("unchecked") NamedList<Integer> protocols = facetFields == null ? null : (NamedList<Integer>) facetFields.get(CollectionSchema.url_protocol_s.getSolrFieldName()); @SuppressWarnings("unchecked") NamedList<Integer> authors = facetFields == null ? null : (NamedList<Integer>) facetFields.get(CollectionSchema.author_sxt.getSolrFieldName()); @SuppressWarnings("unchecked") NamedList<Integer> collections = facetFields == null ? null : (NamedList<Integer>) facetFields.get(CollectionSchema.collection_sxt.getSolrFieldName()); if (domains != null) { openTag(writer, "yacy:facet name=\"domains\" displayname=\"Domains\" type=\"String\" min=\"0\" max=\"0\" mean=\"0\""); for (Map.Entry<String, Integer> entry : domains) facetEntry(writer, "site", entry.getKey(), Integer.toString(entry.getValue())); closeTag(writer, "yacy:facet"); } if (filetypes != null) { openTag(writer, "yacy:facet name=\"filetypes\" displayname=\"Filetypes\" type=\"String\" min=\"0\" max=\"0\" mean=\"0\""); for (Map.Entry<String, Integer> entry : filetypes) facetEntry(writer, "filetype", entry.getKey(), Integer.toString(entry.getValue())); closeTag(writer, "yacy:facet"); } if (protocols != null) { openTag(writer, "yacy:facet name=\"protocols\" displayname=\"Protocols\" type=\"String\" min=\"0\" max=\"0\" mean=\"0\""); for (Map.Entry<String, Integer> entry : protocols) facetEntry(writer, "protocol", entry.getKey(), Integer.toString(entry.getValue())); closeTag(writer, "yacy:facet"); } if (authors != null) { openTag(writer, "yacy:facet name=\"authors\" displayname=\"Authors\" type=\"String\" min=\"0\" max=\"0\" mean=\"0\""); for (Map.Entry<String, Integer> entry : authors) facetEntry(writer, "author", entry.getKey(), Integer.toString(entry.getValue())); closeTag(writer, "yacy:facet"); } if (collections != null) { openTag(writer, "yacy:facet name=\"collections\" displayname=\"Collections\" type=\"String\" min=\"0\" max=\"0\" mean=\"0\""); for (Map.Entry<String, Integer> entry : collections) facetEntry(writer, "collection", entry.getKey(), Integer.toString(entry.getValue())); closeTag(writer, "yacy:facet"); } closeTag(writer, "yacy:navigation"); closeTag(writer, "channel"); writer.write("</rss>\n".toCharArray()); }
From source file:net.yacy.cora.federate.solr.responsewriter.YJsonResponseWriter.java
License:Open Source License
@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; SimpleOrderedMap<Object> responseHeader = (SimpleOrderedMap<Object>) rsp.getResponseHeader(); DocList response = ((ResultContext) values.get("response")).docs; @SuppressWarnings("unchecked") SimpleOrderedMap<Object> facetCounts = (SimpleOrderedMap<Object>) values.get("facet_counts"); @SuppressWarnings("unchecked") 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, LinkedHashSet<String>> snippets = OpensearchResponseWriter.highlighting(highlighting); // parse response header ResHead resHead = new ResHead(); NamedList<?> val0 = (NamedList<?>) responseHeader.get("params"); resHead.rows = Integer.parseInt((String) val0.get("rows")); resHead.offset = response.offset(); // equal to 'start' resHead.numFound = response.matches(); String jsonp = request.getParams().get("callback"); // check for JSONP if (jsonp != null) { writer.write(jsonp.toCharArray()); writer.write("([".toCharArray()); }/*from w w w . j av a 2s .co m*/ // write header writer.write(("{\"channels\": [{\n").toCharArray()); solitaireTag(writer, "totalResults", Integer.toString(resHead.numFound)); solitaireTag(writer, "startIndex", Integer.toString(resHead.offset)); solitaireTag(writer, "itemsPerPage", Integer.toString(resHead.rows)); solitaireTag(writer, "title", this.title); solitaireTag(writer, "description", "Search Result"); writer.write("\"items\": [\n".toCharArray()); // parse body final int responseCount = response.size(); SolrIndexSearcher searcher = request.getSearcher(); DocIterator iterator = response.iterator(); for (int i = 0; i < responseCount; i++) { try { writer.write("{\n".toCharArray()); int id = iterator.nextDoc(); Document doc = searcher.doc(id, OpensearchResponseWriter.SOLR_FIELDS); List<IndexableField> fields = doc.getFields(); int fieldc = fields.size(); List<String> texts = new ArrayList<String>(); MultiProtocolURL url = null; String urlhash = null; List<String> descriptions = new ArrayList<String>(); String title = ""; StringBuilder path = new StringBuilder(80); for (int j = 0; j < fieldc; j++) { IndexableField value = fields.get(j); String fieldName = value.name(); // apply generic matching rule String stag = field2tag.get(fieldName); if (stag != null) { solitaireTag(writer, stag, value.stringValue()); continue; } // some special handling here if (CollectionSchema.sku.getSolrFieldName().equals(fieldName)) { String u = value.stringValue(); try { url = new MultiProtocolURL(u); String filename = url.getFileName(); solitaireTag(writer, "link", u); solitaireTag(writer, "file", filename); // get image license if (MultiProtocolURL.isImage(MultiProtocolURL.getFileExtension(filename))) URLLicense.aquireLicense(urlhash, url.toNormalform(true)); } catch (final MalformedURLException e) { } continue; } if (CollectionSchema.title.getSolrFieldName().equals(fieldName)) { title = value.stringValue(); texts.add(title); continue; } if (CollectionSchema.description_txt.getSolrFieldName().equals(fieldName)) { String description = value.stringValue(); descriptions.add(description); texts.add(description); continue; } if (CollectionSchema.id.getSolrFieldName().equals(fieldName)) { urlhash = value.stringValue(); solitaireTag(writer, "guid", urlhash); continue; } if (CollectionSchema.url_paths_sxt.getSolrFieldName().equals(fieldName)) { path.append('/').append(value.stringValue()); continue; } if (CollectionSchema.last_modified.getSolrFieldName().equals(fieldName)) { Date d = new Date(Long.parseLong(value.stringValue())); solitaireTag(writer, "pubDate", HeaderFramework.formatRFC1123(d)); continue; } if (CollectionSchema.size_i.getSolrFieldName().equals(fieldName)) { int size = value.stringValue() != null && value.stringValue().length() > 0 ? Integer.parseInt(value.stringValue()) : -1; int sizekb = size / 1024; int sizemb = sizekb / 1024; solitaireTag(writer, "size", value.stringValue()); solitaireTag(writer, "sizename", sizemb > 0 ? (Integer.toString(sizemb) + " mbyte") : sizekb > 0 ? (Integer.toString(sizekb) + " kbyte") : (Integer.toString(size) + " byte")); continue; } if (CollectionSchema.text_t.getSolrFieldName().equals(fieldName)) { texts.add(value.stringValue()); continue; } if (CollectionSchema.h1_txt.getSolrFieldName().equals(fieldName) || CollectionSchema.h2_txt.getSolrFieldName().equals(fieldName) || CollectionSchema.h3_txt.getSolrFieldName().equals(fieldName) || CollectionSchema.h4_txt.getSolrFieldName().equals(fieldName) || CollectionSchema.h5_txt.getSolrFieldName().equals(fieldName) || CollectionSchema.h6_txt.getSolrFieldName().equals(fieldName)) { // because these are multi-valued fields, there can be several of each texts.add(value.stringValue()); continue; } //missing: "code","faviconCode" } // compute snippet from texts solitaireTag(writer, "path", path.toString()); solitaireTag(writer, "title", title.length() == 0 ? (texts.size() == 0 ? path.toString() : texts.get(0)) : title); LinkedHashSet<String> snippet = urlhash == null ? null : snippets.get(urlhash); OpensearchResponseWriter.removeSubsumedTitle(snippet, title); writer.write("\"description\":\""); writer.write(serverObjects.toJSON(snippet == null || snippet.size() == 0 ? (descriptions.size() > 0 ? descriptions.get(0) : "") : OpensearchResponseWriter.getLargestSnippet(snippet))); writer.write("\"\n}\n"); if (i < responseCount - 1) { writer.write(",\n".toCharArray()); } } catch (final Throwable ee) { } } writer.write("],\n".toCharArray()); writer.write("\"navigation\":[\n"); // the facets can be created with the options &facet=true&facet.mincount=1&facet.field=host_s&facet.field=url_file_ext_s&facet.field=url_protocol_s&facet.field=author_sxt @SuppressWarnings("unchecked") NamedList<Integer> domains = facetFields == null ? null : (NamedList<Integer>) facetFields.get(CollectionSchema.host_s.getSolrFieldName()); @SuppressWarnings("unchecked") NamedList<Integer> filetypes = facetFields == null ? null : (NamedList<Integer>) facetFields.get(CollectionSchema.url_file_ext_s.getSolrFieldName()); @SuppressWarnings("unchecked") NamedList<Integer> protocols = facetFields == null ? null : (NamedList<Integer>) facetFields.get(CollectionSchema.url_protocol_s.getSolrFieldName()); @SuppressWarnings("unchecked") NamedList<Integer> authors = facetFields == null ? null : (NamedList<Integer>) facetFields.get(CollectionSchema.author_sxt.getSolrFieldName()); @SuppressWarnings("unchecked") NamedList<Integer> collections = facetFields == null ? null : (NamedList<Integer>) facetFields.get(CollectionSchema.collection_sxt.getSolrFieldName()); if (domains != null) { writer.write( "{\"facetname\":\"domains\",\"displayname\":\"Provider\",\"type\":\"String\",\"min\":\"0\",\"max\":\"0\",\"mean\":\"0\",\"elements\":[\n" .toCharArray()); for (int i = 0; i < domains.size(); i++) { facetEntry(writer, "site", domains.getName(i), Integer.toString(domains.getVal(i))); if (i < domains.size() - 1) writer.write(','); writer.write("\n"); } writer.write("]},\n".toCharArray()); } if (filetypes != null) { writer.write( "{\"facetname\":\"filetypes\",\"displayname\":\"Filetypes\",\"type\":\"String\",\"min\":\"0\",\"max\":\"0\",\"mean\":\"0\",\"elements\":[\n" .toCharArray()); List<Map.Entry<String, Integer>> l = new ArrayList<Map.Entry<String, Integer>>(); for (Map.Entry<String, Integer> e : filetypes) { if (e.getKey().length() <= 6) l.add(e); if (l.size() >= 16) break; } for (int i = 0; i < l.size(); i++) { Map.Entry<String, Integer> e = l.get(i); facetEntry(writer, "filetype", e.getKey(), Integer.toString(e.getValue())); if (i < l.size() - 1) writer.write(','); writer.write("\n"); } writer.write("]},\n".toCharArray()); } if (protocols != null) { writer.write( "{\"facetname\":\"protocols\",\"displayname\":\"Protocol\",\"type\":\"String\",\"min\":\"0\",\"max\":\"0\",\"mean\":\"0\",\"elements\":[\n" .toCharArray()); for (int i = 0; i < protocols.size(); i++) { facetEntry(writer, "protocol", protocols.getName(i), Integer.toString(protocols.getVal(i))); if (i < protocols.size() - 1) writer.write(','); writer.write("\n"); } writer.write("]},\n".toCharArray()); } if (authors != null) { writer.write( "{\"facetname\":\"authors\",\"displayname\":\"Authors\",\"type\":\"String\",\"min\":\"0\",\"max\":\"0\",\"mean\":\"0\",\"elements\":[\n" .toCharArray()); for (int i = 0; i < authors.size(); i++) { facetEntry(writer, "author", authors.getName(i), Integer.toString(authors.getVal(i))); if (i < authors.size() - 1) writer.write(','); writer.write("\n"); } writer.write("]},\n".toCharArray()); } if (collections != null) { writer.write( "{\"facetname\":\"collections\",\"displayname\":\"Collections\",\"type\":\"String\",\"min\":\"0\",\"max\":\"0\",\"mean\":\"0\",\"elements\":[\n" .toCharArray()); for (int i = 0; i < collections.size(); i++) { facetEntry(writer, "collection", collections.getName(i), Integer.toString(collections.getVal(i))); if (i < collections.size() - 1) writer.write(','); writer.write("\n"); } writer.write("]},\n".toCharArray()); } writer.write("]}]}\n".toCharArray()); if (jsonp != null) { writer.write("])".toCharArray()); } }