Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize Taxo Writer for Primary to support flat facets #198

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -478,38 +478,7 @@ public synchronized void start() throws Exception {
openMode = IndexWriterConfig.OpenMode.APPEND;
}

Path taxoDirFile;
if (rootDir == null) {
taxoDirFile = null;
} else {
taxoDirFile = rootDir.resolve("taxonomy");
}
taxoDir = indexState.df.open(taxoDirFile);

taxoSnapshots =
new PersistentSnapshotDeletionPolicy(
new KeepOnlyLastCommitDeletionPolicy(),
taxoDir,
IndexWriterConfig.OpenMode.CREATE_OR_APPEND);

taxoWriter =
new DirectoryTaxonomyWriter(taxoDir, openMode) {
@Override
protected IndexWriterConfig createIndexWriterConfig(
IndexWriterConfig.OpenMode openMode) {
IndexWriterConfig iwc = super.createIndexWriterConfig(openMode);
iwc.setIndexDeletionPolicy(taxoSnapshots);
return iwc;
}

@Override
protected IndexWriter openIndexWriter(Directory dir, IndexWriterConfig iwc)
throws IOException {
IndexWriter w = super.openIndexWriter(dir, iwc);
taxoInternalWriter = w;
return w;
}
};
initalizeTaxoWriter(openMode);

writer =
new IndexWriter(
Expand Down Expand Up @@ -625,6 +594,7 @@ public synchronized void startPrimary(long primaryGen, Path dataPath) throws Exc
if (doCreate) {
// nocommit shouldn't we set doCreate=false after we've done the create?
openMode = IndexWriterConfig.OpenMode.CREATE;
initalizeTaxoWriter(openMode);
} else {
openMode = IndexWriterConfig.OpenMode.APPEND;
}
Expand Down Expand Up @@ -704,6 +674,40 @@ public IndexSearcher newSearcher(IndexReader r, IndexReader previousReader)
}
}

private void initalizeTaxoWriter(IndexWriterConfig.OpenMode openMode) throws IOException {
Path taxoDirFile;
if (rootDir == null) {
taxoDirFile = null;
} else {
taxoDirFile = rootDir.resolve("taxonomy");
}
taxoDir = indexState.df.open(taxoDirFile);

taxoSnapshots =
new PersistentSnapshotDeletionPolicy(
new KeepOnlyLastCommitDeletionPolicy(),
taxoDir,
IndexWriterConfig.OpenMode.CREATE_OR_APPEND);

taxoWriter =
new DirectoryTaxonomyWriter(taxoDir, openMode) {
@Override
protected IndexWriterConfig createIndexWriterConfig(IndexWriterConfig.OpenMode openMode) {
IndexWriterConfig iwc = super.createIndexWriterConfig(openMode);
iwc.setIndexDeletionPolicy(taxoSnapshots);
return iwc;
}

@Override
protected IndexWriter openIndexWriter(Directory dir, IndexWriterConfig iwc)
throws IOException {
IndexWriter w = super.openIndexWriter(dir, iwc);
taxoInternalWriter = w;
return w;
}
};
}

private IndexReader.ClosedListener removeSSDVStates =
cacheKey -> {
synchronized (ssdvStates) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
*/
public class ServerTestCase {
public static final String DEFAULT_TEST_INDEX = "test_index";

/**
* This rule manages automatic graceful shutdown for the registered servers and channels at the
* end of test.
Expand Down Expand Up @@ -226,6 +227,9 @@ private void initIndices() throws Exception {
// start the index
StartIndexRequest.Builder startIndexBuilder =
StartIndexRequest.newBuilder().setIndexName(indexName);
if (shouldInitializeIndicesAsPrimary()) {
startIndexBuilder.setMode(Mode.PRIMARY);
}
blockingStub.startIndex(startIndexBuilder.build());

// add Docs
Expand All @@ -236,6 +240,10 @@ private void initIndices() throws Exception {
}
}

protected boolean shouldInitializeIndicesAsPrimary() {
return false;
}

protected List<String> getIndices() {
return Collections.singletonList(DEFAULT_TEST_INDEX);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2020 Yelp Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.yelp.nrtsearch.server.luceneserver.facet;

import static org.junit.Assert.assertNotNull;

import com.yelp.nrtsearch.server.grpc.AddDocumentRequest;
import com.yelp.nrtsearch.server.grpc.FieldDefRequest;
import com.yelp.nrtsearch.server.luceneserver.ServerTestCase;
import io.grpc.testing.GrpcCleanupRule;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.ClassRule;
import org.junit.Test;

public class NumberFieldFlatFacetsTest extends ServerTestCase {
@ClassRule public static final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();

private static final List<String> fields =
Arrays.asList(
new String[] {
"int_number_facet_field",
"float_number_facet_field",
"long_number_facet_field",
"double_number_facet_field"
});
private static final List<String> numericValues =
Arrays.asList(new String[] {"1", "10", "20", "30"});

private Map<String, AddDocumentRequest.MultiValuedField> getFieldsMapForOneDocument(
String value) {
Map<String, AddDocumentRequest.MultiValuedField> fieldsMap = new HashMap<>();
for (String field : fields) {
fieldsMap.put(
field, AddDocumentRequest.MultiValuedField.newBuilder().addValue(value).build());
}
return fieldsMap;
}

@Override
public FieldDefRequest getIndexDef(String name) throws IOException {
return getFieldsFromResourceFile("/facet/number_field_flat_facets.json");
}

@Override
protected boolean shouldInitializeIndicesAsPrimary() {
return true;
}

@Test
public void addDocuments() throws InterruptedException {
List<AddDocumentRequest> documentRequests = new ArrayList<>();
for (String value : numericValues) {
documentRequests.add(
AddDocumentRequest.newBuilder()
.setIndexName(DEFAULT_TEST_INDEX)
.putAllFields(getFieldsMapForOneDocument(value))
.build());
}
assertNotNull(addDocuments(documentRequests.stream()));
}
}
37 changes: 37 additions & 0 deletions src/test/resources/facet/number_field_flat_facets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"indexName": "test_index",
"field": [
{
"name": "int_number_facet_field",
"type": "INT",
"storeDocValues": true,
"multiValued": false,
"search": true,
"facet": "FLAT"
},
{
"name": "float_number_facet_field",
"type": "FLOAT",
"storeDocValues": true,
"multiValued": false,
"search": true,
"facet": "FLAT"
},
{
"name": "long_number_facet_field",
"type": "LONG",
"storeDocValues": true,
"multiValued": false,
"search": true,
"facet": "FLAT"
},
{
"name": "double_number_facet_field",
"type": "DOUBLE",
"storeDocValues": true,
"multiValued": false,
"search": true,
"facet": "FLAT"
}
]
}