Search in sources :

Example 16 with SolrDocument

use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.

the class QueryComponent method returnFields.

protected void returnFields(ResponseBuilder rb, ShardRequest sreq) {
    if ((sreq.purpose & ShardRequest.PURPOSE_GET_FIELDS) != 0) {
        boolean returnScores = (rb.getFieldFlags() & SolrIndexSearcher.GET_SCORES) != 0;
        String keyFieldName = rb.req.getSchema().getUniqueKeyField().getName();
        boolean removeKeyField = !rb.rsp.getReturnFields().wantsField(keyFieldName);
        if (rb.rsp.getReturnFields().getFieldRenames().get(keyFieldName) != null) {
            // if id was renamed we need to use the new name
            keyFieldName = rb.rsp.getReturnFields().getFieldRenames().get(keyFieldName);
        }
        for (ShardResponse srsp : sreq.responses) {
            if (srsp.getException() != null) {
                // Don't try to get the documents if there was an exception in the shard
                if (rb.req.getParams().getBool(ShardParams.SHARDS_INFO, false)) {
                    @SuppressWarnings("unchecked") NamedList<Object> shardInfo = (NamedList<Object>) rb.rsp.getValues().get(ShardParams.SHARDS_INFO);
                    @SuppressWarnings("unchecked") SimpleOrderedMap<Object> nl = (SimpleOrderedMap<Object>) shardInfo.get(srsp.getShard());
                    if (nl.get("error") == null) {
                        // Add the error to the shards info section if it wasn't added before
                        Throwable t = srsp.getException();
                        if (t instanceof SolrServerException) {
                            t = ((SolrServerException) t).getCause();
                        }
                        nl.add("error", t.toString());
                        StringWriter trace = new StringWriter();
                        t.printStackTrace(new PrintWriter(trace));
                        nl.add("trace", trace.toString());
                    }
                }
                continue;
            }
            SolrDocumentList docs = (SolrDocumentList) srsp.getSolrResponse().getResponse().get("response");
            for (SolrDocument doc : docs) {
                Object id = doc.getFieldValue(keyFieldName);
                ShardDoc sdoc = rb.resultIds.get(id.toString());
                if (sdoc != null) {
                    if (returnScores) {
                        doc.setField("score", sdoc.score);
                    } else {
                        // Score might have been added (in createMainQuery) to shard-requests (and therefore in shard-response-docs)
                        // Remove score if the outer request did not ask for it returned
                        doc.remove("score");
                    }
                    if (removeKeyField) {
                        doc.removeFields(keyFieldName);
                    }
                    rb.getResponseDocs().set(sdoc.positionInResponse, doc);
                }
            }
        }
    }
}
Also used : NamedList(org.apache.solr.common.util.NamedList) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) SolrDocument(org.apache.solr.common.SolrDocument) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter)

Example 17 with SolrDocument

use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.

the class MoreLikeThisComponent method mergeSolrDocumentList.

public SolrDocumentList mergeSolrDocumentList(SolrDocumentList one, SolrDocumentList two, int maxSize, String idField) {
    List<SolrDocument> l = new ArrayList<>();
    // De-dup records sets. Shouldn't happen if indexed correctly.
    Map<String, SolrDocument> map = new HashMap<>();
    for (SolrDocument doc : one) {
        Object id = doc.getFieldValue(idField);
        assert id != null : doc.toString();
        map.put(id.toString(), doc);
    }
    for (SolrDocument doc : two) {
        map.put(doc.getFieldValue(idField).toString(), doc);
    }
    l = new ArrayList<>(map.values());
    // Comparator to sort docs based on score. null scores/docs are set to 0.
    // hmm...we are ordering by scores that are not really comparable...
    Comparator<SolrDocument> c = new Comparator<SolrDocument>() {

        public int compare(SolrDocument o1, SolrDocument o2) {
            Float f1 = getFloat(o1);
            Float f2 = getFloat(o2);
            return f2.compareTo(f1);
        }

        private Float getFloat(SolrDocument doc) {
            Float f = 0f;
            if (doc != null) {
                Object o = doc.getFieldValue("score");
                if (o != null && o instanceof Float) {
                    f = (Float) o;
                }
            }
            return f;
        }
    };
    Collections.sort(l, c);
    // Truncate list to maxSize
    if (l.size() > maxSize) {
        l = l.subList(0, maxSize);
    }
    // Create SolrDocumentList Attributes from originals
    SolrDocumentList result = new SolrDocumentList();
    result.addAll(l);
    result.setMaxScore(Math.max(one.getMaxScore(), two.getMaxScore()));
    result.setNumFound(one.getNumFound() + two.getNumFound());
    result.setStart(Math.min(one.getStart(), two.getStart()));
    return result;
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Comparator(java.util.Comparator)

Example 18 with SolrDocument

use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.

the class RealTimeGetComponent method reopenRealtimeSearcherAndGet.

/**
   * Re-open the RT searcher and get the document, referred to by the idTerm, from that searcher. 
   * @return Returns the document or null if not found.
   */
private static SolrDocument reopenRealtimeSearcherAndGet(SolrCore core, Term idTerm, ReturnFields returnFields) throws IOException {
    UpdateLog ulog = core.getUpdateHandler().getUpdateLog();
    ulog.openRealtimeSearcher();
    RefCounted<SolrIndexSearcher> searcherHolder = core.getRealtimeSearcher();
    try {
        SolrIndexSearcher searcher = searcherHolder.get();
        int docid = searcher.getFirstMatch(idTerm);
        if (docid < 0) {
            return null;
        }
        Document luceneDocument = searcher.doc(docid, returnFields.getLuceneFieldNames());
        SolrDocument doc = toSolrDoc(luceneDocument, core.getLatestSchema());
        SolrDocumentFetcher docFetcher = searcher.getDocFetcher();
        docFetcher.decorateDocValueFields(doc, docid, docFetcher.getNonStoredDVs(false));
        return doc;
    } finally {
        searcherHolder.decref();
    }
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) UpdateLog(org.apache.solr.update.UpdateLog) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) Document(org.apache.lucene.document.Document) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) IndexFingerprint(org.apache.solr.update.IndexFingerprint) SolrDocumentFetcher(org.apache.solr.search.SolrDocumentFetcher)

Example 19 with SolrDocument

use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.

the class CloudMLTQParser method parse.

public Query parse() {
    String id = localParams.get(QueryParsing.V);
    // Do a Real Time Get for the document
    SolrDocument doc = getDocument(id);
    if (doc == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error completing MLT request. Could not fetch " + "document with id [" + id + "]");
    }
    String[] qf = localParams.getParams("qf");
    Map<String, Float> boostFields = new HashMap<>();
    MoreLikeThis mlt = new MoreLikeThis(req.getSearcher().getIndexReader());
    mlt.setMinTermFreq(localParams.getInt("mintf", MoreLikeThis.DEFAULT_MIN_TERM_FREQ));
    mlt.setMinDocFreq(localParams.getInt("mindf", 0));
    mlt.setMinWordLen(localParams.getInt("minwl", MoreLikeThis.DEFAULT_MIN_WORD_LENGTH));
    mlt.setMaxWordLen(localParams.getInt("maxwl", MoreLikeThis.DEFAULT_MAX_WORD_LENGTH));
    mlt.setMaxQueryTerms(localParams.getInt("maxqt", MoreLikeThis.DEFAULT_MAX_QUERY_TERMS));
    mlt.setMaxNumTokensParsed(localParams.getInt("maxntp", MoreLikeThis.DEFAULT_MAX_NUM_TOKENS_PARSED));
    mlt.setMaxDocFreq(localParams.getInt("maxdf", MoreLikeThis.DEFAULT_MAX_DOC_FREQ));
    Boolean boost = localParams.getBool("boost", MoreLikeThis.DEFAULT_BOOST);
    mlt.setBoost(boost);
    mlt.setAnalyzer(req.getSchema().getIndexAnalyzer());
    Map<String, Collection<Object>> filteredDocument = new HashMap<>();
    String[] fieldNames;
    if (qf != null) {
        ArrayList<String> fields = new ArrayList();
        for (String fieldName : qf) {
            if (!StringUtils.isEmpty(fieldName)) {
                String[] strings = splitList.split(fieldName);
                for (String string : strings) {
                    if (!StringUtils.isEmpty(string)) {
                        fields.add(string);
                    }
                }
            }
        }
        // Parse field names and boosts from the fields
        boostFields = SolrPluginUtils.parseFieldBoosts(fields.toArray(new String[0]));
        fieldNames = boostFields.keySet().toArray(new String[0]);
    } else {
        ArrayList<String> fields = new ArrayList();
        for (String field : doc.getFieldNames()) {
            // Only use fields that are stored and have an explicit analyzer.
            // This makes sense as the query uses tf/idf/.. for query construction.
            // We might want to relook and change this in the future though.
            SchemaField f = req.getSchema().getFieldOrNull(field);
            if (f != null && f.stored() && f.getType().isExplicitAnalyzer()) {
                fields.add(field);
            }
        }
        fieldNames = fields.toArray(new String[0]);
    }
    if (fieldNames.length < 1) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "MoreLikeThis requires at least one similarity field: qf");
    }
    mlt.setFieldNames(fieldNames);
    for (String field : fieldNames) {
        Collection<Object> fieldValues = doc.getFieldValues(field);
        if (fieldValues != null) {
            Collection<Object> values = new ArrayList();
            for (Object val : fieldValues) {
                if (val instanceof IndexableField) {
                    values.add(((IndexableField) val).stringValue());
                } else {
                    values.add(val);
                }
            }
            filteredDocument.put(field, values);
        }
    }
    try {
        Query rawMLTQuery = mlt.like(filteredDocument);
        BooleanQuery boostedMLTQuery = (BooleanQuery) rawMLTQuery;
        if (boost && boostFields.size() > 0) {
            BooleanQuery.Builder newQ = new BooleanQuery.Builder();
            newQ.setMinimumNumberShouldMatch(boostedMLTQuery.getMinimumNumberShouldMatch());
            for (BooleanClause clause : boostedMLTQuery) {
                Query q = clause.getQuery();
                float originalBoost = 1f;
                if (q instanceof BoostQuery) {
                    BoostQuery bq = (BoostQuery) q;
                    q = bq.getQuery();
                    originalBoost = bq.getBoost();
                }
                Float fieldBoost = boostFields.get(((TermQuery) q).getTerm().field());
                q = ((fieldBoost != null) ? new BoostQuery(q, fieldBoost * originalBoost) : clause.getQuery());
                newQ.add(q, clause.getOccur());
            }
            boostedMLTQuery = newQ.build();
        }
        // exclude current document from results
        BooleanQuery.Builder realMLTQuery = new BooleanQuery.Builder();
        realMLTQuery.add(boostedMLTQuery, BooleanClause.Occur.MUST);
        realMLTQuery.add(createIdQuery(req.getSchema().getUniqueKeyField().getName(), id), BooleanClause.Occur.MUST_NOT);
        return realMLTQuery.build();
    } catch (IOException e) {
        e.printStackTrace();
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Bad Request");
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) HashMap(java.util.HashMap) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) ArrayList(java.util.ArrayList) MoreLikeThis(org.apache.lucene.queries.mlt.MoreLikeThis) BoostQuery(org.apache.lucene.search.BoostQuery) SolrDocument(org.apache.solr.common.SolrDocument) SolrException(org.apache.solr.common.SolrException) TermQuery(org.apache.lucene.search.TermQuery) IOException(java.io.IOException) SchemaField(org.apache.solr.schema.SchemaField) IndexableField(org.apache.lucene.index.IndexableField) BooleanClause(org.apache.lucene.search.BooleanClause) Collection(java.util.Collection)

Example 20 with SolrDocument

use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.

the class StoredFieldsShardResponseProcessor method process.

/**
   * {@inheritDoc}
   */
@Override
public void process(ResponseBuilder rb, ShardRequest shardRequest) {
    boolean returnScores = (rb.getFieldFlags() & SolrIndexSearcher.GET_SCORES) != 0;
    ShardResponse srsp = shardRequest.responses.get(0);
    SolrDocumentList docs = (SolrDocumentList) srsp.getSolrResponse().getResponse().get("response");
    String uniqueIdFieldName = rb.req.getSchema().getUniqueKeyField().getName();
    for (SolrDocument doc : docs) {
        Object id = doc.getFieldValue(uniqueIdFieldName).toString();
        ShardDoc shardDoc = rb.resultIds.get(id);
        FieldDoc fieldDoc = (FieldDoc) shardDoc;
        if (shardDoc != null) {
            if (returnScores && !Float.isNaN(fieldDoc.score)) {
                doc.setField("score", fieldDoc.score);
            }
            rb.retrievedDocuments.put(id, doc);
        }
    }
}
Also used : ShardResponse(org.apache.solr.handler.component.ShardResponse) SolrDocument(org.apache.solr.common.SolrDocument) FieldDoc(org.apache.lucene.search.FieldDoc) SolrDocumentList(org.apache.solr.common.SolrDocumentList) ShardDoc(org.apache.solr.handler.component.ShardDoc)

Aggregations

SolrDocument (org.apache.solr.common.SolrDocument)230 SolrDocumentList (org.apache.solr.common.SolrDocumentList)93 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)81 ArrayList (java.util.ArrayList)50 SolrQuery (org.apache.solr.client.solrj.SolrQuery)47 Test (org.junit.Test)46 SolrParams (org.apache.solr.common.params.SolrParams)38 SolrServerException (org.apache.solr.client.solrj.SolrServerException)35 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)35 IOException (java.io.IOException)32 SolrInputDocument (org.apache.solr.common.SolrInputDocument)28 HashMap (java.util.HashMap)26 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)24 NamedList (org.apache.solr.common.util.NamedList)21 Map (java.util.Map)20 List (java.util.List)18 SolrClient (org.apache.solr.client.solrj.SolrClient)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)12 SolrException (org.apache.solr.common.SolrException)12