Example usage for org.apache.lucene.util BytesRef BytesRef

List of usage examples for org.apache.lucene.util BytesRef BytesRef

Introduction

In this page you can find the example usage for org.apache.lucene.util BytesRef BytesRef.

Prototype

public BytesRef(CharSequence text) 

Source Link

Document

Initialize the byte[] from the UTF8 bytes for the provided String.

Usage

From source file:io.crate.integrationtests.TransportSQLActionTest.java

License:Apache License

@Test
public void testInsertPartitionedTableReversedPartitionedColumns() throws Exception {
    execute("create table parted (id integer, name string, date timestamp)" + "partitioned by (name, date)");
    ensureGreen();/*w  w  w . j ava2 s .  co m*/

    Long dateValue = System.currentTimeMillis();
    execute("insert into parted (id, date, name) values (?, ?, ?)", new Object[] { 1, dateValue, "Trillian" });
    assertThat(response.rowCount(), is(1L));
    ensureGreen();
    refresh();
    String partitionName = new PartitionName("parted",
            Arrays.asList(new BytesRef("Trillian"), new BytesRef(dateValue.toString()))).stringValue();
    assertNotNull(client().admin().cluster().prepareState().execute().actionGet().getState().metaData()
            .indices().get(partitionName).aliases().get("parted"));
}

From source file:io.crate.integrationtests.TransportSQLActionTest.java

License:Apache License

@Test
public void testAlterPartitionTable() throws Exception {
    execute("create table quotes (id integer, quote string, date timestamp) "
            + "partitioned by(date) clustered into 3 shards with (number_of_replicas='0-all')");
    ensureGreen();//from   www  .ja  v a 2s  . com
    assertThat(response.rowCount(), is(1L));

    String templateName = PartitionName.templateName("quotes");
    GetIndexTemplatesResponse templatesResponse = client().admin().indices().prepareGetTemplates(templateName)
            .execute().actionGet();
    Settings templateSettings = templatesResponse.getIndexTemplates().get(0).getSettings();
    assertThat(templateSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0), is(1));
    assertThat(templateSettings.get(IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS), is("0-all"));
    assertThat(templateSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 0), is(3));

    execute("alter table quotes set (number_of_replicas=0)");
    ensureGreen();

    templatesResponse = client().admin().indices().prepareGetTemplates(templateName).execute().actionGet();
    templateSettings = templatesResponse.getIndexTemplates().get(0).getSettings();
    assertThat(templateSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0), is(0));
    assertThat(templateSettings.getAsBoolean(IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS, true), is(false));
    assertThat(templateSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 0), is(3));

    execute("insert into quotes (id, quote, date) values (?, ?, ?), (?, ?, ?)",
            new Object[] { 1, "Don't panic", 1395874800000L, 2, "Now panic", 1395961200000L });
    assertThat(response.rowCount(), is(2L));
    ensureGreen();
    refresh();

    assertTrue(clusterService().state().metaData().aliases().containsKey("quotes"));

    execute("select number_of_replicas, number_of_shards from information_schema.tables where table_name = 'quotes'");
    assertEquals("0", response.rows()[0][0]);
    assertEquals(3, response.rows()[0][1]);

    execute("alter table quotes set (number_of_replicas='1-all')");
    ensureGreen();

    execute("select number_of_replicas from information_schema.tables where table_name = 'quotes'");
    assertEquals("1-all", response.rows()[0][0]);

    templatesResponse = client().admin().indices().prepareGetTemplates(templateName).execute().actionGet();
    templateSettings = templatesResponse.getIndexTemplates().get(0).getSettings();
    assertThat(templateSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1), is(0));
    assertThat(templateSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 0), is(3));
    assertThat(templateSettings.get(IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS), is("1-all"));

    List<String> partitions = ImmutableList.of(
            new PartitionName("quotes", Arrays.asList(new BytesRef("1395874800000"))).stringValue(),
            new PartitionName("quotes", Arrays.asList(new BytesRef("1395961200000"))).stringValue());
    Thread.sleep(1000);
    GetSettingsResponse settingsResponse = client().admin().indices()
            .prepareGetSettings(partitions.get(0), partitions.get(1)).execute().get();

    for (String index : partitions) {
        assertThat(settingsResponse.getSetting(index, IndexMetaData.SETTING_NUMBER_OF_REPLICAS), is("1"));
        assertThat(settingsResponse.getSetting(index, IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS), is("1-all"));
    }
}

From source file:io.crate.integrationtests.TransportSQLActionTest.java

License:Apache License

@Test
public void testAlterTableResetPartitionedTable() throws Exception {
    execute("create table quotes (id integer, quote string, date timestamp) "
            + "partitioned by(date) clustered into 3 shards with (number_of_replicas='1-all')");
    ensureGreen();//from w  w  w. j a  v  a2  s.c  o  m
    assertThat(response.rowCount(), is(1L));

    execute("insert into quotes (id, quote, date) values (?, ?, ?), (?, ?, ?)",
            new Object[] { 1, "Don't panic", 1395874800000L, 2, "Now panic", 1395961200000L });
    assertThat(response.rowCount(), is(2L));
    ensureGreen();
    refresh();

    execute("alter table quotes reset (number_of_replicas)");
    ensureGreen();

    String templateName = PartitionName.templateName("quotes");
    GetIndexTemplatesResponse templatesResponse = client().admin().indices().prepareGetTemplates(templateName)
            .execute().actionGet();
    Settings templateSettings = templatesResponse.getIndexTemplates().get(0).getSettings();
    assertThat(templateSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0), is(1));
    assertThat(templateSettings.get(IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS), is("false"));

    List<String> partitions = ImmutableList.of(
            new PartitionName("quotes", Arrays.asList(new BytesRef("1395874800000"))).stringValue(),
            new PartitionName("quotes", Arrays.asList(new BytesRef("1395961200000"))).stringValue());
    Thread.sleep(1000);
    GetSettingsResponse settingsResponse = client().admin().indices()
            .prepareGetSettings(partitions.get(0), partitions.get(1)).execute().get();

    for (String index : partitions) {
        assertThat(settingsResponse.getSetting(index, IndexMetaData.SETTING_NUMBER_OF_REPLICAS), is("1"));
        assertThat(settingsResponse.getSetting(index, IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS), is("false"));
    }

}

From source file:io.crate.integrationtests.TransportSQLActionTest.java

License:Apache License

@Test
public void testAlterPartitionedTablePartition() throws Exception {
    execute("create table quotes (id integer, quote string, date timestamp) "
            + "partitioned by(date) clustered into 3 shards with (number_of_replicas=0)");
    ensureGreen();//from w  w w  .  j  a v  a2  s .c o m
    assertThat(response.rowCount(), is(1L));

    execute("insert into quotes (id, quote, date) values (?, ?, ?), (?, ?, ?)",
            new Object[] { 1, "Don't panic", 1395874800000L, 2, "Now panic", 1395961200000L });
    assertThat(response.rowCount(), is(2L));
    ensureGreen();
    refresh();

    execute("alter table quotes partition (date=1395874800000) set (number_of_replicas=1)");
    ensureGreen();
    List<String> partitions = ImmutableList.of(
            new PartitionName("quotes", Arrays.asList(new BytesRef("1395874800000"))).stringValue(),
            new PartitionName("quotes", Arrays.asList(new BytesRef("1395961200000"))).stringValue());

    GetSettingsResponse settingsResponse = client().admin().indices()
            .prepareGetSettings(partitions.get(0), partitions.get(1)).execute().get();
    assertThat(settingsResponse.getSetting(partitions.get(0), IndexMetaData.SETTING_NUMBER_OF_REPLICAS),
            is("1"));
    assertThat(settingsResponse.getSetting(partitions.get(1), IndexMetaData.SETTING_NUMBER_OF_REPLICAS),
            is("0"));

    String templateName = PartitionName.templateName("quotes");
    GetIndexTemplatesResponse templatesResponse = client().admin().indices().prepareGetTemplates(templateName)
            .execute().actionGet();
    Settings templateSettings = templatesResponse.getIndexTemplates().get(0).getSettings();
    assertThat(templateSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0), is(0));
    assertThat(templateSettings.get(IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS), is("false"));

}

From source file:io.crate.lucene.LuceneQueryBuilderTest.java

License:Apache License

@Test
public void testAnyOnArrayLiteral() throws Exception {
    Reference ref = createReference("d", DataTypes.STRING);
    Literal stringArrayLiteral = Literal.newLiteral(
            new Object[] { new BytesRef("a"), new BytesRef("b"), new BytesRef("c") },
            new ArrayType(DataTypes.STRING));

    Query neqQuery = convert("name != any (['a', 'b', 'c'])");
    assertThat(neqQuery, instanceOf(FilteredQuery.class));
    assertThat(((FilteredQuery) neqQuery).getFilter(), instanceOf(BooleanFilter.class));
    BooleanFilter filter = (BooleanFilter) ((FilteredQuery) neqQuery).getFilter();
    assertThat(filter.toString(), is("BooleanFilter(-BooleanFilter(+name:a +name:b +name:c))"));

    Query likeQuery = convert("name like any (['a', 'b', 'c'])");
    assertThat(likeQuery, instanceOf(BooleanQuery.class));
    BooleanQuery likeBQuery = (BooleanQuery) likeQuery;
    assertThat(likeBQuery.clauses().size(), is(3));
    for (int i = 0; i < 2; i++) {
        // like --> XConstantScoreQuery with regexp-filter
        Query filteredQuery = likeBQuery.clauses().get(i).getQuery();
        assertThat(filteredQuery, instanceOf(WildcardQuery.class));
    }/*from w  ww  .  jav a2 s .c  o m*/

    Query notLikeQuery = convert("name not like any (['a', 'b', 'c'])");
    assertThat(notLikeQuery, instanceOf(BooleanQuery.class));
    BooleanQuery notLikeBQuery = (BooleanQuery) notLikeQuery;
    assertThat(notLikeBQuery.clauses(), hasSize(1));
    BooleanClause clause = notLikeBQuery.clauses().get(0);
    assertThat(clause.getOccur(), is(BooleanClause.Occur.MUST_NOT));
    assertThat(((BooleanQuery) clause.getQuery()).clauses(), hasSize(3));
    for (BooleanClause innerClause : ((BooleanQuery) clause.getQuery()).clauses()) {
        assertThat(innerClause.getOccur(), is(BooleanClause.Occur.MUST));
        assertThat(innerClause.getQuery(), instanceOf(WildcardQuery.class));
    }

    Query ltQuery2 = convert("name < any (['a', 'b', 'c'])");
    assertThat(ltQuery2, instanceOf(BooleanQuery.class));
    BooleanQuery ltBQuery = (BooleanQuery) ltQuery2;
    assertThat(ltBQuery.toString(), is("(name:{* TO a} name:{* TO b} name:{* TO c})~1"));
}

From source file:io.crate.lucene.match.CrateRegexTermsEnum.java

License:Apache License

CrateRegexTermsEnum(TermsEnum tenum, Term term, int flags) {
    super(tenum);
    String text = term.text();/*from   w w w.j a  va 2  s .  c  om*/
    this.regexImpl = CrateRegexCapabilities.compile(text, flags);

    setInitialSeekTerm(new BytesRef(""));
}

From source file:io.crate.lucene.match.MatchQueryBuilderTest.java

License:Apache License

@Test
public void testUnknownMatchType() throws Exception {
    expectedException.expect(IllegalArgumentException.class);
    expectedException.expectMessage(//  www  .  j a  va  2 s. c o m
            "Unknown matchType \"foo\". Possible matchTypes are: best_fields, most_fields, cross_fields, phrase, phrase_prefix");
    new MatchQueryBuilder(null, cache, new BytesRef("foo"), Collections.emptyMap());
}

From source file:io.crate.lucene.match.MatchQueryBuilderTest.java

License:Apache License

@Test
public void testSimpleSingleMatchSingleTerm() throws Exception {
    Map<String, Object> fields = MapBuilder.<String, Object>newMapBuilder().put("col1", null).map();
    MatchQueryBuilder builder = new MatchQueryBuilder(mockMapperService(), cache, null, Collections.emptyMap());
    Query query = builder.query(fields, new BytesRef("foo"));
    assertThat(query, instanceOf(TermQuery.class));
}

From source file:io.crate.lucene.match.MatchQueryBuilderTest.java

License:Apache License

@Test
public void testSimpleSingleMatchTwoTerms() throws Exception {
    Map<String, Object> fields = MapBuilder.<String, Object>newMapBuilder().put("col1", null).map();
    MatchQueryBuilder builder = new MatchQueryBuilder(mockMapperService(), cache, null, Collections.emptyMap());
    Query query = builder.query(fields, new BytesRef("foo bar"));
    assertThat(query, instanceOf(BooleanQuery.class));
}

From source file:io.crate.lucene.match.MatchQueryBuilderTest.java

License:Apache License

@Test
public void testSingleFieldWithCutFrequency() throws Exception {
    MatchQueryBuilder builder = new MatchQueryBuilder(mockMapperService(), cache, null,
            newMapBuilder().put("cutoff_frequency", 3).map());

    Map<String, Object> fields = MapBuilder.<String, Object>newMapBuilder().put("col1", null).map();
    Query query = builder.query(fields, new BytesRef("foo bar"));
    assertThat(query, instanceOf(ExtendedCommonTermsQuery.class));
}