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, SortedSetSelector.Type selector) 

Source Link

Document

Creates a sort, possibly in reverse, specifying how the sort value from the document's set is selected.

Usage

From source file:org.elasticsearch.action.admin.indices.create.ShrinkIndexIT.java

License:Apache License

public void testCreateShrinkWithIndexSort() throws Exception {
    SortField expectedSortField = new SortedSetSortField("id", true, SortedSetSelector.Type.MAX);
    expectedSortField.setMissingValue(SortedSetSortField.STRING_FIRST);
    Sort expectedIndexSort = new Sort(expectedSortField);
    internalCluster().ensureAtLeastNumDataNodes(2);
    prepareCreate("source")
            .setSettings(Settings.builder().put(indexSettings()).put("sort.field", "id")
                    .put("sort.order", "desc").put("number_of_shards", 8).put("number_of_replicas", 0))
            .addMapping("type", "id", "type=keyword,doc_values=true").get();
    for (int i = 0; i < 20; i++) {
        client().prepareIndex("source", "type", Integer.toString(i))
                .setSource("{\"foo\" : \"bar\", \"id\" : " + i + "}", XContentType.JSON).get();
    }/*from w w  w  .  j  av  a 2  s.c  o  m*/
    ImmutableOpenMap<String, DiscoveryNode> dataNodes = client().admin().cluster().prepareState().get()
            .getState().nodes().getDataNodes();
    assertTrue("at least 2 nodes but was: " + dataNodes.size(), dataNodes.size() >= 2);
    DiscoveryNode[] discoveryNodes = dataNodes.values().toArray(DiscoveryNode.class);
    String mergeNode = discoveryNodes[0].getName();
    // ensure all shards are allocated otherwise the ensure green below might not succeed since we require the merge node
    // if we change the setting too quickly we will end up with one replica unassigned which can't be assigned anymore due
    // to the require._name below.
    ensureGreen();

    flushAndRefresh();
    assertSortedSegments("source", expectedIndexSort);

    // relocate all shards to one node such that we can merge it.
    client().admin().indices().prepareUpdateSettings("source").setSettings(Settings.builder()
            .put("index.routing.allocation.require._name", mergeNode).put("index.blocks.write", true)).get();
    ensureGreen();

    // check that index sort cannot be set on the target index
    IllegalArgumentException exc = expectThrows(IllegalArgumentException.class,
            () -> client().admin().indices().prepareShrinkIndex("source", "target")
                    .setSettings(Settings.builder().put("index.number_of_replicas", 0)
                            .put("index.number_of_shards", "2").put("index.sort.field", "foo").build())
                    .get());
    assertThat(exc.getMessage(), containsString("can't override index sort when shrinking index"));

    // check that the index sort order of `source` is correctly applied to the `target`
    assertAcked(client().admin().indices().prepareShrinkIndex("source", "target").setSettings(
            Settings.builder().put("index.number_of_replicas", 0).put("index.number_of_shards", "2").build())
            .get());
    ensureGreen();
    flushAndRefresh();
    GetSettingsResponse settingsResponse = client().admin().indices().prepareGetSettings("target").execute()
            .actionGet();
    assertEquals(settingsResponse.getSetting("target", "index.sort.field"), "id");
    assertEquals(settingsResponse.getSetting("target", "index.sort.order"), "desc");
    assertSortedSegments("target", expectedIndexSort);

    // ... and that the index sort is also applied to updates
    for (int i = 20; i < 40; i++) {
        client().prepareIndex("target", "type")
                .setSource("{\"foo\" : \"bar\", \"i\" : " + i + "}", XContentType.JSON).get();
    }
    flushAndRefresh();
    assertSortedSegments("target", expectedIndexSort);
}

From source file:org.elasticsearch.action.admin.indices.create.SplitIndexIT.java

License:Apache License

public void testCreateSplitWithIndexSort() throws Exception {
    SortField expectedSortField = new SortedSetSortField("id", true, SortedSetSelector.Type.MAX);
    expectedSortField.setMissingValue(SortedSetSortField.STRING_FIRST);
    Sort expectedIndexSort = new Sort(expectedSortField);
    internalCluster().ensureAtLeastNumDataNodes(2);
    prepareCreate("source")
            .setSettings(Settings.builder().put(indexSettings()).put("sort.field", "id")
                    .put("sort.order", "desc").put("number_of_shards", 2).put("number_of_replicas", 0))
            .addMapping("type", "id", "type=keyword,doc_values=true").get();
    for (int i = 0; i < 20; i++) {
        client().prepareIndex("source", "type", Integer.toString(i))
                .setSource("{\"foo\" : \"bar\", \"id\" : " + i + "}", XContentType.JSON).get();
    }/* w  ww. j  av a 2  s.c  o m*/
    ImmutableOpenMap<String, DiscoveryNode> dataNodes = client().admin().cluster().prepareState().get()
            .getState().nodes().getDataNodes();
    assertTrue("at least 2 nodes but was: " + dataNodes.size(), dataNodes.size() >= 2);
    DiscoveryNode[] discoveryNodes = dataNodes.values().toArray(DiscoveryNode.class);
    String mergeNode = discoveryNodes[0].getName();
    // ensure all shards are allocated otherwise the ensure green below might not succeed since we require the merge node
    // if we change the setting too quickly we will end up with one replica unassigned which can't be assigned anymore due
    // to the require._name below.
    ensureGreen();

    flushAndRefresh();
    assertSortedSegments("source", expectedIndexSort);

    client().admin().indices().prepareUpdateSettings("source")
            .setSettings(Settings.builder().put("index.blocks.write", true)).get();
    ensureYellow();

    // check that index sort cannot be set on the target index
    IllegalArgumentException exc = expectThrows(IllegalArgumentException.class,
            () -> client().admin().indices().prepareResizeIndex("source", "target")
                    .setResizeType(ResizeType.SPLIT)
                    .setSettings(Settings.builder().put("index.number_of_replicas", 0)
                            .put("index.number_of_shards", 4).put("index.sort.field", "foo").build())
                    .get());
    assertThat(exc.getMessage(), containsString("can't override index sort when resizing an index"));

    // check that the index sort order of `source` is correctly applied to the `target`
    assertAcked(client().admin().indices().prepareResizeIndex("source", "target")
            .setResizeType(ResizeType.SPLIT).setSettings(Settings.builder().put("index.number_of_replicas", 0)
                    .put("index.number_of_shards", 4).build())
            .get());
    ensureGreen();
    flushAndRefresh();
    GetSettingsResponse settingsResponse = client().admin().indices().prepareGetSettings("target").execute()
            .actionGet();
    assertEquals(settingsResponse.getSetting("target", "index.sort.field"), "id");
    assertEquals(settingsResponse.getSetting("target", "index.sort.order"), "desc");
    assertSortedSegments("target", expectedIndexSort);

    // ... and that the index sort is also applied to updates
    for (int i = 20; i < 40; i++) {
        client().prepareIndex("target", "type")
                .setSource("{\"foo\" : \"bar\", \"i\" : " + i + "}", XContentType.JSON).get();
    }
    flushAndRefresh();
    assertSortedSegments("target", expectedIndexSort);
}

From source file:org.elasticsearch.index.engine.SegmentTests.java

License:Apache License

static SortField randomSortField() {
    if (randomBoolean()) {
        SortedNumericSortField field = new SortedNumericSortField(randomAlphaOfLengthBetween(1, 10),
                SortField.Type.INT, randomBoolean(),
                randomBoolean() ? SortedNumericSelector.Type.MAX : SortedNumericSelector.Type.MIN);
        if (randomBoolean()) {
            field.setMissingValue(randomInt());
        }/*from w  w w.j a v  a  2 s  .  c o  m*/
        return field;
    } else {
        SortedSetSortField field = new SortedSetSortField(randomAlphaOfLengthBetween(1, 10), randomBoolean(),
                randomBoolean() ? SortedSetSelector.Type.MAX : SortedSetSelector.Type.MIN);
        if (randomBoolean()) {
            field.setMissingValue(
                    randomBoolean() ? SortedSetSortField.STRING_FIRST : SortedSetSortField.STRING_LAST);
        }
        return field;
    }
}