Skip to content

Commit

Permalink
build term queries using the mapped field type directly
Browse files Browse the repository at this point in the history
  • Loading branch information
jimczi committed Nov 29, 2023
1 parent 0174a73 commit 5ee088d
Showing 1 changed file with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
package org.elasticsearch.xpack.ml.queries;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.*;
import org.elasticsearch.TransportVersion;
import org.elasticsearch.TransportVersions;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.query.AbstractQueryBuilder;
import org.elasticsearch.index.query.MatchNoneQueryBuilder;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.XContentBuilder;
Expand Down Expand Up @@ -96,6 +94,10 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep

@Override
protected Query doToQuery(SearchExecutionContext context) throws IOException {
final MappedFieldType mapper = context.getFieldType(fieldName);
if (mapper == null) {
return new MatchNoDocsQuery("The \"" + getName() + "\" query is against a field that does not exist");
}
var qb = new BooleanQuery.Builder();
var lowTerms = new BooleanQuery.Builder();
int maxDocs = context.getIndexReader().maxDoc();
Expand All @@ -114,9 +116,9 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException {
float termFreq = (float) context.getIndexReader().docFreq(term) / maxDocs;
if (termFreq > cutoffFrequencyRatio * averageTokenFreq
&& token.weight() < cutoffBestWeight * bestWeight) {
qb.add(new BoostQuery(new TermQuery(term), token.weight()), BooleanClause.Occur.SHOULD);
qb.add(new BoostQuery(mapper.termQuery(token.token(), context), token.weight()), BooleanClause.Occur.SHOULD);
} else {
lowTerms.add(new BoostQuery(new TermQuery(term), token.weight()), BooleanClause.Occur.SHOULD);
lowTerms.add(new BoostQuery(mapper.termQuery(token.token(), context), token.weight()), BooleanClause.Occur.SHOULD);
}
}
switch (queryStrategy) {
Expand Down

0 comments on commit 5ee088d

Please sign in to comment.