Search in sources :

Example 1 with IndexState

use of com.yelp.nrtsearch.server.luceneserver.IndexState in project nrtsearch by Yelp.

the class SearchRequestProcessor method getVirtualFields.

/**
 * Parses any virtualFields, which define dynamic (expression) fields for this one request.
 *
 * @throws IllegalArgumentException if there are multiple virtual fields with the same name
 */
private static Map<String, FieldDef> getVirtualFields(ShardState shardState, SearchRequest searchRequest) {
    if (searchRequest.getVirtualFieldsList().isEmpty()) {
        return new HashMap<>();
    }
    IndexState indexState = shardState.indexState;
    Map<String, FieldDef> virtualFields = new HashMap<>();
    for (VirtualField vf : searchRequest.getVirtualFieldsList()) {
        if (virtualFields.containsKey(vf.getName())) {
            throw new IllegalArgumentException("Multiple definitions of Virtual field: " + vf.getName());
        }
        ScoreScript.Factory factory = ScriptService.getInstance().compile(vf.getScript(), ScoreScript.CONTEXT);
        Map<String, Object> params = ScriptParamsUtils.decodeParams(vf.getScript().getParamsMap());
        FieldDef virtualField = new VirtualFieldDef(vf.getName(), factory.newFactory(params, indexState.docLookup));
        virtualFields.put(vf.getName(), virtualField);
    }
    return virtualFields;
}
Also used : VirtualFieldDef(com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef) IndexableFieldDef(com.yelp.nrtsearch.server.luceneserver.field.IndexableFieldDef) FieldDef(com.yelp.nrtsearch.server.luceneserver.field.FieldDef) VirtualField(com.yelp.nrtsearch.server.grpc.VirtualField) ScoreScript(com.yelp.nrtsearch.server.luceneserver.script.ScoreScript) HashMap(java.util.HashMap) IndexState(com.yelp.nrtsearch.server.luceneserver.IndexState) VirtualFieldDef(com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef)

Example 2 with IndexState

use of com.yelp.nrtsearch.server.luceneserver.IndexState in project nrtsearch by Yelp.

the class BackendGlobalState method getIndex.

@Override
public IndexState getIndex(String name, boolean hasRestore) throws IOException {
    IndexState state = immutableState.indexStateMap.get(name);
    if (state != null) {
        return state;
    }
    synchronized (this) {
        state = immutableState.indexStateMap.get(name);
        if (state == null) {
            if (!immutableState.persistentGlobalState.getIndices().containsKey(name)) {
                throw new IllegalArgumentException("index \"" + name + "\" was not saved or committed");
            }
            Path rootPath = getIndexDir(name);
            state = new IndexState(this, name, rootPath, false, hasRestore);
            // nocommit we need to also persist which shards are here?
            state.addShard(0, false);
            Map<String, IndexState> updatedIndexStateMap = new HashMap<>(immutableState.indexStateMap);
            updatedIndexStateMap.put(name, state);
            immutableState = new ImmutableState(immutableState.persistentGlobalState, updatedIndexStateMap);
        }
        return state;
    }
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) IndexState(com.yelp.nrtsearch.server.luceneserver.IndexState)

Example 3 with IndexState

use of com.yelp.nrtsearch.server.luceneserver.IndexState in project nrtsearch by Yelp.

the class LegacyGlobalState method getIndex.

@Override
public IndexState getIndex(String name, boolean hasRestore) throws IOException {
    synchronized (indices) {
        IndexState state = indices.get(name);
        if (state == null) {
            String rootPath = null;
            JsonElement indexJsonName = indexNames.get(name);
            if (indexJsonName == null) {
                throw new IllegalArgumentException("index " + name + " was not saved or commited");
            }
            String indexName = indexJsonName.getAsString();
            if (indexName != null) {
                rootPath = getIndexDir(name).toString();
            }
            if (rootPath != null) {
                if (rootPath.equals(NULL)) {
                    state = new IndexState(this, name, null, false, hasRestore);
                } else {
                    state = new IndexState(this, name, Paths.get(rootPath), false, hasRestore);
                }
                // nocommit we need to also persist which shards are here?
                state.addShard(0, false);
                indices.put(name, state);
            } else {
                throw new IllegalArgumentException("index \"" + name + "\" was not yet created");
            }
        }
        return state;
    }
}
Also used : JsonElement(com.google.gson.JsonElement) IndexState(com.yelp.nrtsearch.server.luceneserver.IndexState)

Example 4 with IndexState

use of com.yelp.nrtsearch.server.luceneserver.IndexState in project nrtsearch by Yelp.

the class TimeoutTest method queryWithFunction.

private TopDocs queryWithFunction(SearchRequest request, Function<SearchContext, TopDocs> func) throws Exception {
    SearcherTaxonomyManager.SearcherAndTaxonomy s = null;
    IndexState indexState = getGlobalState().getIndex(TEST_INDEX);
    ShardState shardState = indexState.getShard(0);
    try {
        s = shardState.acquire();
        SearchContext context = SearchRequestProcessor.buildContextForRequest(request, indexState, shardState, s, ProfileResult.newBuilder());
        return func.apply(context);
    } finally {
        if (s != null) {
            shardState.release(s);
        }
    }
}
Also used : ShardState(com.yelp.nrtsearch.server.luceneserver.ShardState) SearchContext(com.yelp.nrtsearch.server.luceneserver.search.SearchContext) IndexState(com.yelp.nrtsearch.server.luceneserver.IndexState) SearcherTaxonomyManager(org.apache.lucene.facet.taxonomy.SearcherTaxonomyManager)

Example 5 with IndexState

use of com.yelp.nrtsearch.server.luceneserver.IndexState in project nrtsearch by Yelp.

the class BackendGlobalStateTest method testGetCreatedIndex.

@Test
public void testGetCreatedIndex() throws IOException {
    StateBackend mockBackend = mock(StateBackend.class);
    PersistentGlobalState initialState = new PersistentGlobalState();
    when(mockBackend.loadOrCreateGlobalState()).thenReturn(initialState);
    MockBackendGlobalState.stateBackend = mockBackend;
    BackendGlobalState backendGlobalState = new MockBackendGlobalState(getConfig(), null);
    backendGlobalState.createIndex("test_index");
    assertEquals(1, backendGlobalState.getIndexNames().size());
    assertTrue(backendGlobalState.getIndexNames().contains("test_index"));
    IndexState createdState = backendGlobalState.getIndex("test_index");
    assertNotNull(createdState);
    IndexState getIndexState = backendGlobalState.getIndex("test_index");
    assertSame(createdState, getIndexState);
    verify(mockBackend, times(1)).loadOrCreateGlobalState();
    ArgumentCaptor<PersistentGlobalState> stateArgumentCaptor = ArgumentCaptor.forClass(PersistentGlobalState.class);
    verify(mockBackend, times(1)).commitGlobalState(stateArgumentCaptor.capture());
    PersistentGlobalState committedState = stateArgumentCaptor.getValue();
    assertEquals(1, committedState.getIndices().size());
    assertNotNull(committedState.getIndices().get("test_index"));
    verifyNoMoreInteractions(mockBackend);
}
Also used : StateBackend(com.yelp.nrtsearch.server.luceneserver.state.backend.StateBackend) RemoteStateBackend(com.yelp.nrtsearch.server.luceneserver.state.backend.RemoteStateBackend) LocalStateBackend(com.yelp.nrtsearch.server.luceneserver.state.backend.LocalStateBackend) IndexState(com.yelp.nrtsearch.server.luceneserver.IndexState) Test(org.junit.Test)

Aggregations

IndexState (com.yelp.nrtsearch.server.luceneserver.IndexState)23 Test (org.junit.Test)13 SearchRequest (com.yelp.nrtsearch.server.grpc.SearchRequest)9 Path (java.nio.file.Path)6 HashMap (java.util.HashMap)5 SearchCutoffWrapper (com.yelp.nrtsearch.server.luceneserver.search.SearchCutoffWrapper)4 SearchHandler (com.yelp.nrtsearch.server.luceneserver.SearchHandler)3 FieldDef (com.yelp.nrtsearch.server.luceneserver.field.FieldDef)3 IndexableFieldDef (com.yelp.nrtsearch.server.luceneserver.field.IndexableFieldDef)3 BufferedWriter (java.io.BufferedWriter)3 ArrayList (java.util.ArrayList)3 Facet (com.yelp.nrtsearch.server.grpc.Facet)2 SearchResponse (com.yelp.nrtsearch.server.grpc.SearchResponse)2 VirtualFieldDef (com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef)2 TerminateAfterWrapper (com.yelp.nrtsearch.server.luceneserver.search.TerminateAfterWrapper)2 LocalStateBackend (com.yelp.nrtsearch.server.luceneserver.state.backend.LocalStateBackend)2 RemoteStateBackend (com.yelp.nrtsearch.server.luceneserver.state.backend.RemoteStateBackend)2 StateBackend (com.yelp.nrtsearch.server.luceneserver.state.backend.StateBackend)2 List (java.util.List)2 Facets (org.apache.lucene.facet.Facets)2