use of org.apache.lucene.search.ConstantScoreQuery in project SearchServices by Alfresco.
the class Solr4QueryParser method createFingerPrintQuery.
/**
* @param field
* @param queryText
* @param analysisMode
*
* @param luceneFunction
* @return
* @throws IOException
* @throws ParseException
*/
private Query createFingerPrintQuery(String field, String queryText, Collection values, AnalysisMode analysisMode, LuceneFunction luceneFunction) throws IOException, ParseException {
String[] parts = queryText.split("_");
if (parts.length == 0) {
return createIsNodeQuery("T");
}
if (values != null) {
int bandSize = 1;
float fraction = -1;
float truePositive = 1;
if (parts.length > 1) {
fraction = Float.parseFloat(parts[1]);
if (fraction > 1) {
fraction /= 100;
}
}
if (parts.length > 2) {
truePositive = Float.parseFloat(parts[2]);
if (truePositive > 1) {
truePositive /= 100;
}
bandSize = computeBandSize(values.size(), fraction, truePositive);
}
BooleanQuery.Builder builder = new BooleanQuery.Builder();
BooleanQuery.Builder childBuilder = new BooleanQuery.Builder();
int rowInBand = 0;
for (Object token : values) {
TermQuery tq = new TermQuery(new Term("MINHASH", token.toString()));
if (bandSize == 1) {
builder.add(new ConstantScoreQuery(tq), Occur.SHOULD);
} else {
childBuilder.add(new ConstantScoreQuery(tq), Occur.MUST);
rowInBand++;
if (rowInBand == bandSize) {
builder.add(new ConstantScoreQuery(childBuilder.setDisableCoord(true).build()), Occur.SHOULD);
childBuilder = new BooleanQuery.Builder();
rowInBand = 0;
}
}
}
// start
if (childBuilder.build().clauses().size() > 0) {
for (Object token : values) {
TermQuery tq = new TermQuery(new Term("MINHASH", token.toString()));
childBuilder.add(new ConstantScoreQuery(tq), Occur.MUST);
rowInBand++;
if (rowInBand == bandSize) {
builder.add(new ConstantScoreQuery(childBuilder.setDisableCoord(true).build()), Occur.SHOULD);
break;
}
}
}
builder.setDisableCoord(true);
if (parts.length == 2) {
builder.setMinimumNumberShouldMatch((int) (Math.ceil(values.size() * fraction)));
}
Query q = builder.build();
return q;
} else {
return getFieldQueryImpl(field, queryText, analysisMode, luceneFunction);
}
}
use of org.apache.lucene.search.ConstantScoreQuery in project greplin-lucene-utils by Cue.
the class Queries method constantScore.
/**
* Returns a version of the given query that always matches with the
* given score.
* @param query the query
* @param score the desired score
* @return the constant score query
*/
public static ConstantScoreQuery constantScore(final Query query, final float score) {
ConstantScoreQuery result = new ConstantScoreQuery(query);
result.setBoost(score);
return result;
}
use of org.apache.lucene.search.ConstantScoreQuery in project neo4j by neo4j.
the class LuceneDocumentStructureTest method shouldBuildQueryRepresentingStringProperty.
@Test
void shouldBuildQueryRepresentingStringProperty() {
// given
BooleanQuery booleanQuery = (BooleanQuery) newSeekQuery("Characters");
ConstantScoreQuery query = (ConstantScoreQuery) booleanQuery.clauses().get(0).getQuery();
// then
assertEquals("Characters", ((TermQuery) query.getQuery()).getTerm().text());
}
use of org.apache.lucene.search.ConstantScoreQuery in project neo4j by neo4j.
the class LuceneDocumentStructureTest method shouldBuildQueryRepresentingMultipleProperties.
@Test
void shouldBuildQueryRepresentingMultipleProperties() {
// given
BooleanQuery booleanQuery = (BooleanQuery) newSeekQuery("foo", "bar");
ConstantScoreQuery fooScoreQuery = (ConstantScoreQuery) booleanQuery.clauses().get(0).getQuery();
TermQuery fooTermQuery = (TermQuery) fooScoreQuery.getQuery();
ConstantScoreQuery barScoreQuery = (ConstantScoreQuery) booleanQuery.clauses().get(1).getQuery();
TermQuery barTermQuery = (TermQuery) barScoreQuery.getQuery();
// then
assertEquals("foo", fooTermQuery.getTerm().text());
assertEquals("bar", barTermQuery.getTerm().text());
}
use of org.apache.lucene.search.ConstantScoreQuery in project neo4j by neo4j.
the class FulltextIndexReader method query.
@Override
public void query(QueryContext context, IndexProgressor.EntityValueClient client, IndexQueryConstraints constraints, PropertyIndexQuery... queries) throws IndexNotApplicableKernelException {
BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
for (PropertyIndexQuery indexQuery : queries) {
if (indexQuery.type() == PropertyIndexQuery.IndexQueryType.fulltextSearch) {
PropertyIndexQuery.FulltextSearchPredicate fulltextSearch = (PropertyIndexQuery.FulltextSearchPredicate) indexQuery;
try {
queryBuilder.add(parseFulltextQuery(fulltextSearch.query()), BooleanClause.Occur.SHOULD);
} catch (ParseException e) {
throw new RuntimeException("Could not parse the given fulltext search query: '" + fulltextSearch.query() + "'.", e);
}
} else {
// Not fulltext query
assertNotComposite(queries);
assertCypherCompatible();
Query query;
if (indexQuery.type() == PropertyIndexQuery.IndexQueryType.stringContains) {
PropertyIndexQuery.StringContainsPredicate scp = (PropertyIndexQuery.StringContainsPredicate) indexQuery;
String searchTerm = QueryParser.escape(scp.contains().stringValue());
Term term = new Term(propertyNames[0], "*" + searchTerm + "*");
query = new WildcardQuery(term);
} else if (indexQuery.type() == PropertyIndexQuery.IndexQueryType.stringSuffix) {
PropertyIndexQuery.StringSuffixPredicate ssp = (PropertyIndexQuery.StringSuffixPredicate) indexQuery;
String searchTerm = QueryParser.escape(ssp.suffix().stringValue());
Term term = new Term(propertyNames[0], "*" + searchTerm);
query = new WildcardQuery(term);
} else if (indexQuery.type() == PropertyIndexQuery.IndexQueryType.stringPrefix) {
PropertyIndexQuery.StringPrefixPredicate spp = (PropertyIndexQuery.StringPrefixPredicate) indexQuery;
String searchTerm = spp.prefix().stringValue();
Term term = new Term(propertyNames[0], searchTerm);
query = new LuceneDocumentStructure.PrefixMultiTermsQuery(term);
} else if (indexQuery.getClass() == PropertyIndexQuery.ExactPredicate.class && indexQuery.valueGroup() == ValueGroup.TEXT) {
PropertyIndexQuery.ExactPredicate exact = (PropertyIndexQuery.ExactPredicate) indexQuery;
String searchTerm = ((TextValue) exact.value()).stringValue();
Term term = new Term(propertyNames[0], searchTerm);
query = new ConstantScoreQuery(new TermQuery(term));
} else if (indexQuery.getClass() == PropertyIndexQuery.TextRangePredicate.class) {
PropertyIndexQuery.TextRangePredicate sp = (PropertyIndexQuery.TextRangePredicate) indexQuery;
query = newRangeSeekByStringQuery(propertyNames[0], sp.from(), sp.fromInclusive(), sp.to(), sp.toInclusive());
} else {
throw new IndexNotApplicableKernelException("A fulltext schema index cannot answer " + indexQuery.type() + " queries on " + indexQuery.valueCategory() + " values.");
}
queryBuilder.add(query, BooleanClause.Occur.MUST);
}
}
Query query = queryBuilder.build();
ValuesIterator itr = searchLucene(query, constraints, context, context.cursorContext(), context.memoryTracker());
IndexProgressor progressor = new FulltextIndexProgressor(itr, client, constraints);
client.initialize(index, progressor, queries, constraints, true);
}
Aggregations