Example usage for org.apache.lucene.search SortedSetSortField SortedSetSortField

List of usage examples for org.apache.lucene.search SortedSetSortField SortedSetSortField

Introduction

In this page you can find the example usage for org.apache.lucene.search SortedSetSortField SortedSetSortField.

Prototype

public SortedSetSortField(String field, boolean reverse) 

Source Link

Document

Creates a sort, possibly in reverse, by the minimum value in the set for the document.

Usage

From source file:org.elasticsearch.index.IndexSortIT.java

License:Apache License

public void testIndexSort() {
    SortField dateSort = new SortedNumericSortField("date", SortField.Type.LONG, false);
    dateSort.setMissingValue(Long.MAX_VALUE);
    SortField numericSort = new SortedNumericSortField("numeric_dv", SortField.Type.LONG, false);
    numericSort.setMissingValue(Long.MAX_VALUE);
    SortField keywordSort = new SortedSetSortField("keyword_dv", false);
    keywordSort.setMissingValue(SortField.STRING_LAST);
    Sort indexSort = new Sort(dateSort, numericSort, keywordSort);
    prepareCreate("test")
            .setSettings(Settings.builder().put(indexSettings()).put("index.number_of_shards", "1")
                    .put("index.number_of_replicas", "1")
                    .putList("index.sort.field", "date", "numeric_dv", "keyword_dv"))
            .addMapping("test", TEST_MAPPING).get();
    for (int i = 0; i < 20; i++) {
        client().prepareIndex("test", "test", Integer.toString(i))
                .setSource("numeric_dv", randomInt(), "keyword_dv", randomAlphaOfLengthBetween(10, 20)).get();
    }//from   ww w .  j  ava2 s. c  o m
    flushAndRefresh();
    ensureYellow();
    assertSortedSegments("test", indexSort);
}

From source file:org.elasticsearch.search.searchafter.SearchAfterBuilderTests.java

License:Apache License

public void testExtractSortType() throws Exception {
    SortField.Type type = extractSortType(LatLonDocValuesField.newDistanceSort("field", 0.0, 180.0));
    assertThat(type, equalTo(SortField.Type.DOUBLE));
    IndexFieldData.XFieldComparatorSource source = new IndexFieldData.XFieldComparatorSource(null,
            MultiValueMode.MIN, null) {//from   w w w.j a  v a 2  s.co  m
        @Override
        public SortField.Type reducedType() {
            return SortField.Type.STRING;
        }

        @Override
        public FieldComparator<?> newComparator(String fieldname, int numHits, int sortPos, boolean reversed) {
            return null;
        }
    };

    type = extractSortType(new SortField("field", source));
    assertThat(type, equalTo(SortField.Type.STRING));

    type = extractSortType(new SortedNumericSortField("field", SortField.Type.DOUBLE));
    assertThat(type, equalTo(SortField.Type.DOUBLE));

    type = extractSortType(new SortedSetSortField("field", false));
    assertThat(type, equalTo(SortField.Type.STRING));
}

From source file:org.neo4j.index.impl.lucene.legacy.TestLuceneIndex.java

License:Open Source License

@Test
public void queryIndexWithSortByString() throws Exception {
    Index<Node> index = nodeIndex(stringMap());
    String stringProperty = "NODE_NAME";

    String[] names = new String[] { "Fry", "Leela", "Bender", "Amy", "Hubert", "Calculon" };
    try (Transaction transaction = graphDb.beginTx()) {
        for (String name : names) {
            Node node = graphDb.createNode();
            node.setProperty(stringProperty, name);
            index.add(node, stringProperty, name);
        }/*from   w w  w. j a  v a 2  s  . c om*/
        transaction.success();
    }

    try (Transaction transaction = graphDb.beginTx()) {
        QueryContext queryContext = new QueryContext(stringProperty + ":**");
        queryContext.sort(new Sort(new SortedSetSortField(stringProperty, true)));
        IndexHits<Node> nodes = index.query(queryContext);

        int nameIndex = 0;
        String[] sortedNames = new String[] { "Leela", "Hubert", "Fry", "Calculon", "Bender", "Amy" };
        for (Node node : nodes) {
            assertEquals("Nodes should be sorted by string property", sortedNames[nameIndex++],
                    node.getProperty(stringProperty));
        }
        transaction.success();
    }
}