Example usage for org.apache.solr.search DocList matches

List of usage examples for org.apache.solr.search DocList matches

Introduction

In this page you can find the example usage for org.apache.solr.search DocList matches.

Prototype

public long matches();

Source Link

Document

Returns the total number of matches for the search (as opposed to just the number collected according to offset() and size()).

Usage

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 ava2 s . c  om
    }

    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 w w.j a va 2  s.  co  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  .  j a  va 2 s . com*/
 * @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()));
    }//from  ww w  . j  a  v  a  2 s  . c o m
    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  w w . ja 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 ww  .  j av  a 2 s  . co  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());
    }// w w w.  ja v a  2 s.  com

    // 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());
    }
}

From source file:net.yacy.http.servlets.SolrSelectServlet.java

License:Open Source License

@Override
public void service(ServletRequest request, ServletResponse response) throws IOException, ServletException {

    HttpServletRequest hrequest = (HttpServletRequest) request;
    HttpServletResponse hresponse = (HttpServletResponse) response;
    SolrQueryRequest req = null;//from   ww  w .  j ava  2  s.  com

    final Method reqMethod = Method.getMethod(hrequest.getMethod());

    Writer out = null;
    try {
        // prepare request to solr
        MultiMapSolrParams mmsp = SolrRequestParsers.parseQueryString(hrequest.getQueryString());

        Switchboard sb = Switchboard.getSwitchboard();
        boolean authenticated = true;

        // count remote searches if this was part of a p2p search
        if (mmsp.getMap().containsKey("partitions")) {
            final int partitions = mmsp.getInt("partitions", 30);
            sb.searchQueriesGlobal += 1.0f / partitions; // increase query counter
        }

        // get the ranking profile id
        int profileNr = mmsp.getInt("profileNr", 0);

        // rename post fields according to result style
        String querystring = "";
        if (!mmsp.getMap().containsKey(CommonParams.Q) && mmsp.getMap().containsKey(CommonParams.QUERY)) {
            querystring = mmsp.get(CommonParams.QUERY, "");
            mmsp.getMap().remove(CommonParams.QUERY);
            QueryModifier modifier = new QueryModifier(0);
            querystring = modifier.parse(querystring);
            modifier.apply(mmsp);
            QueryGoal qg = new QueryGoal(querystring);
            StringBuilder solrQ = qg.collectionTextQuery();
            mmsp.getMap().put(CommonParams.Q, new String[] { solrQ.toString() }); // sru patch
        }
        String q = mmsp.get(CommonParams.Q, "");
        if (querystring.length() == 0)
            querystring = q;
        if (!mmsp.getMap().containsKey(CommonParams.START)) {
            int startRecord = mmsp.getFieldInt("startRecord", null, 0);
            mmsp.getMap().remove("startRecord");
            mmsp.getMap().put(CommonParams.START, new String[] { Integer.toString(startRecord) }); // sru patch
        }
        if (!mmsp.getMap().containsKey(CommonParams.ROWS)) {
            int maximumRecords = mmsp.getFieldInt("maximumRecords", null, 10);
            mmsp.getMap().remove("maximumRecords");
            mmsp.getMap().put(CommonParams.ROWS, new String[] { Integer.toString(maximumRecords) }); // sru patch
        }
        mmsp.getMap().put(CommonParams.ROWS, new String[] { Integer
                .toString(Math.min(mmsp.getInt(CommonParams.ROWS, 10), (authenticated) ? 100000000 : 100)) });

        // set ranking according to profile number if ranking attributes are not given in the request
        Ranking ranking = sb.index.fulltext().getDefaultConfiguration().getRanking(profileNr);
        if (!mmsp.getMap().containsKey(CommonParams.SORT) && !mmsp.getMap().containsKey(DisMaxParams.BQ)
                && !mmsp.getMap().containsKey(DisMaxParams.BF) && !mmsp.getMap().containsKey("boost")) {
            if (!mmsp.getMap().containsKey("defType"))
                mmsp.getMap().put("defType", new String[] { "edismax" });
            String fq = ranking.getFilterQuery();
            String bq = ranking.getBoostQuery();
            String bf = ranking.getBoostFunction();
            if (fq.length() > 0)
                mmsp.getMap().put(CommonParams.FQ, new String[] { fq });
            if (bq.length() > 0)
                mmsp.getMap().put(DisMaxParams.BQ, new String[] { bq });
            if (bf.length() > 0)
                mmsp.getMap().put("boost", new String[] { bf }); // a boost function extension, see http://wiki.apache.org/solr/ExtendedDisMax#bf_.28Boost_Function.2C_additive.29
        }

        // get a response writer for the result
        String wt = mmsp.get(CommonParams.WT, "xml"); // maybe use /solr/select?q=*:*&start=0&rows=10&wt=exml
        QueryResponseWriter responseWriter = RESPONSE_WRITER.get(wt);
        if (responseWriter == null)
            throw new ServletException("no response writer");
        if (responseWriter instanceof OpensearchResponseWriter) {
            // set the title every time, it is possible that it has changed
            final String promoteSearchPageGreeting = (sb
                    .getConfigBool(SwitchboardConstants.GREETING_NETWORK_NAME, false))
                            ? sb.getConfig("network.unit.description", "")
                            : sb.getConfig(SwitchboardConstants.GREETING, "");
            ((OpensearchResponseWriter) responseWriter).setTitle(promoteSearchPageGreeting);
        }

        // if this is a call to YaCys special search formats, enhance the query with field assignments
        if ((responseWriter instanceof YJsonResponseWriter
                || responseWriter instanceof OpensearchResponseWriter)
                && "true".equals(mmsp.get("hl", "true"))) {
            // add options for snippet generation
            if (!mmsp.getMap().containsKey("hl.q"))
                mmsp.getMap().put("hl.q", new String[] { q });
            if (!mmsp.getMap().containsKey("hl.fl"))
                mmsp.getMap().put("hl.fl",
                        new String[] { CollectionSchema.description_txt + ","
                                + CollectionSchema.h4_txt.getSolrFieldName() + ","
                                + CollectionSchema.h3_txt.getSolrFieldName() + ","
                                + CollectionSchema.h2_txt.getSolrFieldName() + ","
                                + CollectionSchema.h1_txt.getSolrFieldName() + ","
                                + CollectionSchema.text_t.getSolrFieldName() });
            if (!mmsp.getMap().containsKey("hl.alternateField"))
                mmsp.getMap().put("hl.alternateField",
                        new String[] { CollectionSchema.description_txt.getSolrFieldName() });
            if (!mmsp.getMap().containsKey("hl.simple.pre"))
                mmsp.getMap().put("hl.simple.pre", new String[] { "<b>" });
            if (!mmsp.getMap().containsKey("hl.simple.post"))
                mmsp.getMap().put("hl.simple.post", new String[] { "</b>" });
            if (!mmsp.getMap().containsKey("hl.fragsize"))
                mmsp.getMap().put("hl.fragsize",
                        new String[] { Integer.toString(SearchEvent.SNIPPET_MAX_LENGTH) });
        }

        // get the embedded connector
        String requestURI = hrequest.getRequestURI();
        boolean defaultConnector = (requestURI.startsWith("/solr/" + WebgraphSchema.CORE_NAME)) ? false
                : requestURI.startsWith("/solr/" + CollectionSchema.CORE_NAME)
                        || mmsp.get("core", CollectionSchema.CORE_NAME).equals(CollectionSchema.CORE_NAME);
        mmsp.getMap().remove("core");
        SolrConnector connector = defaultConnector ? sb.index.fulltext().getDefaultEmbeddedConnector()
                : sb.index.fulltext().getEmbeddedConnector(WebgraphSchema.CORE_NAME);
        if (connector == null) {
            connector = defaultConnector ? sb.index.fulltext().getDefaultConnector()
                    : sb.index.fulltext().getConnectorForRead(WebgraphSchema.CORE_NAME);
        }
        if (connector == null)
            throw new ServletException("no core");

        // add default queryfield parameter according to local ranking config (or defaultfield)
        if (ranking != null) { // ranking normally never null
            final String qf = ranking.getQueryFields();
            if (qf.length() > 4) { // make sure qf has content (else use df)
                addParam(DisMaxParams.QF, qf, mmsp.getMap()); // add QF that we set to be best suited for our index
                // TODO: if every peer applies a decent QF itself, this can be reverted to getMap().put()
            } else {
                mmsp.getMap().put(CommonParams.DF, new String[] { CollectionSchema.text_t.getSolrFieldName() });
            }
        } else {
            mmsp.getMap().put(CommonParams.DF, new String[] { CollectionSchema.text_t.getSolrFieldName() });
        }

        // do the solr request, generate facets if we use a special YaCy format
        final SolrQueryResponse rsp;
        if (connector instanceof EmbeddedSolrConnector) {
            req = ((EmbeddedSolrConnector) connector).request(mmsp);
            rsp = ((EmbeddedSolrConnector) connector).query(req);

            // prepare response
            hresponse.setHeader("Cache-Control", "no-cache, no-store");
            HttpCacheHeaderUtil.checkHttpCachingVeto(rsp, hresponse, reqMethod);

            // check error
            if (rsp.getException() != null) {
                AccessTracker.addToDump(querystring, "0", new Date());
                sendError(hresponse, rsp.getException());
                return;
            }

            NamedList<?> values = rsp.getValues();
            DocList r = ((ResultContext) values.get("response")).docs;
            int numFound = r.matches();
            AccessTracker.addToDump(querystring, Integer.toString(numFound), new Date());

            // write response header
            final String contentType = responseWriter.getContentType(req, rsp);
            if (null != contentType)
                response.setContentType(contentType);

            if (Method.HEAD == reqMethod) {
                return;
            }

            // write response body
            if (responseWriter instanceof BinaryResponseWriter) {
                ((BinaryResponseWriter) responseWriter).write(response.getOutputStream(), req, rsp);
            } else {
                out = new FastWriter(new OutputStreamWriter(response.getOutputStream(), UTF8.charset));
                responseWriter.write(out, req, rsp);
                out.flush();
            }
        } else {
            // write a 'faked' response using a call to the backend
            SolrDocumentList sdl = connector.getDocumentListByQuery(mmsp.getMap().get(CommonParams.Q)[0],
                    mmsp.getMap().get(CommonParams.SORT) == null ? null
                            : mmsp.getMap().get(CommonParams.SORT)[0],
                    Integer.parseInt(mmsp.getMap().get(CommonParams.START)[0]),
                    Integer.parseInt(mmsp.getMap().get(CommonParams.ROWS)[0]),
                    mmsp.getMap().get(CommonParams.FL));
            OutputStreamWriter osw = new OutputStreamWriter(response.getOutputStream());
            EnhancedXMLResponseWriter.write(osw, req, sdl);
            osw.close();
        }
    } catch (final Throwable ex) {
        sendError(hresponse, ex);
    } finally {
        if (req != null) {
            req.close();
        }
        SolrRequestInfo.clearRequestInfo();
        if (out != null)
            try {
                out.close();
            } catch (final IOException e1) {
            }
    }
}

From source file:org.dice.solrenhancements.morelikethis.DiceMoreLikeThisHandler.java

License:Apache License

private MLTResult getMoreLikeTheseFromQuery(SolrQueryResponse rsp, SolrParams params, int flags, String q,
        Query query, SortSpec sortSpec, List<Query> targetFqFilters, List<Query> mltFqFilters,
        SolrIndexSearcher searcher, MoreLikeThisHelper mlt, int start, int rows)
        throws IOException, SyntaxError {

    boolean includeMatch = params.getBool(MoreLikeThisParams.MATCH_INCLUDE, true);
    int matchOffset = params.getInt(MoreLikeThisParams.MATCH_OFFSET, 0);
    // Find the base match
    DocList match = searcher.getDocList(query, targetFqFilters, null, matchOffset, 10000, flags); // only get the first one...
    if (match.matches() == 0) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                String.format("MoreLikeThis was unable to find any documents matching the query: '%s'.", q));
    }//from ww w  .  j a  va2 s . co m

    if (includeMatch) {
        rsp.add("match", match);
    }

    // This is an iterator, but we only handle the first match
    DocIterator iterator = match.iterator();
    if (iterator.hasNext()) {
        // do a MoreLikeThis query for each document in results
        return mlt.getMoreLikeTheseFromDocs(iterator, start, rows, mltFqFilters, flags, sortSpec.getSort());
    }
    return null;
}

From source file:org.dice.solrenhancements.unsupervisedfeedback.DiceUnsupervisedFeedbackHandler.java

License:Apache License

private DocListAndSet expandQueryAndReExecute(SolrQueryResponse rsp, SolrParams params, int maxDocumentsToMatch,
        int flags, String q, Query seedQuery, SortSpec sortSpec, List<Query> targetFqFilters,
        List<Query> mltFqFilters, SolrIndexSearcher searcher, UnsupervisedFeedbackHelper uff,
        List<InterestingTerm> interesting, DocListAndSet mltDocs, int start, int rows)
        throws IOException, SyntaxError {

    boolean includeMatch = params.getBool(UnsupervisedFeedbackParams.MATCH_INCLUDE, true);
    int matchOffset = params.getInt(UnsupervisedFeedbackParams.MATCH_OFFSET, 0);
    // Find the base match
    DocList match = searcher.getDocList(seedQuery, targetFqFilters, null, matchOffset, maxDocumentsToMatch,
            flags); // only get the first one...
    if (match.matches() == 0) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, String.format(
                "Unsupervised feedback handler was unable to find any documents matching the seed query: '%s'.",
                q));//from   w  w w . j a  v  a 2s  .  c  o m
    }

    if (includeMatch) {
        rsp.add("match", match);
    }

    // This is an iterator, but we only handle the first match
    DocIterator iterator = match.iterator();
    if (iterator.hasNext()) {
        // do a MoreLikeThis query for each document in results
        mltDocs = uff.expandQueryAndReExecute(iterator, seedQuery, start, rows, mltFqFilters, interesting,
                flags, sortSpec.getSort());
    }
    return mltDocs;
}