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

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

Introduction

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

Prototype

public TermInSetQuery(String field, BytesRef... terms) 

Source Link

Document

Creates a new TermInSetQuery from the given array of terms.

Usage

From source file:com.b2international.index.lucene.BooleanIndexField.java

License:Apache License

@Override
protected Query toSetQuery(Iterable<Boolean> values) {
    final SortedSet<BytesRef> uniqueBytesRefs = newTreeSet();
    for (Boolean value : values) {
        uniqueBytesRefs.add(toBytesRef(value));
    }//  ww w . j a  v a 2 s  .  c o m
    return new TermInSetQuery(fieldName(), uniqueBytesRefs);
}

From source file:com.b2international.index.lucene.StringIndexFieldBase.java

License:Apache License

@Override
protected Query toSetQuery(Iterable<String> values) {
    final SortedSet<BytesRef> uniqueBytesRefs = newTreeSet();
    for (String value : values) {
        uniqueBytesRefs.add(toBytesRef(value));
    }/*  www . j a  va 2s.  co  m*/
    return new TermInSetQuery(fieldName(), uniqueBytesRefs);
}

From source file:org.elasticsearch.index.mapper.CollationFieldTypeTests.java

License:Apache License

public void testTermsQuery() {
    MappedFieldType ft = createDefaultFieldType();
    ft.setName("field");
    ft.setIndexOptions(IndexOptions.DOCS);

    Collator collator = Collator.getInstance(ULocale.ROOT).freeze();
    ((CollationFieldType) ft).setCollator(collator);

    RawCollationKey fooKey = collator.getRawCollationKey("foo", null);
    RawCollationKey barKey = collator.getRawCollationKey("bar", null);

    List<BytesRef> terms = new ArrayList<>();
    terms.add(new BytesRef(fooKey.bytes, 0, fooKey.size));
    terms.add(new BytesRef(barKey.bytes, 0, barKey.size));

    assertEquals(new TermInSetQuery("field", terms), ft.termsQuery(Arrays.asList("foo", "bar"), null));

    ft.setIndexOptions(IndexOptions.NONE);
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
            () -> ft.termsQuery(Arrays.asList("foo", "bar"), null));
    assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage());
}

From source file:org.elasticsearch.index.mapper.IdFieldTypeTests.java

License:Apache License

public void testTermsQueryWhenTypesAreDisabled() throws Exception {
    QueryShardContext context = Mockito.mock(QueryShardContext.class);
    Settings indexSettings = Settings.builder().put(IndexSettings.INDEX_MAPPING_SINGLE_TYPE_SETTING_KEY, true)
            .put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_5_6_0)
            .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
            .put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).build();
    IndexMetaData indexMetaData = IndexMetaData.builder(IndexMetaData.INDEX_UUID_NA_VALUE)
            .settings(indexSettings).build();
    IndexSettings mockSettings = new IndexSettings(indexMetaData, Settings.EMPTY);
    Mockito.when(context.getIndexSettings()).thenReturn(mockSettings);
    Mockito.when(context.indexVersionCreated())
            .thenReturn(indexSettings.getAsVersion(IndexMetaData.SETTING_VERSION_CREATED, null));

    MapperService mapperService = Mockito.mock(MapperService.class);
    Collection<String> types = Collections.singleton("type");
    Mockito.when(context.queryTypes()).thenReturn(types);
    Mockito.when(context.getMapperService()).thenReturn(mapperService);
    MappedFieldType ft = IdFieldMapper.defaultFieldType(mockSettings);
    Query query = ft.termQuery("id", context);
    assertEquals(new TermInSetQuery("_id", new BytesRef("id")), query);
}

From source file:org.elasticsearch.index.mapper.IdFieldTypeTests.java

License:Apache License

public void testTermsQuery() throws Exception {
    QueryShardContext context = Mockito.mock(QueryShardContext.class);
    Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
            .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
            .put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).build();
    IndexMetaData indexMetaData = IndexMetaData.builder(IndexMetaData.INDEX_UUID_NA_VALUE)
            .settings(indexSettings).build();
    IndexSettings mockSettings = new IndexSettings(indexMetaData, Settings.EMPTY);
    Mockito.when(context.getIndexSettings()).thenReturn(mockSettings);
    Mockito.when(context.indexVersionCreated())
            .thenReturn(indexSettings.getAsVersion(IndexMetaData.SETTING_VERSION_CREATED, null));

    MapperService mapperService = Mockito.mock(MapperService.class);
    Collection<String> types = Collections.emptySet();
    Mockito.when(context.queryTypes()).thenReturn(types);
    Mockito.when(context.getMapperService()).thenReturn(mapperService);

    MappedFieldType ft = IdFieldMapper.defaultFieldType(mockSettings);
    ft.setName(IdFieldMapper.NAME);/*from w ww.j  a  va 2s.co  m*/
    Query query = ft.termQuery("id", context);
    assertEquals(new TermInSetQuery("_id", Uid.encodeId("id")), query);

    types = Collections.singleton("type");
    Mockito.when(context.queryTypes()).thenReturn(types);
    query = ft.termQuery("id", context);
    assertEquals(new TermInSetQuery("_id", Uid.encodeId("id")), query);
}

From source file:org.elasticsearch.index.mapper.UidFieldTypeTests.java

License:Apache License

public void testTermsQueryWhenTypesAreEnabled() throws Exception {
    QueryShardContext context = Mockito.mock(QueryShardContext.class);
    Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_5_6_0) // to allow for multipel types
            .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
            .put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).build();
    IndexMetaData indexMetaData = IndexMetaData.builder(IndexMetaData.INDEX_UUID_NA_VALUE)
            .settings(indexSettings).build();
    IndexSettings mockSettings = new IndexSettings(indexMetaData, Settings.EMPTY);
    Mockito.when(context.getIndexSettings()).thenReturn(mockSettings);

    MapperService mapperService = Mockito.mock(MapperService.class);
    Collection<String> types = Collections.emptySet();
    Mockito.when(context.queryTypes()).thenReturn(types);
    Mockito.when(context.getMapperService()).thenReturn(mapperService);

    MappedFieldType ft = UidFieldMapper.defaultFieldType(mockSettings);
    ft.setName(UidFieldMapper.NAME);/*from   w  w  w .  ja v  a 2 s.c  o m*/
    Query query = ft.termQuery("type#id", context);
    assertEquals(new TermInSetQuery("_uid", new BytesRef("type#id")), query);
}

From source file:org.elasticsearch.index.mapper.UidFieldTypeTests.java

License:Apache License

public void testTermsQueryWhenTypesAreDisabled() throws Exception {
    QueryShardContext context = Mockito.mock(QueryShardContext.class);
    Settings indexSettings = Settings.builder().put(IndexSettings.INDEX_MAPPING_SINGLE_TYPE_SETTING_KEY, true)
            .put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_5_6_0)
            .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
            .put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).build();
    IndexMetaData indexMetaData = IndexMetaData.builder(IndexMetaData.INDEX_UUID_NA_VALUE)
            .settings(indexSettings).build();
    IndexSettings mockSettings = new IndexSettings(indexMetaData, Settings.EMPTY);
    Mockito.when(context.getIndexSettings()).thenReturn(mockSettings);

    MapperService mapperService = Mockito.mock(MapperService.class);
    Collection<String> types = Collections.emptySet();
    Mockito.when(mapperService.types()).thenReturn(types);
    Mockito.when(context.getMapperService()).thenReturn(mapperService);
    Mockito.when(context.indexVersionCreated())
            .thenReturn(indexSettings.getAsVersion(IndexMetaData.SETTING_VERSION_CREATED, null));

    MappedFieldType ft = UidFieldMapper.defaultFieldType(mockSettings);
    ft.setName(UidFieldMapper.NAME);/*from   w  w w  . j  a  v a 2  s .com*/
    Query query = ft.termQuery("type#id", context);
    assertEquals(new MatchNoDocsQuery(), query);

    types = Collections.singleton("type");
    Mockito.when(mapperService.types()).thenReturn(types);
    query = ft.termQuery("type#id", context);
    assertEquals(new TermInSetQuery("_id", new BytesRef("id")), query);
    query = ft.termQuery("type2#id", context);
    assertEquals(new TermInSetQuery("_id"), query);
}

From source file:org.elasticsearch.index.mapper.UidFieldTypeTests.java

License:Apache License

public void testTermsQuery() throws Exception {
    QueryShardContext context = Mockito.mock(QueryShardContext.class);
    Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
            .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
            .put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).build();
    IndexMetaData indexMetaData = IndexMetaData.builder(IndexMetaData.INDEX_UUID_NA_VALUE)
            .settings(indexSettings).build();
    IndexSettings mockSettings = new IndexSettings(indexMetaData, Settings.EMPTY);
    Mockito.when(context.getIndexSettings()).thenReturn(mockSettings);

    MapperService mapperService = Mockito.mock(MapperService.class);
    Collection<String> types = Collections.emptySet();
    Mockito.when(mapperService.types()).thenReturn(types);
    Mockito.when(context.getMapperService()).thenReturn(mapperService);
    Mockito.when(context.indexVersionCreated())
            .thenReturn(indexSettings.getAsVersion(IndexMetaData.SETTING_VERSION_CREATED, null));

    MappedFieldType ft = UidFieldMapper.defaultFieldType(mockSettings);
    ft.setName(UidFieldMapper.NAME);//from ww w . j a  va 2s.  c  o  m
    Query query = ft.termQuery("type#id", context);
    assertEquals(new MatchNoDocsQuery(), query);

    types = Collections.singleton("type");
    Mockito.when(mapperService.types()).thenReturn(types);
    query = ft.termQuery("type#id", context);
    assertEquals(new TermInSetQuery("_id", Uid.encodeId("id")), query);
    query = ft.termQuery("type2#id", context);
    assertEquals(new TermInSetQuery("_id"), query);
}

From source file:org.elasticsearch.join.aggregations.ParentToChildrenAggregatorTests.java

License:Apache License

public void testParentChild() throws IOException {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);

    final Map<String, Tuple<Integer, Integer>> expectedParentChildRelations = setupIndex(indexWriter);
    indexWriter.close();//from  w ww. j  av  a2 s .  c  o  m

    IndexReader indexReader = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(directory),
            new ShardId(new Index("foo", "_na_"), 1));
    // TODO set "maybeWrap" to true for IndexSearcher once #23338 is resolved
    IndexSearcher indexSearcher = newSearcher(indexReader, false, true);

    testCase(new MatchAllDocsQuery(), indexSearcher, child -> {
        int expectedTotalChildren = 0;
        int expectedMinValue = Integer.MAX_VALUE;
        for (Tuple<Integer, Integer> expectedValues : expectedParentChildRelations.values()) {
            expectedTotalChildren += expectedValues.v1();
            expectedMinValue = Math.min(expectedMinValue, expectedValues.v2());
        }
        assertEquals(expectedTotalChildren, child.getDocCount());
        assertEquals(expectedMinValue, ((InternalMin) child.getAggregations().get("in_child")).getValue(),
                Double.MIN_VALUE);
    });

    for (String parent : expectedParentChildRelations.keySet()) {
        testCase(new TermInSetQuery(UidFieldMapper.NAME, new BytesRef(Uid.createUid(PARENT_TYPE, parent))),
                indexSearcher, child -> {
                    assertEquals((long) expectedParentChildRelations.get(parent).v1(), child.getDocCount());
                    assertEquals(expectedParentChildRelations.get(parent).v2(),
                            ((InternalMin) child.getAggregations().get("in_child")).getValue(),
                            Double.MIN_VALUE);
                });
    }
    indexReader.close();
    directory.close();
}

From source file:org.silverpeas.core.index.search.model.IndexSearcher.java

License:Open Source License

private TermInSetQuery getScopeQuery(QueryDescription query) {
    List<BytesRef> terms = new ArrayList<>();
    for (String scope : query.getWhereToSearch()) {
        terms.add(new BytesRef(scope));
    }/* w  w  w  .  j av  a 2  s .c o m*/
    return new TermInSetQuery(IndexManager.SCOPE, terms);
}