Search in sources :

Example 81 with IndexableField

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

the class CollationField method createFields.

@Override
public List<IndexableField> createFields(SchemaField field, Object value) {
    if (field.hasDocValues()) {
        List<IndexableField> fields = new ArrayList<>();
        fields.add(createField(field, value));
        final BytesRef bytes = getCollationKey(field.getName(), value.toString());
        if (field.multiValued()) {
            fields.add(new SortedSetDocValuesField(field.getName(), bytes));
        } else {
            fields.add(new SortedDocValuesField(field.getName(), bytes));
        }
        return fields;
    } else {
        return Collections.singletonList(createField(field, value));
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) ArrayList(java.util.ArrayList) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) BytesRef(org.apache.lucene.util.BytesRef)

Example 82 with IndexableField

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

the class BasicFunctionalityTest method testTermVectorFields.

@Test
public void testTermVectorFields() {
    IndexSchema ischema = IndexSchemaFactory.buildIndexSchema(getSchemaFile(), solrConfig);
    // Solr field type
    SchemaField f;
    // Lucene field
    IndexableField luf;
    f = ischema.getField("test_basictv");
    luf = f.createField("test");
    assertTrue(f.storeTermVector());
    assertTrue(luf.fieldType().storeTermVectors());
    f = ischema.getField("test_notv");
    luf = f.createField("test");
    assertTrue(!f.storeTermVector());
    assertTrue(!luf.fieldType().storeTermVectors());
    f = ischema.getField("test_postv");
    luf = f.createField("test");
    assertTrue(f.storeTermVector() && f.storeTermPositions());
    assertTrue(luf.fieldType().storeTermVectorPositions());
    f = ischema.getField("test_offtv");
    luf = f.createField("test");
    assertTrue(f.storeTermVector() && f.storeTermOffsets());
    assertTrue(luf.fieldType().storeTermVectorOffsets());
    f = ischema.getField("test_posofftv");
    luf = f.createField("test");
    assertTrue(f.storeTermVector() && f.storeTermPositions() && f.storeTermOffsets());
    assertTrue(luf.fieldType().storeTermVectorOffsets() && luf.fieldType().storeTermVectorPositions());
    f = ischema.getField("test_posoffpaytv");
    luf = f.createField("test");
    assertTrue(f.storeTermVector() && f.storeTermPositions() && f.storeTermOffsets() && f.storeTermPayloads());
    assertTrue(luf.fieldType().storeTermVectorOffsets() && luf.fieldType().storeTermVectorPositions() && luf.fieldType().storeTermVectorPayloads());
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) IndexableField(org.apache.lucene.index.IndexableField) IndexSchema(org.apache.solr.schema.IndexSchema) Test(org.junit.Test)

Example 83 with IndexableField

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

the class BasicFunctionalityTest method testLazyField.

@Test
public void testLazyField() throws IOException {
    assertU(adoc("id", "7777", "title", "keyword", "test_hlt", mkstr(10000), "test_hlt", mkstr(20000), "test_hlt", mkstr(30000), "test_hlt", mkstr(40000)));
    assertU(commit());
    SolrCore core = h.getCore();
    // initial request
    SolrQueryRequest req = req("q", "id:7777", "fl", "id,title");
    SolrQueryResponse rsp = new SolrQueryResponse();
    core.execute(core.getRequestHandler(req.getParams().get(CommonParams.QT)), req, rsp);
    DocList dl = ((ResultContext) rsp.getResponse()).getDocList();
    DocIterator di = dl.iterator();
    Document d1 = req.getSearcher().doc(di.nextDoc());
    IndexableField[] values1 = null;
    // ensure fl field is non lazy, and non-fl field is lazy
    assertFalse(d1.getField("title") instanceof LazyDocument.LazyField);
    assertFalse(d1.getField("id") instanceof LazyDocument.LazyField);
    values1 = d1.getFields("test_hlt");
    assertEquals(4, values1.length);
    for (int i = 0; i < values1.length; i++) {
        assertTrue(values1[i] instanceof LazyDocument.LazyField);
        LazyDocument.LazyField f = (LazyDocument.LazyField) values1[i];
        assertFalse(f.hasBeenLoaded());
    }
    req.close();
    // followup request, different fl
    req = req("q", "id:7777", "fl", "id,test_hlt");
    rsp = new SolrQueryResponse();
    core.execute(core.getRequestHandler(req.getParams().get(CommonParams.QT)), req, rsp);
    dl = ((ResultContext) rsp.getResponse()).getDocList();
    di = dl.iterator();
    Document d2 = req.getSearcher().doc(di.nextDoc());
    // ensure same doc, same lazy field now
    assertTrue("Doc was not cached", d1 == d2);
    IndexableField[] values2 = d2.getFields("test_hlt");
    assertEquals(values1.length, values2.length);
    for (int i = 0; i < values1.length; i++) {
        assertSame("LazyField wasn't reused", values1[i], values2[i]);
        LazyDocument.LazyField f = (LazyDocument.LazyField) values1[i];
        // still not a real boy, no response writer in play
        assertFalse(f.hasBeenLoaded());
    }
    // actuallize one value
    assertNotNull(values2[0].stringValue());
    for (int i = 0; i < values2.length; i++) {
        // now all values for this field should be loaded & cached
        LazyDocument.LazyField f = (LazyDocument.LazyField) values2[i];
        assertTrue(f.hasBeenLoaded());
    }
    req.close();
}
Also used : ResultContext(org.apache.solr.response.ResultContext) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) DocIterator(org.apache.solr.search.DocIterator) SolrCore(org.apache.solr.core.SolrCore) LazyDocument(org.apache.lucene.document.LazyDocument) Document(org.apache.lucene.document.Document) IndexableField(org.apache.lucene.index.IndexableField) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) LazyDocument(org.apache.lucene.document.LazyDocument) DocList(org.apache.solr.search.DocList) Test(org.junit.Test)

Example 84 with IndexableField

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

the class DocumentBuilderTest method testMoveLargestLast.

public void testMoveLargestLast() {
    SolrInputDocument inDoc = new SolrInputDocument();
    // not stored.  It won't be moved.  This value is the longest, however.
    String TEXT_FLD = "text";
    inDoc.addField(TEXT_FLD, "NOT STORED|" + RandomStrings.randomAsciiOfLength(random(), 4 * DocumentBuilder.MIN_LENGTH_TO_MOVE_LAST));
    // stored, multiValued
    String CAT_FLD = "cat";
    inDoc.addField(CAT_FLD, "STORED V1|");
    //  pretty long value
    inDoc.addField(CAT_FLD, "STORED V2|" + RandomStrings.randomAsciiOfLength(random(), 2 * DocumentBuilder.MIN_LENGTH_TO_MOVE_LAST));
    inDoc.addField(CAT_FLD, "STORED V3|" + RandomStrings.randomAsciiOfLength(random(), DocumentBuilder.MIN_LENGTH_TO_MOVE_LAST));
    // stored.  This value is long, but not long enough.
    String SUBJECT_FLD = "subject";
    inDoc.addField(SUBJECT_FLD, "2ndplace|" + RandomStrings.randomAsciiOfLength(random(), DocumentBuilder.MIN_LENGTH_TO_MOVE_LAST));
    Document outDoc = DocumentBuilder.toDocument(inDoc, h.getCore().getLatestSchema());
    // filter outDoc by stored fields; convert to list.
    List<IndexableField> storedFields = StreamSupport.stream(outDoc.spliterator(), false).filter(f -> f.fieldType().stored()).collect(Collectors.toList());
    // clip to last 3.  We expect these to be for CAT_FLD
    storedFields = storedFields.subList(storedFields.size() - 3, storedFields.size());
    Iterator<IndexableField> fieldIterator = storedFields.iterator();
    IndexableField field;
    // Test that we retained the particular value ordering, even though though the 2nd of three was longest
    assertTrue(fieldIterator.hasNext());
    field = fieldIterator.next();
    assertEquals(CAT_FLD, field.name());
    assertTrue(field.stringValue().startsWith("STORED V1|"));
    assertTrue(fieldIterator.hasNext());
    field = fieldIterator.next();
    assertEquals(CAT_FLD, field.name());
    assertTrue(field.stringValue().startsWith("STORED V2|"));
    assertTrue(fieldIterator.hasNext());
    field = fieldIterator.next();
    assertEquals(CAT_FLD, field.name());
    assertTrue(field.stringValue().startsWith("STORED V3|"));
}
Also used : AfterClass(org.junit.AfterClass) Iterator(java.util.Iterator) BeforeClass(org.junit.BeforeClass) SolrCore(org.apache.solr.core.SolrCore) IndexableField(org.apache.lucene.index.IndexableField) FieldType(org.apache.solr.schema.FieldType) TestUtil(org.apache.lucene.util.TestUtil) Test(org.junit.Test) Collectors(java.util.stream.Collectors) SolrInputField(org.apache.solr.common.SolrInputField) SolrTestCaseJ4(org.apache.solr.SolrTestCaseJ4) SolrDocument(org.apache.solr.common.SolrDocument) SolrException(org.apache.solr.common.SolrException) List(java.util.List) Document(org.apache.lucene.document.Document) After(org.junit.After) StreamSupport(java.util.stream.StreamSupport) RandomStrings(com.carrotsearch.randomizedtesting.generators.RandomStrings) SolrInputDocument(org.apache.solr.common.SolrInputDocument) IndexableField(org.apache.lucene.index.IndexableField) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) Document(org.apache.lucene.document.Document) SolrInputDocument(org.apache.solr.common.SolrInputDocument)

Example 85 with IndexableField

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

the class LargeFieldTest method assertLazyLoaded.

private void assertLazyLoaded(Document d, String fieldName) {
    IndexableField field = d.getField(fieldName);
    if (fieldName == BIG_FIELD) {
        assertTrue(field instanceof SolrDocumentFetcher.LargeLazyField);
        assertTrue(((SolrDocumentFetcher.LargeLazyField) field).hasBeenLoaded());
    } else {
        assertTrue(field instanceof LazyDocument.LazyField);
        assertTrue(((LazyDocument.LazyField) field).hasBeenLoaded());
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) LazyDocument(org.apache.lucene.document.LazyDocument)

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