use of org.apache.lucene.search.BooleanQuery in project graphdb by neo4j-attic.
the class FullTxData method injectOrphans.
private Query injectOrphans(Query query) {
if (query instanceof BooleanQuery) {
BooleanQuery source = (BooleanQuery) query;
BooleanQuery result = new BooleanQuery();
for (BooleanClause clause : source.clauses()) {
result.add(injectOrphans(clause.getQuery()), clause.getOccur());
}
return result;
} else {
Set<Term> terms = new HashSet<Term>();
query.extractTerms(terms);
// TODO Don't only use the first term
Term term = terms.iterator().next();
BooleanQuery result = new BooleanQuery();
result.add(query, Occur.SHOULD);
result.add(new TermQuery(new Term(ORPHANS_KEY, term.field())), Occur.SHOULD);
return result;
}
}
use of org.apache.lucene.search.BooleanQuery in project neo4j by neo4j.
the class FullTxData method injectOrphans.
private Query injectOrphans(Query query) {
if (query instanceof BooleanQuery) {
BooleanQuery source = (BooleanQuery) query;
BooleanQuery.Builder builder = new BooleanQuery.Builder();
for (BooleanClause clause : source.clauses()) {
builder.add(injectOrphans(clause.getQuery()), clause.getOccur());
}
return builder.build();
}
String orphanField = extractTermField(query);
if (orphanField == null) {
return query;
}
return new BooleanQuery.Builder().add(query, Occur.SHOULD).add(new TermQuery(new Term(ORPHANS_KEY, orphanField)), Occur.SHOULD).build();
}
use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.
the class MoreLikeThisComponent method process.
@Override
public void process(ResponseBuilder rb) throws IOException {
SolrParams params = rb.req.getParams();
if (params.getBool(MoreLikeThisParams.MLT, false)) {
ReturnFields returnFields = new SolrReturnFields(rb.req);
int flags = 0;
if (returnFields.wantsScore()) {
flags |= SolrIndexSearcher.GET_SCORES;
}
rb.setFieldFlags(flags);
log.debug("Starting MoreLikeThis.Process. isShard: " + params.getBool(ShardParams.IS_SHARD));
SolrIndexSearcher searcher = rb.req.getSearcher();
if (params.getBool(ShardParams.IS_SHARD, false)) {
if (params.get(MoreLikeThisComponent.DIST_DOC_ID) == null) {
if (rb.getResults().docList.size() == 0) {
// return empty response
rb.rsp.add("moreLikeThis", new NamedList<DocList>());
return;
}
MoreLikeThisHandler.MoreLikeThisHelper mlt = new MoreLikeThisHandler.MoreLikeThisHelper(params, searcher);
NamedList<BooleanQuery> bQuery = mlt.getMoreLikeTheseQuery(rb.getResults().docList);
NamedList<String> temp = new NamedList<>();
Iterator<Entry<String, BooleanQuery>> idToQueryIt = bQuery.iterator();
while (idToQueryIt.hasNext()) {
Entry<String, BooleanQuery> idToQuery = idToQueryIt.next();
String s = idToQuery.getValue().toString();
log.debug("MLT Query:" + s);
temp.add(idToQuery.getKey(), idToQuery.getValue().toString());
}
rb.rsp.add("moreLikeThis", temp);
} else {
NamedList<DocList> sim = getMoreLikeThese(rb, rb.req.getSearcher(), rb.getResults().docList, flags);
rb.rsp.add("moreLikeThis", sim);
}
} else {
// non distrib case
NamedList<DocList> sim = getMoreLikeThese(rb, rb.req.getSearcher(), rb.getResults().docList, flags);
rb.rsp.add("moreLikeThis", sim);
}
}
}
use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.
the class PointTypeValueSource method createSpatialQuery.
/**
* Calculates the range and creates a RangeQuery (bounding box) wrapped in a BooleanQuery (unless the dimension is
* 1, one range for every dimension, AND'd together by a Boolean
*
* @param parser The parser
* @param options The {@link org.apache.solr.search.SpatialOptions} for this filter.
* @return The Query representing the bounding box around the point.
*/
@Override
public Query createSpatialQuery(QParser parser, SpatialOptions options) {
String[] pointStrs = parseCommaSeparatedList(options.pointStr, dimension);
double[] point = new double[dimension];
try {
for (int i = 0; i < pointStrs.length; i++) {
point[i] = Double.parseDouble(pointStrs[i]);
}
} catch (NumberFormatException e) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
}
IndexSchema schema = parser.getReq().getSchema();
if (dimension == 1) {
//TODO: Handle distance measures
String lower = String.valueOf(point[0] - options.distance);
String upper = String.valueOf(point[0] + options.distance);
SchemaField subSF = subField(options.field, 0, schema);
// points must currently be ordered... should we support specifying any two opposite corner points?
return subSF.getType().getRangeQuery(parser, subSF, lower, upper, true, true);
} else {
BooleanQuery.Builder tmp = new BooleanQuery.Builder();
//TODO: Handle distance measures, as this assumes Euclidean
double[] ur = vectorBoxCorner(point, null, options.distance, true);
double[] ll = vectorBoxCorner(point, null, options.distance, false);
for (int i = 0; i < ur.length; i++) {
SchemaField subSF = subField(options.field, i, schema);
Query range = subSF.getType().getRangeQuery(parser, subSF, String.valueOf(ll[i]), String.valueOf(ur[i]), true, true);
tmp.add(range, BooleanClause.Occur.MUST);
}
return tmp.build();
}
}
use of org.apache.lucene.search.BooleanQuery in project lucene-solr by apache.
the class ExtendedDismaxQParser method parseOriginalQuery.
/**
* Parses the user's original query. This method attempts to cleanly parse the specified query string using the specified parser, any Exceptions are ignored resulting in null being returned.
*
* @param up parser used
* @param mainUserQuery query string that is parsed
* @param clauses used to dictate "min should match" logic
* @param config Configuration options for this parse request
* @return the resulting query with "min should match" rules applied as specified in the config.
* @see #parseEscapedQuery
*/
protected Query parseOriginalQuery(ExtendedSolrQueryParser up, String mainUserQuery, List<Clause> clauses, ExtendedDismaxConfiguration config) {
Query query = null;
try {
up.setRemoveStopFilter(!config.stopwords);
up.exceptions = true;
query = up.parse(mainUserQuery);
if (shouldRemoveStopFilter(config, query)) {
// if the query was all stop words, remove none of them
up.setRemoveStopFilter(true);
query = up.parse(mainUserQuery);
}
} catch (Exception e) {
// ignore failure and reparse later after escaping reserved chars
up.exceptions = false;
}
if (query == null) {
return null;
}
// and there were explicit operators (except for AND).
if (query instanceof BooleanQuery) {
// config.minShouldMatch holds the value of mm which MIGHT have come from the user,
// but could also have been derived from q.op.
String mmSpec = config.minShouldMatch;
if (foundOperators(clauses, config.lowercaseOperators)) {
// Use provided mm spec if present, otherwise turn off mm processing
mmSpec = params.get(DisMaxParams.MM, "0%");
}
query = SolrPluginUtils.setMinShouldMatch((BooleanQuery) query, mmSpec, config.mmAutoRelax);
}
return query;
}
Aggregations