Search in sources :

Example 1 with SortedSetSortField

use of org.apache.lucene.search.SortedSetSortField in project elasticsearch by elastic.

the class CollapsingTopDocsCollectorTests method testCollapseString.

public void testCollapseString() throws Exception {
    CollapsingDocValuesProducer producer = new CollapsingDocValuesProducer<BytesRef>() {

        @Override
        public BytesRef randomGroup(int maxGroup) {
            return new BytesRef(Integer.toString(randomIntBetween(0, maxGroup - 1)));
        }

        @Override
        public void add(Document doc, BytesRef value, boolean multivalued) {
            if (multivalued) {
                doc.add(new SortedSetDocValuesField("field", value));
            } else {
                doc.add(new SortedDocValuesField("field", value));
            }
        }

        @Override
        public SortField sortField(boolean multivalued) {
            if (multivalued) {
                return new SortedSetSortField("field", false);
            } else {
                return new SortField("field", SortField.Type.STRING_VAL);
            }
        }
    };
    assertSearchCollapse(producer, false);
}
Also used : SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) Document(org.apache.lucene.document.Document) BytesRef(org.apache.lucene.util.BytesRef)

Example 2 with SortedSetSortField

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

the class SimpleTextSegmentInfoFormat method write.

@Override
public void write(Directory dir, SegmentInfo si, IOContext ioContext) throws IOException {
    String segFileName = IndexFileNames.segmentFileName(si.name, "", SimpleTextSegmentInfoFormat.SI_EXTENSION);
    try (IndexOutput output = dir.createOutput(segFileName, ioContext)) {
        // Only add the file once we've successfully created it, else IFD assert can trip:
        si.addFile(segFileName);
        BytesRefBuilder scratch = new BytesRefBuilder();
        SimpleTextUtil.write(output, SI_VERSION);
        SimpleTextUtil.write(output, si.getVersion().toString(), scratch);
        SimpleTextUtil.writeNewline(output);
        SimpleTextUtil.write(output, SI_MIN_VERSION);
        if (si.getMinVersion() == null) {
            SimpleTextUtil.write(output, "null", scratch);
        } else {
            SimpleTextUtil.write(output, si.getMinVersion().toString(), scratch);
        }
        SimpleTextUtil.writeNewline(output);
        SimpleTextUtil.write(output, SI_DOCCOUNT);
        SimpleTextUtil.write(output, Integer.toString(si.maxDoc()), scratch);
        SimpleTextUtil.writeNewline(output);
        SimpleTextUtil.write(output, SI_USECOMPOUND);
        SimpleTextUtil.write(output, Boolean.toString(si.getUseCompoundFile()), scratch);
        SimpleTextUtil.writeNewline(output);
        Map<String, String> diagnostics = si.getDiagnostics();
        int numDiagnostics = diagnostics == null ? 0 : diagnostics.size();
        SimpleTextUtil.write(output, SI_NUM_DIAG);
        SimpleTextUtil.write(output, Integer.toString(numDiagnostics), scratch);
        SimpleTextUtil.writeNewline(output);
        if (numDiagnostics > 0) {
            for (Map.Entry<String, String> diagEntry : diagnostics.entrySet()) {
                SimpleTextUtil.write(output, SI_DIAG_KEY);
                SimpleTextUtil.write(output, diagEntry.getKey(), scratch);
                SimpleTextUtil.writeNewline(output);
                SimpleTextUtil.write(output, SI_DIAG_VALUE);
                SimpleTextUtil.write(output, diagEntry.getValue(), scratch);
                SimpleTextUtil.writeNewline(output);
            }
        }
        Map<String, String> attributes = si.getAttributes();
        SimpleTextUtil.write(output, SI_NUM_ATT);
        SimpleTextUtil.write(output, Integer.toString(attributes.size()), scratch);
        SimpleTextUtil.writeNewline(output);
        for (Map.Entry<String, String> attEntry : attributes.entrySet()) {
            SimpleTextUtil.write(output, SI_ATT_KEY);
            SimpleTextUtil.write(output, attEntry.getKey(), scratch);
            SimpleTextUtil.writeNewline(output);
            SimpleTextUtil.write(output, SI_ATT_VALUE);
            SimpleTextUtil.write(output, attEntry.getValue(), scratch);
            SimpleTextUtil.writeNewline(output);
        }
        Set<String> files = si.files();
        int numFiles = files == null ? 0 : files.size();
        SimpleTextUtil.write(output, SI_NUM_FILES);
        SimpleTextUtil.write(output, Integer.toString(numFiles), scratch);
        SimpleTextUtil.writeNewline(output);
        if (numFiles > 0) {
            for (String fileName : files) {
                SimpleTextUtil.write(output, SI_FILE);
                SimpleTextUtil.write(output, fileName, scratch);
                SimpleTextUtil.writeNewline(output);
            }
        }
        SimpleTextUtil.write(output, SI_ID);
        SimpleTextUtil.write(output, new BytesRef(si.getId()));
        SimpleTextUtil.writeNewline(output);
        Sort indexSort = si.getIndexSort();
        SimpleTextUtil.write(output, SI_SORT);
        final int numSortFields = indexSort == null ? 0 : indexSort.getSort().length;
        SimpleTextUtil.write(output, Integer.toString(numSortFields), scratch);
        SimpleTextUtil.writeNewline(output);
        for (int i = 0; i < numSortFields; ++i) {
            final SortField sortField = indexSort.getSort()[i];
            SimpleTextUtil.write(output, SI_SORT_FIELD);
            SimpleTextUtil.write(output, sortField.getField(), scratch);
            SimpleTextUtil.writeNewline(output);
            SimpleTextUtil.write(output, SI_SORT_TYPE);
            final String sortTypeString;
            final SortField.Type sortType;
            final boolean multiValued;
            if (sortField instanceof SortedSetSortField) {
                sortType = SortField.Type.STRING;
                multiValued = true;
            } else if (sortField instanceof SortedNumericSortField) {
                sortType = ((SortedNumericSortField) sortField).getNumericType();
                multiValued = true;
            } else {
                sortType = sortField.getType();
                multiValued = false;
            }
            switch(sortType) {
                case STRING:
                    if (multiValued) {
                        sortTypeString = "multi_valued_string";
                    } else {
                        sortTypeString = "string";
                    }
                    break;
                case LONG:
                    if (multiValued) {
                        sortTypeString = "multi_valued_long";
                    } else {
                        sortTypeString = "long";
                    }
                    break;
                case INT:
                    if (multiValued) {
                        sortTypeString = "multi_valued_int";
                    } else {
                        sortTypeString = "int";
                    }
                    break;
                case DOUBLE:
                    if (multiValued) {
                        sortTypeString = "multi_valued_double";
                    } else {
                        sortTypeString = "double";
                    }
                    break;
                case FLOAT:
                    if (multiValued) {
                        sortTypeString = "multi_valued_float";
                    } else {
                        sortTypeString = "float";
                    }
                    break;
                default:
                    throw new IllegalStateException("Unexpected sort type: " + sortField.getType());
            }
            SimpleTextUtil.write(output, sortTypeString, scratch);
            SimpleTextUtil.writeNewline(output);
            if (sortField instanceof SortedSetSortField) {
                SortedSetSelector.Type selector = ((SortedSetSortField) sortField).getSelector();
                final String selectorString;
                if (selector == SortedSetSelector.Type.MIN) {
                    selectorString = "min";
                } else if (selector == SortedSetSelector.Type.MIDDLE_MIN) {
                    selectorString = "middle_min";
                } else if (selector == SortedSetSelector.Type.MIDDLE_MAX) {
                    selectorString = "middle_max";
                } else if (selector == SortedSetSelector.Type.MAX) {
                    selectorString = "max";
                } else {
                    throw new IllegalStateException("Unexpected SortedSetSelector type selector: " + selector);
                }
                SimpleTextUtil.write(output, SI_SELECTOR_TYPE);
                SimpleTextUtil.write(output, selectorString, scratch);
                SimpleTextUtil.writeNewline(output);
            } else if (sortField instanceof SortedNumericSortField) {
                SortedNumericSelector.Type selector = ((SortedNumericSortField) sortField).getSelector();
                final String selectorString;
                if (selector == SortedNumericSelector.Type.MIN) {
                    selectorString = "min";
                } else if (selector == SortedNumericSelector.Type.MAX) {
                    selectorString = "max";
                } else {
                    throw new IllegalStateException("Unexpected SortedNumericSelector type selector: " + selector);
                }
                SimpleTextUtil.write(output, SI_SELECTOR_TYPE);
                SimpleTextUtil.write(output, selectorString, scratch);
                SimpleTextUtil.writeNewline(output);
            }
            SimpleTextUtil.write(output, SI_SORT_REVERSE);
            SimpleTextUtil.write(output, Boolean.toString(sortField.getReverse()), scratch);
            SimpleTextUtil.writeNewline(output);
            SimpleTextUtil.write(output, SI_SORT_MISSING);
            final Object missingValue = sortField.getMissingValue();
            final String missing;
            if (missingValue == null) {
                missing = "null";
            } else if (missingValue == SortField.STRING_FIRST) {
                missing = "first";
            } else if (missingValue == SortField.STRING_LAST) {
                missing = "last";
            } else {
                missing = missingValue.toString();
            }
            SimpleTextUtil.write(output, missing, scratch);
            SimpleTextUtil.writeNewline(output);
        }
        SimpleTextUtil.writeChecksum(output, scratch);
    }
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) IndexOutput(org.apache.lucene.store.IndexOutput) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) Sort(org.apache.lucene.search.Sort) SortedSetSelector(org.apache.lucene.search.SortedSetSelector) HashMap(java.util.HashMap) Map(java.util.Map) BytesRef(org.apache.lucene.util.BytesRef)

Example 3 with SortedSetSortField

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

the class TestIndexSorting method randomIndexSortField.

private static SortField randomIndexSortField() {
    boolean reversed = random().nextBoolean();
    SortField sortField;
    switch(random().nextInt(10)) {
        case 0:
            sortField = new SortField("int", SortField.Type.INT, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextInt());
            }
            break;
        case 1:
            sortField = new SortedNumericSortField("multi_valued_int", SortField.Type.INT, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextInt());
            }
            break;
        case 2:
            sortField = new SortField("long", SortField.Type.LONG, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextLong());
            }
            break;
        case 3:
            sortField = new SortedNumericSortField("multi_valued_long", SortField.Type.LONG, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextLong());
            }
            break;
        case 4:
            sortField = new SortField("float", SortField.Type.FLOAT, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextFloat());
            }
            break;
        case 5:
            sortField = new SortedNumericSortField("multi_valued_float", SortField.Type.FLOAT, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextFloat());
            }
            break;
        case 6:
            sortField = new SortField("double", SortField.Type.DOUBLE, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextDouble());
            }
            break;
        case 7:
            sortField = new SortedNumericSortField("multi_valued_double", SortField.Type.DOUBLE, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextDouble());
            }
            break;
        case 8:
            sortField = new SortField("bytes", SortField.Type.STRING, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(SortField.STRING_LAST);
            }
            break;
        case 9:
            sortField = new SortedSetSortField("multi_valued_bytes", reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(SortField.STRING_LAST);
            }
            break;
        default:
            sortField = null;
            fail();
    }
    return sortField;
}
Also used : SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField)

Example 4 with SortedSetSortField

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

the class TestIndexSorting method testBasicMultiValuedString.

public void testBasicMultiValuedString() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
    Sort indexSort = new Sort(new SortedSetSortField("foo", false));
    iwc.setIndexSort(indexSort);
    IndexWriter w = new IndexWriter(dir, iwc);
    Document doc = new Document();
    doc.add(new NumericDocValuesField("id", 3));
    doc.add(new SortedSetDocValuesField("foo", new BytesRef("zzz")));
    w.addDocument(doc);
    // so we get more than one segment, so that forceMerge actually does merge, since we only get a sorted segment by merging:
    w.commit();
    doc = new Document();
    doc.add(new NumericDocValuesField("id", 1));
    doc.add(new SortedSetDocValuesField("foo", new BytesRef("aaa")));
    doc.add(new SortedSetDocValuesField("foo", new BytesRef("zzz")));
    doc.add(new SortedSetDocValuesField("foo", new BytesRef("bcg")));
    w.addDocument(doc);
    w.commit();
    doc = new Document();
    doc.add(new NumericDocValuesField("id", 2));
    doc.add(new SortedSetDocValuesField("foo", new BytesRef("mmm")));
    doc.add(new SortedSetDocValuesField("foo", new BytesRef("pppp")));
    w.addDocument(doc);
    w.forceMerge(1);
    DirectoryReader r = DirectoryReader.open(w);
    LeafReader leaf = getOnlyLeafReader(r);
    assertEquals(3, leaf.maxDoc());
    NumericDocValues values = leaf.getNumericDocValues("id");
    assertEquals(0, values.nextDoc());
    assertEquals(1l, values.longValue());
    assertEquals(1, values.nextDoc());
    assertEquals(2l, values.longValue());
    assertEquals(2, values.nextDoc());
    assertEquals(3l, values.longValue());
    r.close();
    w.close();
    dir.close();
}
Also used : SortedSetSortField(org.apache.lucene.search.SortedSetSortField) Document(org.apache.lucene.document.Document) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Sort(org.apache.lucene.search.Sort) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 5 with SortedSetSortField

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

the class BaseSegmentInfoFormatTestCase method randomIndexSortField.

private SortField randomIndexSortField() {
    boolean reversed = random().nextBoolean();
    SortField sortField;
    switch(random().nextInt(10)) {
        case 0:
            sortField = new SortField(TestUtil.randomSimpleString(random()), SortField.Type.INT, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextInt());
            }
            break;
        case 1:
            sortField = new SortedNumericSortField(TestUtil.randomSimpleString(random()), SortField.Type.INT, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextInt());
            }
            break;
        case 2:
            sortField = new SortField(TestUtil.randomSimpleString(random()), SortField.Type.LONG, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextLong());
            }
            break;
        case 3:
            sortField = new SortedNumericSortField(TestUtil.randomSimpleString(random()), SortField.Type.LONG, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextLong());
            }
            break;
        case 4:
            sortField = new SortField(TestUtil.randomSimpleString(random()), SortField.Type.FLOAT, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextFloat());
            }
            break;
        case 5:
            sortField = new SortedNumericSortField(TestUtil.randomSimpleString(random()), SortField.Type.FLOAT, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextFloat());
            }
            break;
        case 6:
            sortField = new SortField(TestUtil.randomSimpleString(random()), SortField.Type.DOUBLE, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextDouble());
            }
            break;
        case 7:
            sortField = new SortedNumericSortField(TestUtil.randomSimpleString(random()), SortField.Type.DOUBLE, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(random().nextDouble());
            }
            break;
        case 8:
            sortField = new SortField(TestUtil.randomSimpleString(random()), SortField.Type.STRING, reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(SortField.STRING_LAST);
            }
            break;
        case 9:
            sortField = new SortedSetSortField(TestUtil.randomSimpleString(random()), reversed);
            if (random().nextBoolean()) {
                sortField.setMissingValue(SortField.STRING_LAST);
            }
            break;
        default:
            sortField = null;
            fail();
    }
    return sortField;
}
Also used : SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortField(org.apache.lucene.search.SortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField)

Aggregations

SortedSetSortField (org.apache.lucene.search.SortedSetSortField)17 SortField (org.apache.lucene.search.SortField)13 SortedNumericSortField (org.apache.lucene.search.SortedNumericSortField)12 Sort (org.apache.lucene.search.Sort)11 BytesRef (org.apache.lucene.util.BytesRef)7 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)6 Document (org.apache.lucene.document.Document)5 Version (org.apache.lucene.util.Version)5 SortedSetSelector (org.apache.lucene.search.SortedSetSelector)4 Directory (org.apache.lucene.store.Directory)4 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)3 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)3 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)3 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)3 SegmentInfo (org.apache.lucene.index.SegmentInfo)3 SortedNumericSelector (org.apache.lucene.search.SortedNumericSelector)3 ChecksumIndexInput (org.apache.lucene.store.ChecksumIndexInput)3 IndexOutput (org.apache.lucene.store.IndexOutput)3 HashMap (java.util.HashMap)2 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)2