Search in sources :

Example 31 with ScoreDoc

use of org.apache.lucene.search.ScoreDoc in project lucene-solr by apache.

the class TopGroupsResultTransformer method serializeTopDocs.

protected NamedList serializeTopDocs(QueryCommandResult result) throws IOException {
    NamedList<Object> queryResult = new NamedList<>();
    queryResult.add("matches", result.getMatches());
    queryResult.add("totalHits", result.getTopDocs().totalHits);
    // debug: assert !Float.isNaN(result.getTopDocs().getMaxScore()) == rb.getGroupingSpec().isNeedScore();
    if (!Float.isNaN(result.getTopDocs().getMaxScore())) {
        queryResult.add("maxScore", result.getTopDocs().getMaxScore());
    }
    List<NamedList> documents = new ArrayList<>();
    queryResult.add("documents", documents);
    final IndexSchema schema = rb.req.getSearcher().getSchema();
    SchemaField uniqueField = schema.getUniqueKeyField();
    for (ScoreDoc scoreDoc : result.getTopDocs().scoreDocs) {
        NamedList<Object> document = new NamedList<>();
        documents.add(document);
        Document doc = retrieveDocument(uniqueField, scoreDoc.doc);
        document.add(ID, uniqueField.getType().toExternal(doc.getField(uniqueField.getName())));
        if (!Float.isNaN(scoreDoc.score)) {
            document.add("score", scoreDoc.score);
        }
        if (!FieldDoc.class.isInstance(scoreDoc)) {
            // thus don't add sortValues below
            continue;
        }
        FieldDoc fieldDoc = (FieldDoc) scoreDoc;
        Object[] convertedSortValues = new Object[fieldDoc.fields.length];
        for (int j = 0; j < fieldDoc.fields.length; j++) {
            Object sortValue = fieldDoc.fields[j];
            Sort groupSort = rb.getGroupingSpec().getGroupSort();
            SchemaField field = groupSort.getSort()[j].getField() != null ? schema.getFieldOrNull(groupSort.getSort()[j].getField()) : null;
            convertedSortValues[j] = ShardResultTransformerUtils.marshalSortValue(sortValue, field);
        }
        document.add("sortValues", convertedSortValues);
    }
    return queryResult;
}
Also used : FieldDoc(org.apache.lucene.search.FieldDoc) NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) SchemaField(org.apache.solr.schema.SchemaField) Sort(org.apache.lucene.search.Sort) IndexSchema(org.apache.solr.schema.IndexSchema)

Example 32 with ScoreDoc

use of org.apache.lucene.search.ScoreDoc in project lucene-solr by apache.

the class TopGroupsResultTransformer method transformToNativeShardDoc.

protected ScoreDoc[] transformToNativeShardDoc(List<NamedList<Object>> documents, Sort groupSort, String shard, IndexSchema schema) {
    ScoreDoc[] scoreDocs = new ScoreDoc[documents.size()];
    int j = 0;
    for (NamedList<Object> document : documents) {
        Object docId = document.get(ID);
        if (docId != null) {
            docId = docId.toString();
        } else {
            log.error("doc {} has null 'id'", document);
        }
        Float score = (Float) document.get("score");
        if (score == null) {
            score = Float.NaN;
        }
        Object[] sortValues = null;
        Object sortValuesVal = document.get("sortValues");
        if (sortValuesVal != null) {
            sortValues = ((List) sortValuesVal).toArray();
            for (int k = 0; k < sortValues.length; k++) {
                SchemaField field = groupSort.getSort()[k].getField() != null ? schema.getFieldOrNull(groupSort.getSort()[k].getField()) : null;
                sortValues[k] = ShardResultTransformerUtils.unmarshalSortValue(sortValues[k], field);
            }
        } else {
            log.debug("doc {} has null 'sortValues'", document);
        }
        scoreDocs[j++] = new ShardDoc(score, sortValues, docId, shard);
    }
    return scoreDocs;
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) ShardDoc(org.apache.solr.handler.component.ShardDoc) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 33 with ScoreDoc

use of org.apache.lucene.search.ScoreDoc in project lucene-solr by apache.

the class MainEndResultTransformer method transform.

/**
   * {@inheritDoc}
   */
@Override
public void transform(Map<String, ?> result, ResponseBuilder rb, SolrDocumentSource solrDocumentSource) {
    Object value = result.get(rb.getGroupingSpec().getFields()[0]);
    if (TopGroups.class.isInstance(value)) {
        @SuppressWarnings("unchecked") TopGroups<BytesRef> topGroups = (TopGroups<BytesRef>) value;
        SolrDocumentList docList = new SolrDocumentList();
        docList.setStart(rb.getGroupingSpec().getOffset());
        docList.setNumFound(rb.totalHitCount);
        Float maxScore = Float.NEGATIVE_INFINITY;
        for (GroupDocs<BytesRef> group : topGroups.groups) {
            for (ScoreDoc scoreDoc : group.scoreDocs) {
                if (maxScore < scoreDoc.score) {
                    maxScore = scoreDoc.score;
                }
                docList.add(solrDocumentSource.retrieve(scoreDoc));
            }
        }
        if (maxScore != Float.NEGATIVE_INFINITY) {
            docList.setMaxScore(maxScore);
        }
        rb.rsp.addResponse(docList);
    }
}
Also used : TopGroups(org.apache.lucene.search.grouping.TopGroups) SolrDocumentList(org.apache.solr.common.SolrDocumentList) BytesRef(org.apache.lucene.util.BytesRef) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 34 with ScoreDoc

use of org.apache.lucene.search.ScoreDoc in project lucene-solr by apache.

the class GroupedEndResultTransformer method transform.

/**
   * {@inheritDoc}
   */
@Override
public void transform(Map<String, ?> result, ResponseBuilder rb, SolrDocumentSource solrDocumentSource) {
    NamedList<Object> commands = new SimpleOrderedMap<>();
    for (Map.Entry<String, ?> entry : result.entrySet()) {
        Object value = entry.getValue();
        if (TopGroups.class.isInstance(value)) {
            @SuppressWarnings("unchecked") TopGroups<BytesRef> topGroups = (TopGroups<BytesRef>) value;
            NamedList<Object> command = new SimpleOrderedMap<>();
            command.add("matches", rb.totalHitCount);
            Integer totalGroupCount = rb.mergedGroupCounts.get(entry.getKey());
            if (totalGroupCount != null) {
                command.add("ngroups", totalGroupCount);
            }
            List<NamedList> groups = new ArrayList<>();
            SchemaField groupField = searcher.getSchema().getField(entry.getKey());
            FieldType groupFieldType = groupField.getType();
            for (GroupDocs<BytesRef> group : topGroups.groups) {
                SimpleOrderedMap<Object> groupResult = new SimpleOrderedMap<>();
                if (group.groupValue != null) {
                    groupResult.add("groupValue", groupFieldType.toObject(groupField.createField(group.groupValue.utf8ToString())));
                } else {
                    groupResult.add("groupValue", null);
                }
                SolrDocumentList docList = new SolrDocumentList();
                docList.setNumFound(group.totalHits);
                if (!Float.isNaN(group.maxScore)) {
                    docList.setMaxScore(group.maxScore);
                }
                docList.setStart(rb.getGroupingSpec().getWithinGroupOffset());
                for (ScoreDoc scoreDoc : group.scoreDocs) {
                    docList.add(solrDocumentSource.retrieve(scoreDoc));
                }
                groupResult.add("doclist", docList);
                groups.add(groupResult);
            }
            command.add("groups", groups);
            commands.add(entry.getKey(), command);
        } else if (QueryCommandResult.class.isInstance(value)) {
            QueryCommandResult queryCommandResult = (QueryCommandResult) value;
            NamedList<Object> command = new SimpleOrderedMap<>();
            command.add("matches", queryCommandResult.getMatches());
            SolrDocumentList docList = new SolrDocumentList();
            docList.setNumFound(queryCommandResult.getTopDocs().totalHits);
            if (!Float.isNaN(queryCommandResult.getTopDocs().getMaxScore())) {
                docList.setMaxScore(queryCommandResult.getTopDocs().getMaxScore());
            }
            docList.setStart(rb.getGroupingSpec().getWithinGroupOffset());
            for (ScoreDoc scoreDoc : queryCommandResult.getTopDocs().scoreDocs) {
                docList.add(solrDocumentSource.retrieve(scoreDoc));
            }
            command.add("doclist", docList);
            commands.add(entry.getKey(), command);
        }
    }
    rb.rsp.add("grouped", commands);
}
Also used : NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) QueryCommandResult(org.apache.solr.search.grouping.distributed.command.QueryCommandResult) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) FieldType(org.apache.solr.schema.FieldType) ScoreDoc(org.apache.lucene.search.ScoreDoc) SchemaField(org.apache.solr.schema.SchemaField) TopGroups(org.apache.lucene.search.grouping.TopGroups) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) Map(java.util.Map) BytesRef(org.apache.lucene.util.BytesRef)

Example 35 with ScoreDoc

use of org.apache.lucene.search.ScoreDoc in project lucene-solr by apache.

the class SimpleMLTQParser method parse.

public Query parse() {
    String defaultField = req.getSchema().getUniqueKeyField().getName();
    String uniqueValue = localParams.get(QueryParsing.V);
    String[] qf = localParams.getParams("qf");
    SolrIndexSearcher searcher = req.getSearcher();
    Query docIdQuery = createIdQuery(defaultField, uniqueValue);
    Map<String, Float> boostFields = new HashMap<>();
    try {
        TopDocs td = searcher.search(docIdQuery, 1);
        if (td.totalHits != 1)
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error completing MLT request. Could not fetch " + "document with id [" + uniqueValue + "]");
        ScoreDoc[] scoreDocs = td.scoreDocs;
        MoreLikeThis mlt = new MoreLikeThis(req.getSearcher().getIndexReader());
        mlt.setMinTermFreq(localParams.getInt("mintf", MoreLikeThis.DEFAULT_MIN_TERM_FREQ));
        mlt.setMinDocFreq(localParams.getInt("mindf", MoreLikeThis.DEFAULT_MIN_DOC_FREQ));
        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", false);
        mlt.setBoost(boost);
        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 {
            Map<String, SchemaField> fieldDefinitions = req.getSearcher().getSchema().getFields();
            ArrayList<String> fields = new ArrayList();
            for (String fieldName : fieldDefinitions.keySet()) {
                if (fieldDefinitions.get(fieldName).indexed() && fieldDefinitions.get(fieldName).stored())
                    if (fieldDefinitions.get(fieldName).getType().getNumberType() == null)
                        fields.add(fieldName);
            }
            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);
        mlt.setAnalyzer(req.getSchema().getIndexAnalyzer());
        Query rawMLTQuery = mlt.like(scoreDocs[0].doc);
        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(docIdQuery, BooleanClause.Occur.MUST_NOT);
        return realMLTQuery.build();
    } catch (IOException e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error completing MLT request" + e.getMessage());
    }
}
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) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) SolrException(org.apache.solr.common.SolrException) TermQuery(org.apache.lucene.search.TermQuery) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) IOException(java.io.IOException) SchemaField(org.apache.solr.schema.SchemaField) BooleanClause(org.apache.lucene.search.BooleanClause)

Aggregations

ScoreDoc (org.apache.lucene.search.ScoreDoc)222 TopDocs (org.apache.lucene.search.TopDocs)124 IndexSearcher (org.apache.lucene.search.IndexSearcher)98 Document (org.apache.lucene.document.Document)95 Query (org.apache.lucene.search.Query)71 TermQuery (org.apache.lucene.search.TermQuery)52 IOException (java.io.IOException)48 ArrayList (java.util.ArrayList)46 IndexReader (org.apache.lucene.index.IndexReader)45 Term (org.apache.lucene.index.Term)39 Directory (org.apache.lucene.store.Directory)37 BooleanQuery (org.apache.lucene.search.BooleanQuery)27 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)26 Test (org.junit.Test)23 Sort (org.apache.lucene.search.Sort)22 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)21 HashMap (java.util.HashMap)20 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)20 FieldDoc (org.apache.lucene.search.FieldDoc)20 QueryParser (org.apache.lucene.queryparser.classic.QueryParser)18