Search in sources :

Example 1 with BinaryFieldMapper

use of org.opensearch.index.mapper.BinaryFieldMapper in project OpenSearch by opensearch-project.

the class QueryBuilderStoreTests method testStoringQueryBuilders.

public void testStoringQueryBuilders() throws IOException {
    try (Directory directory = newDirectory()) {
        TermQueryBuilder[] queryBuilders = new TermQueryBuilder[randomIntBetween(1, 16)];
        IndexWriterConfig config = new IndexWriterConfig(new WhitespaceAnalyzer());
        config.setMergePolicy(NoMergePolicy.INSTANCE);
        Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build();
        BinaryFieldMapper fieldMapper = PercolatorFieldMapper.Builder.createQueryBuilderFieldBuilder(new Mapper.BuilderContext(settings, new ContentPath(0)));
        Version version = Version.CURRENT;
        try (IndexWriter indexWriter = new IndexWriter(directory, config)) {
            for (int i = 0; i < queryBuilders.length; i++) {
                queryBuilders[i] = new TermQueryBuilder(randomAlphaOfLength(4), randomAlphaOfLength(8));
                ParseContext parseContext = mock(ParseContext.class);
                ParseContext.Document document = new ParseContext.Document();
                when(parseContext.doc()).thenReturn(document);
                PercolatorFieldMapper.createQueryBuilderField(version, fieldMapper, queryBuilders[i], parseContext);
                indexWriter.addDocument(document);
            }
        }
        QueryShardContext queryShardContext = mock(QueryShardContext.class);
        when(queryShardContext.indexVersionCreated()).thenReturn(version);
        when(queryShardContext.getWriteableRegistry()).thenReturn(writableRegistry());
        when(queryShardContext.getXContentRegistry()).thenReturn(xContentRegistry());
        when(queryShardContext.getForField(fieldMapper.fieldType())).thenReturn(new BytesBinaryIndexFieldData(fieldMapper.name(), CoreValuesSourceType.BYTES));
        when(queryShardContext.fieldMapper(Mockito.anyString())).thenAnswer(invocation -> {
            final String fieldName = (String) invocation.getArguments()[0];
            return new KeywordFieldMapper.KeywordFieldType(fieldName);
        });
        PercolateQuery.QueryStore queryStore = PercolateQueryBuilder.createStore(fieldMapper.fieldType(), queryShardContext);
        try (IndexReader indexReader = DirectoryReader.open(directory)) {
            LeafReaderContext leafContext = indexReader.leaves().get(0);
            CheckedFunction<Integer, Query, IOException> queries = queryStore.getQueries(leafContext);
            assertEquals(queryBuilders.length, leafContext.reader().numDocs());
            for (int i = 0; i < queryBuilders.length; i++) {
                TermQuery query = (TermQuery) queries.apply(i);
                assertEquals(queryBuilders[i].fieldName(), query.getTerm().field());
                assertEquals(queryBuilders[i].value(), query.getTerm().text());
            }
        }
    }
}
Also used : BytesBinaryIndexFieldData(org.opensearch.index.fielddata.plain.BytesBinaryIndexFieldData) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) BinaryFieldMapper(org.opensearch.index.mapper.BinaryFieldMapper) KeywordFieldMapper(org.opensearch.index.mapper.KeywordFieldMapper) Mapper(org.opensearch.index.mapper.Mapper) Version(org.opensearch.Version) QueryShardContext(org.opensearch.index.query.QueryShardContext) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Settings(org.opensearch.common.settings.Settings) Directory(org.apache.lucene.store.Directory) WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) TermQuery(org.apache.lucene.search.TermQuery) ContentPath(org.opensearch.index.mapper.ContentPath) IOException(java.io.IOException) IndexWriter(org.apache.lucene.index.IndexWriter) ParseContext(org.opensearch.index.mapper.ParseContext) IndexReader(org.apache.lucene.index.IndexReader) BinaryFieldMapper(org.opensearch.index.mapper.BinaryFieldMapper) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

IOException (java.io.IOException)1 WhitespaceAnalyzer (org.apache.lucene.analysis.core.WhitespaceAnalyzer)1 IndexReader (org.apache.lucene.index.IndexReader)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 Query (org.apache.lucene.search.Query)1 TermQuery (org.apache.lucene.search.TermQuery)1 Directory (org.apache.lucene.store.Directory)1 Version (org.opensearch.Version)1 Settings (org.opensearch.common.settings.Settings)1 BytesBinaryIndexFieldData (org.opensearch.index.fielddata.plain.BytesBinaryIndexFieldData)1 BinaryFieldMapper (org.opensearch.index.mapper.BinaryFieldMapper)1 ContentPath (org.opensearch.index.mapper.ContentPath)1 KeywordFieldMapper (org.opensearch.index.mapper.KeywordFieldMapper)1 Mapper (org.opensearch.index.mapper.Mapper)1 ParseContext (org.opensearch.index.mapper.ParseContext)1 QueryShardContext (org.opensearch.index.query.QueryShardContext)1 TermQueryBuilder (org.opensearch.index.query.TermQueryBuilder)1