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

optimize term in set queries where for string fields when they are sortable #106

Merged
merged 1 commit into from
May 26, 2023
Merged
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
15 changes: 13 additions & 2 deletions zulia-server/src/main/java/io/zulia/server/index/ZuliaIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import org.apache.lucene.search.IndexOrDocValuesQuery;
import org.apache.lucene.search.KnnFloatVectorQuery;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.search.TermQuery;
Expand Down Expand Up @@ -396,21 +397,31 @@ else if (query.getQfList().size() == 1) {
BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder();
booleanQueryBuilder.setMinimumNumberShouldMatch(query.getMm());
for (String field : query.getQfList()) {
TermInSetQuery inSetQuery = getTermInSetQuery(query, field);
Query inSetQuery = getTermInSetQuery(query, field);
booleanQueryBuilder.add(inSetQuery, BooleanClause.Occur.SHOULD);
}
return booleanQueryBuilder.build();
}

}

private TermInSetQuery getTermInSetQuery(ZuliaQuery.Query query, String field) {
private Query getTermInSetQuery(ZuliaQuery.Query query, String field) {

List<BytesRef> termBytesRef = new ArrayList<>();
for (String term : query.getTermList()) {
termBytesRef.add(new BytesRef(term));
}

IndexFieldInfo indexFieldInfo = indexConfig.getIndexFieldInfo(field);

String sortField = indexFieldInfo.getInternalSortFieldName();

if (sortField != null) {
Query indexQuery = new TermInSetQuery(field, termBytesRef);
Query dvQuery = new TermInSetQuery(MultiTermQuery.DOC_VALUES_REWRITE, sortField, termBytesRef);
return new IndexOrDocValuesQuery(indexQuery, dvQuery);
}

return new TermInSetQuery(field, termBytesRef);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void createIndex() throws Exception {
ZuliaWorkPool zuliaWorkPool = nodeExtension.getClient();
ClientIndexConfig indexConfig = new ClientIndexConfig();
indexConfig.addDefaultSearchField("title");
indexConfig.addFieldConfig(FieldConfigBuilder.createString("id").indexAs(DefaultAnalyzers.LC_KEYWORD).sort());
indexConfig.addFieldConfig(FieldConfigBuilder.createString("id").indexAs(DefaultAnalyzers.LC_KEYWORD));
indexConfig.addFieldConfig(FieldConfigBuilder.createString("title").indexAs(DefaultAnalyzers.STANDARD).sort());
indexConfig.addFieldConfig(FieldConfigBuilder.createString("issn").indexAs(DefaultAnalyzers.LC_KEYWORD).facet().sort());
indexConfig.addFieldConfig(FieldConfigBuilder.createString("eissn").indexAs(DefaultAnalyzers.LC_KEYWORD));
Expand Down Expand Up @@ -373,7 +373,7 @@ public void reindex() throws Exception {
ZuliaWorkPool zuliaWorkPool = nodeExtension.getClient();
ClientIndexConfig indexConfig = new ClientIndexConfig();
indexConfig.addDefaultSearchField("title");
indexConfig.addFieldConfig(FieldConfigBuilder.createString("id").indexAs(DefaultAnalyzers.LC_KEYWORD).sort());
indexConfig.addFieldConfig(FieldConfigBuilder.createString("id").indexAs(DefaultAnalyzers.LC_KEYWORD).sort()); // make it sortable this time
indexConfig.addFieldConfig(FieldConfigBuilder.createString("title").indexAs(DefaultAnalyzers.STANDARD).sort());
indexConfig.addFieldConfig(FieldConfigBuilder.createString("issn").indexAs(DefaultAnalyzers.LC_KEYWORD).facet().sort());
indexConfig.addFieldConfig(FieldConfigBuilder.createString("eissn").indexAs(DefaultAnalyzers.LC_KEYWORD).facet());
Expand All @@ -383,6 +383,7 @@ public void reindex() throws Exception {
indexConfig.addFieldConfig(
FieldConfigBuilder.createDate("date").index().facetAs(DateHandling.DATE_YYYY_MM_DD).description("The very special data").sort());
indexConfig.addFieldConfig(FieldConfigBuilder.createString("testList").indexAs(DefaultAnalyzers.STANDARD).facet());
indexConfig.addFieldConfig(FieldConfigBuilder.createBool("testBool").index().facet().sort());
indexConfig.setIndexName(FACET_TEST_INDEX);
indexConfig.setNumberOfShards(1);

Expand Down Expand Up @@ -727,4 +728,11 @@ public void confirmBuilder() throws Exception {

}

@Test
@Order(57)
public void confirmTermInSet() throws Exception {
//try it again with id being sortable
termTestBuilder();
}

}