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

add ignore_above support for nrtsearch #735

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions clientlib/src/main/proto/yelp/nrtsearch/luceneserver.proto
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,9 @@ message Field {
VectorElementType vectorElementType = 34;
// Position increment gap for indexing multi valued TEXT fields. Must be >= 0, defaulting to 100 when not set.
optional int32 positionIncrementGap = 35;
// For arrays of strings, ignoreAbove will be applied for each array element separately and string elements longer than ignore_above will not be indexed or stored.
// This option is also useful for protecting against Lucene’s term byte-length limit of 32766
int32 ignoreAbove = 36;
}

// Vector field element type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public abstract class TextBaseFieldDef extends IndexableFieldDef

public final Map<IndexReader.CacheKey, GlobalOrdinalLookup> ordinalLookupCache = new HashMap<>();
private final Object ordinalBuilderLock = new Object();
private final int ignoreAbove;

/**
* Field constructor. Uses {@link IndexableFieldDef#IndexableFieldDef(String, Field)} to do common
Expand All @@ -76,6 +77,7 @@ protected TextBaseFieldDef(String name, Field requestField) {
indexAnalyzer = parseIndexAnalyzer(requestField);
searchAnalyzer = parseSearchAnalyzer(requestField);
eagerFieldGlobalOrdinals = requestField.getEagerFieldGlobalOrdinals();
ignoreAbove = requestField.getIgnoreAbove();
}

@Override
Expand Down Expand Up @@ -232,6 +234,9 @@ public void parseDocumentField(

for (int i = 0; i < fieldValues.size(); i++) {
String fieldStr = fieldValues.get(i);
if (ignoreAbove > 0 && fieldStr.length() > ignoreAbove) {
continue;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this value still need to be retrievable somehow? This would keep it from being stored anywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Goal of ignoreAbove is to avoid the lucene limit 32766. docValue also has this limit for Sorted and SortedSet. It will be only retrievable if it is Binary DocValue or Stored. So I just dropped it for all.

Do you think I should keep for Stored fields only?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that for elasticsearch this setting keeps the value from being indexed or stored. Though for ES the _source would still contain the value.

I assume the client will want the value at some point (?). This could maybe be done in the field itself, though a child field could also be used potentially.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what would be the best approach to do it in the field itself. I'll store the binary doc value if the doc type is binary already(It should work with Orbs case here. All large text field in orbs are single value text).

If it field is declared with Sorted or Sorted Set doc value, I cannot store it as Binary for large text, it will break sorting. Or if I store with the stored field, the fillDocTask will try to read from doc values since the field definition hasDocValues is true. It would be better if we can force retrieve from doc value. or stored fields, then we can retrieve the text from stored values.

I'll add the logic to store binary doc value for the single text field first to make sure orbs can work. For further improvements, I think we can do it in a separate pr since the current master breaks for large text any way.

if (hasDocValues()) {
BytesRef stringBytes = new BytesRef(fieldStr);
if (docValuesType == DocValuesType.BINARY) {
Expand Down
140 changes: 140 additions & 0 deletions src/test/java/com/yelp/nrtsearch/server/grpc/IgnoreAboveTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Copyright 2022 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.grpc;

import static org.junit.Assert.assertEquals;

import com.yelp.nrtsearch.server.config.IndexStartConfig.IndexDataLocationType;
import com.yelp.nrtsearch.server.grpc.AddDocumentRequest.MultiValuedField;
import java.io.IOException;
import java.util.List;
import java.util.stream.Stream;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class IgnoreAboveTest {

@Rule public final TemporaryFolder folder = new TemporaryFolder();

private static final List<Field> fields =
List.of(
Field.newBuilder()
.setName("id")
.setType(FieldType._ID)
.setStoreDocValues(true)
.setSearch(true)
.build(),
Field.newBuilder()
.setName("field1")
.setStoreDocValues(true)
.setSearch(true)
.setMultiValued(true)
.setIgnoreAbove(12)
.setType(FieldType.TEXT)
.build());

@After
public void cleanup() {
TestServer.cleanupAll();
}

private void addInitialDoc(TestServer testServer) {
AddDocumentRequest addDocumentRequest =
AddDocumentRequest.newBuilder()
.setIndexName("test_index")
.putFields("id", MultiValuedField.newBuilder().addValue("1").build())
.putFields("field1", MultiValuedField.newBuilder().addValue("first Vendor").build())
.build();
testServer.addDocs(Stream.of(addDocumentRequest));
}

private void addAdditionalDoc(TestServer testServer) {
AddDocumentRequest addDocumentRequest =
AddDocumentRequest.newBuilder()
.setIndexName("test_index")
.putFields("id", MultiValuedField.newBuilder().addValue("2").build())
.putFields(
"field1",
MultiValuedField.newBuilder()
.addValue("second Vendor")
.addValue("new Vendor")
.build())
.build();
testServer.addDocs(Stream.of(addDocumentRequest));
}

private void verifyDocs(TestServer testServer) {
SearchRequest request =
SearchRequest.newBuilder()
.setIndexName("test_index")
.addRetrieveFields("id")
.setStartHit(0)
.setTopHits(10)
.setQuery(
Query.newBuilder()
.setMatchQuery(
MatchQuery.newBuilder().setField("field1").setQuery("first").build())
.build())
.build();
SearchResponse response = testServer.getClient().getBlockingStub().search(request);
assertEquals(1, response.getHitsCount());
request =
SearchRequest.newBuilder()
.setIndexName("test_index")
.addRetrieveFields("id")
.setStartHit(0)
.setTopHits(10)
.setQuery(
Query.newBuilder()
.setMatchQuery(
MatchQuery.newBuilder().setField("field1").setQuery("second").build())
.build())
.build();
response = testServer.getClient().getBlockingStub().search(request);
assertEquals(0, response.getHitsCount());
request =
SearchRequest.newBuilder()
.setIndexName("test_index")
.addRetrieveFields("id")
.setStartHit(0)
.setTopHits(10)
.setQuery(
Query.newBuilder()
.setMatchQuery(
MatchQuery.newBuilder().setField("field1").setQuery("new").build())
.build())
.build();
response = testServer.getClient().getBlockingStub().search(request);
assertEquals(1, response.getHitsCount());
}

@Test
public void testIgnoreAbove() throws IOException {
TestServer primaryServer =
TestServer.builder(folder)
.withAutoStartConfig(true, Mode.PRIMARY, 0, IndexDataLocationType.LOCAL)
.build();
primaryServer.createIndex("test_index");
primaryServer.registerFields("test_index", fields);
primaryServer.startPrimaryIndex("test_index", -1, null);
addInitialDoc(primaryServer);
addAdditionalDoc(primaryServer);
primaryServer.refresh("test_index");
verifyDocs(primaryServer);
}
}
Loading