Search in sources :

Example 1 with SearchLookup

use of org.opensearch.search.lookup.SearchLookup in project OpenSearch by opensearch-project.

the class ExpressionNumberSortScriptTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    NumberFieldType fieldType = new NumberFieldType("field", NumberType.DOUBLE);
    MapperService mapperService = mock(MapperService.class);
    when(mapperService.fieldType("field")).thenReturn(fieldType);
    when(mapperService.fieldType("alias")).thenReturn(fieldType);
    SortedNumericDoubleValues doubleValues = mock(SortedNumericDoubleValues.class);
    when(doubleValues.advanceExact(anyInt())).thenReturn(true);
    when(doubleValues.nextValue()).thenReturn(2.718);
    LeafNumericFieldData atomicFieldData = mock(LeafNumericFieldData.class);
    when(atomicFieldData.getDoubleValues()).thenReturn(doubleValues);
    IndexNumericFieldData fieldData = mock(IndexNumericFieldData.class);
    when(fieldData.getFieldName()).thenReturn("field");
    when(fieldData.load(any())).thenReturn(atomicFieldData);
    service = new ExpressionScriptEngine();
    lookup = new SearchLookup(mapperService, (ignored, lookup) -> fieldData);
}
Also used : NumberSortScript(org.opensearch.script.NumberSortScript) IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) LeafNumericFieldData(org.opensearch.index.fielddata.LeafNumericFieldData) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) SearchLookup(org.opensearch.search.lookup.SearchLookup) NumberFieldType(org.opensearch.index.mapper.NumberFieldMapper.NumberFieldType) ScriptException(org.opensearch.script.ScriptException) MapperService(org.opensearch.index.mapper.MapperService) NumberType(org.opensearch.index.mapper.NumberFieldMapper.NumberType) Mockito.anyInt(org.mockito.Mockito.anyInt) ParseException(java.text.ParseException) Mockito.any(org.mockito.Mockito.any) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) LeafNumericFieldData(org.opensearch.index.fielddata.LeafNumericFieldData) NumberFieldType(org.opensearch.index.mapper.NumberFieldMapper.NumberFieldType) IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) MapperService(org.opensearch.index.mapper.MapperService) SearchLookup(org.opensearch.search.lookup.SearchLookup)

Example 2 with SearchLookup

use of org.opensearch.search.lookup.SearchLookup in project OpenSearch by opensearch-project.

the class ExpressionFieldScriptTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    NumberFieldMapper.NumberFieldType fieldType = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.DOUBLE);
    MapperService mapperService = mock(MapperService.class);
    when(mapperService.fieldType("field")).thenReturn(fieldType);
    when(mapperService.fieldType("alias")).thenReturn(fieldType);
    SortedNumericDoubleValues doubleValues = mock(SortedNumericDoubleValues.class);
    when(doubleValues.advanceExact(anyInt())).thenReturn(true);
    when(doubleValues.nextValue()).thenReturn(2.718);
    LeafNumericFieldData atomicFieldData = mock(LeafNumericFieldData.class);
    when(atomicFieldData.getDoubleValues()).thenReturn(doubleValues);
    IndexNumericFieldData fieldData = mock(IndexNumericFieldData.class);
    when(fieldData.getFieldName()).thenReturn("field");
    when(fieldData.load(any())).thenReturn(atomicFieldData);
    service = new ExpressionScriptEngine();
    lookup = new SearchLookup(mapperService, (ignored, lookup) -> fieldData);
}
Also used : IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) LeafNumericFieldData(org.opensearch.index.fielddata.LeafNumericFieldData) FieldScript(org.opensearch.script.FieldScript) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) SearchLookup(org.opensearch.search.lookup.SearchLookup) ScriptException(org.opensearch.script.ScriptException) MapperService(org.opensearch.index.mapper.MapperService) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Mockito.anyInt(org.mockito.Mockito.anyInt) ParseException(java.text.ParseException) Mockito.any(org.mockito.Mockito.any) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) LeafNumericFieldData(org.opensearch.index.fielddata.LeafNumericFieldData) IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) MapperService(org.opensearch.index.mapper.MapperService) SearchLookup(org.opensearch.search.lookup.SearchLookup)

Example 3 with SearchLookup

use of org.opensearch.search.lookup.SearchLookup in project OpenSearch by opensearch-project.

the class ScriptedMetricAggContextsTests method testReturnSource.

public void testReturnSource() throws IOException {
    ScriptedMetricAggContexts.MapScript.Factory factory = getEngine().compile("test", "state._source = params._source", ScriptedMetricAggContexts.MapScript.CONTEXT, Collections.emptyMap());
    Map<String, Object> params = new HashMap<>();
    Map<String, Object> state = new HashMap<>();
    MemoryIndex index = new MemoryIndex();
    // we don't need a real index, just need to construct a LeafReaderContext which cannot be mocked
    LeafReaderContext leafReaderContext = index.createSearcher().getIndexReader().leaves().get(0);
    SearchLookup lookup = mock(SearchLookup.class);
    LeafSearchLookup leafLookup = mock(LeafSearchLookup.class);
    when(lookup.getLeafSearchLookup(leafReaderContext)).thenReturn(leafLookup);
    SourceLookup sourceLookup = mock(SourceLookup.class);
    when(leafLookup.asMap()).thenReturn(Collections.singletonMap("_source", sourceLookup));
    when(sourceLookup.loadSourceIfNeeded()).thenReturn(Collections.singletonMap("test", 1));
    ScriptedMetricAggContexts.MapScript.LeafFactory leafFactory = factory.newFactory(params, state, lookup);
    ScriptedMetricAggContexts.MapScript script = leafFactory.newInstance(leafReaderContext);
    script.execute();
    assertTrue(state.containsKey("_source"));
    assertTrue(state.get("_source") instanceof Map && ((Map) state.get("_source")).containsKey("test"));
    assertEquals(1, ((Map) state.get("_source")).get("test"));
}
Also used : SourceLookup(org.opensearch.search.lookup.SourceLookup) HashMap(java.util.HashMap) MemoryIndex(org.apache.lucene.index.memory.MemoryIndex) ScriptedMetricAggContexts(org.opensearch.script.ScriptedMetricAggContexts) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) LeafSearchLookup(org.opensearch.search.lookup.LeafSearchLookup) HashMap(java.util.HashMap) Map(java.util.Map) LeafSearchLookup(org.opensearch.search.lookup.LeafSearchLookup) SearchLookup(org.opensearch.search.lookup.SearchLookup)

Example 4 with SearchLookup

use of org.opensearch.search.lookup.SearchLookup in project OpenSearch by opensearch-project.

the class ScriptedMetricAggContextsTests method testMapSourceAccess.

public void testMapSourceAccess() throws IOException {
    ScriptedMetricAggContexts.MapScript.Factory factory = getEngine().compile("test", "state.testField = params._source.three", ScriptedMetricAggContexts.MapScript.CONTEXT, Collections.emptyMap());
    Map<String, Object> params = new HashMap<>();
    Map<String, Object> state = new HashMap<>();
    MemoryIndex index = new MemoryIndex();
    // we don't need a real index, just need to construct a LeafReaderContext which cannot be mocked
    LeafReaderContext leafReaderContext = index.createSearcher().getIndexReader().leaves().get(0);
    SearchLookup lookup = mock(SearchLookup.class);
    LeafSearchLookup leafLookup = mock(LeafSearchLookup.class);
    when(lookup.getLeafSearchLookup(leafReaderContext)).thenReturn(leafLookup);
    SourceLookup sourceLookup = mock(SourceLookup.class);
    when(leafLookup.asMap()).thenReturn(Collections.singletonMap("_source", sourceLookup));
    when(sourceLookup.loadSourceIfNeeded()).thenReturn(Collections.singletonMap("three", 3));
    ScriptedMetricAggContexts.MapScript.LeafFactory leafFactory = factory.newFactory(params, state, lookup);
    ScriptedMetricAggContexts.MapScript script = leafFactory.newInstance(leafReaderContext);
    script.execute();
    assertTrue(state.containsKey("testField"));
    assertEquals(3, state.get("testField"));
}
Also used : SourceLookup(org.opensearch.search.lookup.SourceLookup) HashMap(java.util.HashMap) MemoryIndex(org.apache.lucene.index.memory.MemoryIndex) ScriptedMetricAggContexts(org.opensearch.script.ScriptedMetricAggContexts) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) LeafSearchLookup(org.opensearch.search.lookup.LeafSearchLookup) LeafSearchLookup(org.opensearch.search.lookup.LeafSearchLookup) SearchLookup(org.opensearch.search.lookup.SearchLookup)

Example 5 with SearchLookup

use of org.opensearch.search.lookup.SearchLookup in project OpenSearch by opensearch-project.

the class SliceBuilderTests method createShardContext.

private QueryShardContext createShardContext(Version indexVersionCreated, IndexReader reader, String fieldName, DocValuesType dvType, int numShards, int shardId) {
    MappedFieldType fieldType = new MappedFieldType(fieldName, true, false, dvType != null, TextSearchInfo.NONE, Collections.emptyMap()) {

        @Override
        public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) {
            throw new UnsupportedOperationException();
        }

        @Override
        public String typeName() {
            return null;
        }

        @Override
        public Query termQuery(Object value, @Nullable QueryShardContext context) {
            return null;
        }

        public Query existsQuery(QueryShardContext context) {
            return null;
        }
    };
    QueryShardContext context = mock(QueryShardContext.class);
    when(context.fieldMapper(fieldName)).thenReturn(fieldType);
    when(context.getIndexReader()).thenReturn(reader);
    when(context.getShardId()).thenReturn(shardId);
    IndexSettings indexSettings = createIndexSettings(indexVersionCreated, numShards);
    when(context.getIndexSettings()).thenReturn(indexSettings);
    if (dvType != null) {
        IndexNumericFieldData fd = mock(IndexNumericFieldData.class);
        when(context.getForField(fieldType)).thenReturn(fd);
    }
    return context;
}
Also used : IndexSettings(org.opensearch.index.IndexSettings) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) QueryShardContext(org.opensearch.index.query.QueryShardContext) Matchers.containsString(org.hamcrest.Matchers.containsString) Nullable(org.opensearch.common.Nullable) SearchLookup(org.opensearch.search.lookup.SearchLookup)

Aggregations

SearchLookup (org.opensearch.search.lookup.SearchLookup)14 IOException (java.io.IOException)7 Collections (java.util.Collections)6 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)6 Map (java.util.Map)5 QueryShardContext (org.opensearch.index.query.QueryShardContext)5 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)5 HashMap (java.util.HashMap)4 LeafSearchLookup (org.opensearch.search.lookup.LeafSearchLookup)4 ParseException (java.text.ParseException)3 Supplier (java.util.function.Supplier)3 Mockito.any (org.mockito.Mockito.any)3 Mockito.mock (org.mockito.Mockito.mock)3 Mockito.when (org.mockito.Mockito.when)3 IndexFieldData (org.opensearch.index.fielddata.IndexFieldData)3 IndexNumericFieldData (org.opensearch.index.fielddata.IndexNumericFieldData)3 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)3 Collections.emptyList (java.util.Collections.emptyList)2 MemoryIndex (org.apache.lucene.index.memory.MemoryIndex)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2