Search in sources :

Example 66 with IndexableField

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

the class GeoJSONWriter method writeGeo.

protected void writeGeo(Object geo) throws IOException {
    Shape shape = null;
    String str = null;
    if (geo instanceof Shape) {
        shape = (Shape) geo;
    } else if (geo instanceof IndexableField) {
        str = ((IndexableField) geo).stringValue();
    } else if (geo instanceof WriteableGeoJSON) {
        shape = ((WriteableGeoJSON) geo).shape;
    } else {
        str = geo.toString();
    }
    if (str != null) {
        // Assume it is well formed JSON
        if (str.startsWith("{") && str.endsWith("}")) {
            writer.write(str);
            return;
        }
        if (formats == null) {
            // *stored* values for the field look like JSON until we actually try to read them
            throw new SolrException(ErrorCode.BAD_REQUEST, "GeoJSON unable to write field: '&" + GeoJSONResponseWriter.FIELD + "=" + geofield + "' (" + str + ")");
        }
        shape = formats.read(str);
    }
    if (geowriter == null) {
        throw new SolrException(ErrorCode.BAD_REQUEST, "GeoJSON unable to write field: '&" + GeoJSONResponseWriter.FIELD + "=" + geofield + "'");
    }
    if (shape != null) {
        geowriter.write(writer, shape);
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) WriteableGeoJSON(org.apache.solr.response.transform.WriteableGeoJSON) Shape(org.locationtech.spatial4j.shape.Shape) SolrException(org.apache.solr.common.SolrException)

Example 67 with IndexableField

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

the class SolrDocumentFetcher method visitFromCached.

/** Executes a stored field visitor against a hit from the document cache */
private void visitFromCached(Document document, StoredFieldVisitor visitor) throws IOException {
    for (IndexableField f : document) {
        final FieldInfo info = searcher.getFieldInfos().fieldInfo(f.name());
        final StoredFieldVisitor.Status needsField = visitor.needsField(info);
        if (needsField == StoredFieldVisitor.Status.STOP)
            return;
        if (needsField == StoredFieldVisitor.Status.NO)
            continue;
        BytesRef binaryValue = f.binaryValue();
        if (binaryValue != null) {
            visitor.binaryField(info, toByteArrayUnwrapIfPossible(binaryValue));
            continue;
        }
        Number numericValue = f.numericValue();
        if (numericValue != null) {
            if (numericValue instanceof Double) {
                visitor.doubleField(info, numericValue.doubleValue());
            } else if (numericValue instanceof Integer) {
                visitor.intField(info, numericValue.intValue());
            } else if (numericValue instanceof Float) {
                visitor.floatField(info, numericValue.floatValue());
            } else if (numericValue instanceof Long) {
                visitor.longField(info, numericValue.longValue());
            } else {
                throw new AssertionError();
            }
            continue;
        }
        // must be String
        if (f instanceof LargeLazyField) {
            // optimization to avoid premature string conversion
            visitor.stringField(info, toByteArrayUnwrapIfPossible(((LargeLazyField) f).readBytes()));
        } else {
            visitor.stringField(info, f.stringValue().getBytes(StandardCharsets.UTF_8));
        }
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) StoredFieldVisitor(org.apache.lucene.index.StoredFieldVisitor) DocumentStoredFieldVisitor(org.apache.lucene.document.DocumentStoredFieldVisitor) FieldInfo(org.apache.lucene.index.FieldInfo) BytesRef(org.apache.lucene.util.BytesRef)

Example 68 with IndexableField

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

the class TestMemoryIndex method testPointValues.

public void testPointValues() throws Exception {
    List<Function<Long, IndexableField>> fieldFunctions = Arrays.asList((t) -> new IntPoint("number", t.intValue()), (t) -> new LongPoint("number", t), (t) -> new FloatPoint("number", t.floatValue()), (t) -> new DoublePoint("number", t.doubleValue()));
    List<Function<Long, Query>> exactQueryFunctions = Arrays.asList((t) -> IntPoint.newExactQuery("number", t.intValue()), (t) -> LongPoint.newExactQuery("number", t), (t) -> FloatPoint.newExactQuery("number", t.floatValue()), (t) -> DoublePoint.newExactQuery("number", t.doubleValue()));
    List<Function<long[], Query>> setQueryFunctions = Arrays.asList((t) -> IntPoint.newSetQuery("number", LongStream.of(t).mapToInt(value -> (int) value).toArray()), (t) -> LongPoint.newSetQuery("number", t), (t) -> FloatPoint.newSetQuery("number", Arrays.asList(LongStream.of(t).mapToObj(value -> (float) value).toArray(Float[]::new))), (t) -> DoublePoint.newSetQuery("number", LongStream.of(t).mapToDouble(value -> (double) value).toArray()));
    List<BiFunction<Long, Long, Query>> rangeQueryFunctions = Arrays.asList((t, u) -> IntPoint.newRangeQuery("number", t.intValue(), u.intValue()), (t, u) -> LongPoint.newRangeQuery("number", t, u), (t, u) -> FloatPoint.newRangeQuery("number", t.floatValue(), u.floatValue()), (t, u) -> DoublePoint.newRangeQuery("number", t.doubleValue(), u.doubleValue()));
    for (int i = 0; i < fieldFunctions.size(); i++) {
        Function<Long, IndexableField> fieldFunction = fieldFunctions.get(i);
        Function<Long, Query> exactQueryFunction = exactQueryFunctions.get(i);
        Function<long[], Query> setQueryFunction = setQueryFunctions.get(i);
        BiFunction<Long, Long, Query> rangeQueryFunction = rangeQueryFunctions.get(i);
        Document doc = new Document();
        for (int number = 1; number < 32; number += 2) {
            doc.add(fieldFunction.apply((long) number));
        }
        MemoryIndex mi = MemoryIndex.fromDocument(doc, analyzer);
        IndexSearcher indexSearcher = mi.createSearcher();
        Query query = exactQueryFunction.apply(5L);
        assertEquals(1, indexSearcher.count(query));
        query = exactQueryFunction.apply(4L);
        assertEquals(0, indexSearcher.count(query));
        query = setQueryFunction.apply(new long[] { 3L, 9L, 19L });
        assertEquals(1, indexSearcher.count(query));
        query = setQueryFunction.apply(new long[] { 2L, 8L, 13L });
        assertEquals(1, indexSearcher.count(query));
        query = setQueryFunction.apply(new long[] { 2L, 8L, 16L });
        assertEquals(0, indexSearcher.count(query));
        query = rangeQueryFunction.apply(2L, 16L);
        assertEquals(1, indexSearcher.count(query));
        query = rangeQueryFunction.apply(24L, 48L);
        assertEquals(1, indexSearcher.count(query));
        query = rangeQueryFunction.apply(48L, 68L);
        assertEquals(0, indexSearcher.count(query));
    }
}
Also used : Query(org.apache.lucene.search.Query) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) BinaryPoint(org.apache.lucene.document.BinaryPoint) FieldType(org.apache.lucene.document.FieldType) BiFunction(java.util.function.BiFunction) IndexableField(org.apache.lucene.index.IndexableField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Term(org.apache.lucene.index.Term) PhraseQuery(org.apache.lucene.search.PhraseQuery) StoredField(org.apache.lucene.document.StoredField) DoublePoint(org.apache.lucene.document.DoublePoint) Document(org.apache.lucene.document.Document) TermsEnum(org.apache.lucene.index.TermsEnum) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) BytesRef(org.apache.lucene.util.BytesRef) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) StandardCharsets(java.nio.charset.StandardCharsets) SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) List(java.util.List) FieldInvertState(org.apache.lucene.index.FieldInvertState) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) LeafReader(org.apache.lucene.index.LeafReader) LuceneTestCase(org.apache.lucene.util.LuceneTestCase) MockPayloadAnalyzer(org.apache.lucene.analysis.MockPayloadAnalyzer) BinaryDocValues(org.apache.lucene.index.BinaryDocValues) IndexReader(org.apache.lucene.index.IndexReader) BM25Similarity(org.apache.lucene.search.similarities.BM25Similarity) IndexSearcher(org.apache.lucene.search.IndexSearcher) LongPoint(org.apache.lucene.document.LongPoint) StringField(org.apache.lucene.document.StringField) NumericDocValues(org.apache.lucene.index.NumericDocValues) TestUtil(org.apache.lucene.util.TestUtil) CoreMatchers.not(org.hamcrest.CoreMatchers.not) Function(java.util.function.Function) StringContains.containsString(org.junit.internal.matchers.StringContains.containsString) Similarity(org.apache.lucene.search.similarities.Similarity) SortedSetDocValues(org.apache.lucene.index.SortedSetDocValues) IntPoint(org.apache.lucene.document.IntPoint) SortedDocValues(org.apache.lucene.index.SortedDocValues) TermStatistics(org.apache.lucene.search.TermStatistics) Before(org.junit.Before) LongStream(java.util.stream.LongStream) PostingsEnum(org.apache.lucene.index.PostingsEnum) FloatPoint(org.apache.lucene.document.FloatPoint) Analyzer(org.apache.lucene.analysis.Analyzer) IOException(java.io.IOException) Test(org.junit.Test) CollectionStatistics(org.apache.lucene.search.CollectionStatistics) TermQuery(org.apache.lucene.search.TermQuery) Field(org.apache.lucene.document.Field) DocValuesType(org.apache.lucene.index.DocValuesType) TextField(org.apache.lucene.document.TextField) IndexOptions(org.apache.lucene.index.IndexOptions) IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) LongPoint(org.apache.lucene.document.LongPoint) Document(org.apache.lucene.document.Document) BinaryPoint(org.apache.lucene.document.BinaryPoint) DoublePoint(org.apache.lucene.document.DoublePoint) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint) IndexableField(org.apache.lucene.index.IndexableField) IntPoint(org.apache.lucene.document.IntPoint) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) FloatPoint(org.apache.lucene.document.FloatPoint) BiFunction(java.util.function.BiFunction) DoublePoint(org.apache.lucene.document.DoublePoint)

Example 69 with IndexableField

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

the class DocumentDictionaryTest method testWithOptionalPayload.

@Test
public void testWithOptionalPayload() throws IOException {
    Directory dir = newDirectory();
    Analyzer analyzer = new MockAnalyzer(random());
    IndexWriterConfig iwc = newIndexWriterConfig(analyzer);
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc);
    // Create a document that is missing the payload field
    Document doc = new Document();
    Field field = new TextField(FIELD_NAME, "some field", Field.Store.YES);
    doc.add(field);
    // do not store the payload or the contexts
    Field weight = new NumericDocValuesField(WEIGHT_FIELD_NAME, 100);
    doc.add(weight);
    writer.addDocument(doc);
    writer.commit();
    writer.close();
    IndexReader ir = DirectoryReader.open(dir);
    // Even though the payload field is missing, the dictionary iterator should not skip the document
    // because the payload field is optional.
    Dictionary dictionaryOptionalPayload = new DocumentDictionary(ir, FIELD_NAME, WEIGHT_FIELD_NAME, PAYLOAD_FIELD_NAME);
    InputIterator inputIterator = dictionaryOptionalPayload.getEntryIterator();
    BytesRef f = inputIterator.next();
    assertTrue(f.equals(new BytesRef(doc.get(FIELD_NAME))));
    IndexableField weightField = doc.getField(WEIGHT_FIELD_NAME);
    assertEquals(inputIterator.weight(), weightField.numericValue().longValue());
    IndexableField payloadField = doc.getField(PAYLOAD_FIELD_NAME);
    assertNull(payloadField);
    assertTrue(inputIterator.payload().length == 0);
    IOUtils.close(ir, analyzer, dir);
}
Also used : Dictionary(org.apache.lucene.search.spell.Dictionary) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Document(org.apache.lucene.document.Document) IndexableField(org.apache.lucene.index.IndexableField) IndexableField(org.apache.lucene.index.IndexableField) StoredField(org.apache.lucene.document.StoredField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) TextField(org.apache.lucene.document.TextField) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Test(org.junit.Test)

Example 70 with IndexableField

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

the class DocumentValueSourceDictionaryTest method testWithValueSource.

@Test
public void testWithValueSource() throws IOException {
    Directory dir = newDirectory();
    Analyzer analyzer = new MockAnalyzer(random());
    IndexWriterConfig iwc = newIndexWriterConfig(analyzer);
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc);
    Map<String, Document> docs = generateIndexDocuments(atLeast(100));
    for (Document doc : docs.values()) {
        writer.addDocument(doc);
    }
    writer.commit();
    writer.close();
    IndexReader ir = DirectoryReader.open(dir);
    Dictionary dictionary = new DocumentValueSourceDictionary(ir, FIELD_NAME, LongValuesSource.constant(10), PAYLOAD_FIELD_NAME);
    InputIterator inputIterator = dictionary.getEntryIterator();
    BytesRef f;
    while ((f = inputIterator.next()) != null) {
        Document doc = docs.remove(f.utf8ToString());
        assertTrue(f.equals(new BytesRef(doc.get(FIELD_NAME))));
        assertEquals(inputIterator.weight(), 10);
        IndexableField payloadField = doc.getField(PAYLOAD_FIELD_NAME);
        if (payloadField == null)
            assertTrue(inputIterator.payload().length == 0);
        else
            assertEquals(inputIterator.payload(), payloadField.binaryValue());
    }
    assertTrue(docs.isEmpty());
    IOUtils.close(ir, analyzer, dir);
}
Also used : Dictionary(org.apache.lucene.search.spell.Dictionary) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Document(org.apache.lucene.document.Document) IndexableField(org.apache.lucene.index.IndexableField) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Test(org.junit.Test)

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