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

store update seq of docs #4796

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -17,6 +17,7 @@
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import java.util.Collection;
import java.util.Objects;
import org.apache.couchdb.nouveau.core.ser.PrimitiveWrapper;
Expand All @@ -27,6 +28,9 @@ public class SearchHit {
@NotEmpty
private String id;

@Positive
private long seq;

@NotNull
private PrimitiveWrapper<?>[] order;

Expand All @@ -35,8 +39,10 @@ public class SearchHit {

public SearchHit() {}

public SearchHit(final String id, final PrimitiveWrapper<?>[] order, final Collection<StoredField> fields) {
public SearchHit(
final String id, final long seq, final PrimitiveWrapper<?>[] order, final Collection<StoredField> fields) {
this.id = id;
this.seq = seq;
this.order = Objects.requireNonNull(order);
this.fields = Objects.requireNonNull(fields);
}
Expand All @@ -45,6 +51,10 @@ public String getId() {
return id;
}

public long getSeq() {
return seq;
}

public PrimitiveWrapper<?>[] getOrder() {
return order;
}
Expand All @@ -55,6 +65,6 @@ public Collection<StoredField> getFields() {

@Override
public String toString() {
return "SearchHit [id=" + id + ", order=" + order + ", fields=" + fields + "]";
return "SearchHit [id=" + id + ", seq=" + seq + ", order=" + order + ", fields=" + fields + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ private void collectHits(final IndexSearcher searcher, final TopDocs topDocs, fi
final List<StoredField> fields =
new ArrayList<StoredField>(doc.getFields().size());
for (IndexableField field : doc.getFields()) {
if (field.name().equals("_id")) {
if (field.name().startsWith("_")) {
continue;
}
final StoredValue storedValue = field.storedValue();
Expand All @@ -265,7 +265,8 @@ private void collectHits(final IndexSearcher searcher, final TopDocs topDocs, fi
}

final PrimitiveWrapper<?>[] after = toAfter(((FieldDoc) scoreDoc));
hits.add(new SearchHit(doc.get("_id"), after, fields));
hits.add(new SearchHit(
doc.get("_id"), doc.getField("_seq").numericValue().longValue(), after, fields));
}

searchResults.setTotalHits(topDocs.totalHits.value);
Expand Down Expand Up @@ -382,6 +383,9 @@ private static Document toDocument(final String docId, final DocumentUpdateReque
result.add(new org.apache.lucene.document.StringField("_id", docId, Store.YES));
result.add(new SortedDocValuesField("_id", new BytesRef(docId)));

// seq
result.add(new org.apache.lucene.document.LongField("_seq", request.getSeq(), Store.YES));

// partition (optional)
if (request.hasPartition()) {
result.add(new org.apache.lucene.document.StringField("_partition", request.getPartition(), Store.NO));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -74,15 +75,25 @@ public void testSearching(@TempDir Path path) throws IOException {
final Index index = setup(path);
try {
final int count = 100;
List<String> ids = new ArrayList<String>(count);
for (int i = 1; i <= count; i++) {
final Collection<Field> fields = List.of(new StringField("foo", "bar", false));
final DocumentUpdateRequest request = new DocumentUpdateRequest(i - 1, i, null, fields);
index.update("doc" + i, request);
final String id = "doc" + i;
ids.add(id);
index.update(id, request);
}
ids.sort(String::compareTo);
final SearchRequest request = new SearchRequest();
request.setQuery("*:*");
request.setLimit(count);
final SearchResults results = index.search(request);
assertThat(results.getTotalHits()).isEqualTo(count);
for (int i = 0; i < count; i++) {
var hit = results.getHits().get(i);
assertThat(hit.getId()).isEqualTo(ids.get(i));
assertThat(hit.getSeq()).isEqualTo(Long.parseLong(ids.get(i).substring(3)));
}
} finally {
cleanup(index);
}
Expand Down