Search in sources :

Example 56 with IndexableField

use of org.apache.lucene.index.IndexableField in project gerrit by GerritCodeReview.

the class LuceneChangeIndex method decodeReviewedBy.

private void decodeReviewedBy(ListMultimap<String, IndexableField> doc, ChangeData cd) {
    Collection<IndexableField> reviewedBy = doc.get(REVIEWEDBY_FIELD);
    if (reviewedBy.size() > 0) {
        Set<Account.Id> accounts = Sets.newHashSetWithExpectedSize(reviewedBy.size());
        for (IndexableField r : reviewedBy) {
            int id = r.numericValue().intValue();
            if (reviewedBy.size() == 1 && id == ChangeField.NOT_REVIEWED) {
                break;
            }
            accounts.add(new Account.Id(id));
        }
        cd.setReviewedBy(accounts);
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) Account(com.google.gerrit.reviewdb.client.Account)

Example 57 with IndexableField

use of org.apache.lucene.index.IndexableField in project lucene-solr by apache.

the class CollationTestBase method assertMatches.

// Make sure the documents returned by the search match the expected list
// Copied from TestSort.java
private void assertMatches(IndexSearcher searcher, Query query, Sort sort, String expectedResult) throws IOException {
    ScoreDoc[] result = searcher.search(query, 1000, sort).scoreDocs;
    StringBuilder buff = new StringBuilder(10);
    int n = result.length;
    for (int i = 0; i < n; ++i) {
        Document doc = searcher.doc(result[i].doc);
        IndexableField[] v = doc.getFields("tracer");
        for (int j = 0; j < v.length; ++j) {
            buff.append(v[j].stringValue());
        }
    }
    assertEquals(expectedResult, buff.toString());
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 58 with IndexableField

use of org.apache.lucene.index.IndexableField in project lucene-solr by apache.

the class PointField method createFields.

@Override
public List<IndexableField> createFields(SchemaField sf, Object value) {
    if (!isFieldUsed(sf)) {
        return Collections.emptyList();
    }
    List<IndexableField> fields = new ArrayList<>(3);
    IndexableField field = null;
    if (sf.indexed()) {
        field = createField(sf, value);
        fields.add(field);
    }
    if (sf.hasDocValues()) {
        final Number numericValue;
        if (field == null) {
            final Object nativeTypeObject = toNativeType(value);
            if (getNumberType() == NumberType.DATE) {
                numericValue = ((Date) nativeTypeObject).getTime();
            } else {
                numericValue = (Number) nativeTypeObject;
            }
        } else {
            numericValue = field.numericValue();
        }
        final long bits;
        if (!sf.multiValued()) {
            if (numericValue instanceof Integer || numericValue instanceof Long) {
                bits = numericValue.longValue();
            } else if (numericValue instanceof Float) {
                bits = Float.floatToIntBits(numericValue.floatValue());
            } else {
                assert numericValue instanceof Double;
                bits = Double.doubleToLongBits(numericValue.doubleValue());
            }
            fields.add(new NumericDocValuesField(sf.getName(), bits));
        } else {
            // MultiValued
            if (numericValue instanceof Integer || numericValue instanceof Long) {
                bits = numericValue.longValue();
            } else if (numericValue instanceof Float) {
                bits = NumericUtils.floatToSortableInt(numericValue.floatValue());
            } else {
                assert numericValue instanceof Double;
                bits = NumericUtils.doubleToSortableLong(numericValue.doubleValue());
            }
            fields.add(new SortedNumericDocValuesField(sf.getName(), bits));
        }
    }
    if (sf.stored()) {
        fields.add(getStoredField(sf, value));
    }
    return fields;
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) ArrayList(java.util.ArrayList)

Example 59 with IndexableField

use of org.apache.lucene.index.IndexableField in project lucene-solr by apache.

the class TrieDateFieldSource method createFields.

@Override
public List<IndexableField> createFields(SchemaField sf, Object value) {
    if (sf.hasDocValues()) {
        List<IndexableField> fields = new ArrayList<>();
        final IndexableField field = createField(sf, value);
        fields.add(field);
        if (sf.multiValued()) {
            BytesRefBuilder bytes = new BytesRefBuilder();
            storedToIndexed(field, bytes);
            fields.add(new SortedSetDocValuesField(sf.getName(), bytes.get()));
        } else {
            final long bits;
            if (field.numericValue() instanceof Integer || field.numericValue() instanceof Long) {
                bits = field.numericValue().longValue();
            } else if (field.numericValue() instanceof Float) {
                bits = Float.floatToIntBits(field.numericValue().floatValue());
            } else {
                assert field.numericValue() instanceof Double;
                bits = Double.doubleToLongBits(field.numericValue().doubleValue());
            }
            fields.add(new NumericDocValuesField(sf.getName(), bits));
        }
        return fields;
    } else {
        return Collections.singletonList(createField(sf, value));
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) ArrayList(java.util.ArrayList) MutableValueLong(org.apache.lucene.util.mutable.MutableValueLong) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField)

Example 60 with IndexableField

use of org.apache.lucene.index.IndexableField 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)

Aggregations

IndexableField (org.apache.lucene.index.IndexableField)276 Document (org.apache.lucene.document.Document)90 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)75 Matchers.containsString (org.hamcrest.Matchers.containsString)57 BytesRef (org.apache.lucene.util.BytesRef)53 ArrayList (java.util.ArrayList)50 Field (org.apache.lucene.document.Field)34 Test (org.junit.Test)28 IOException (java.io.IOException)27 HashMap (java.util.HashMap)24 IndexReader (org.apache.lucene.index.IndexReader)24 Directory (org.apache.lucene.store.Directory)23 Map (java.util.Map)22 TopDocs (org.apache.lucene.search.TopDocs)22 Term (org.apache.lucene.index.Term)21 HashSet (java.util.HashSet)20 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)20 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)20 Query (org.apache.lucene.search.Query)19 Analyzer (org.apache.lucene.analysis.Analyzer)18