Skip to content

Commit

Permalink
optimize term in set queries where for string fields when they are so…
Browse files Browse the repository at this point in the history
…rtable (#106)
  • Loading branch information
mdavis95 authored May 26, 2023
1 parent 06a635a commit c2828db
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
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();
}

}

0 comments on commit c2828db

Please sign in to comment.