Search in sources :

Example 31 with BooleanQuery

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;
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) HashSet(java.util.HashSet)

Example 32 with BooleanQuery

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();
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term)

Example 33 with BooleanQuery

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);
        }
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) SolrReturnFields(org.apache.solr.search.SolrReturnFields) ReturnFields(org.apache.solr.search.ReturnFields) MoreLikeThisHandler(org.apache.solr.handler.MoreLikeThisHandler) NamedList(org.apache.solr.common.util.NamedList) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) SolrReturnFields(org.apache.solr.search.SolrReturnFields) Entry(java.util.Map.Entry) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) DocList(org.apache.solr.search.DocList)

Example 34 with BooleanQuery

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();
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) SolrException(org.apache.solr.common.SolrException)

Example 35 with BooleanQuery

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;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostedQuery(org.apache.lucene.queries.function.BoostedQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

Aggregations

BooleanQuery (org.apache.lucene.search.BooleanQuery)223 TermQuery (org.apache.lucene.search.TermQuery)134 Term (org.apache.lucene.index.Term)114 Query (org.apache.lucene.search.Query)94 BooleanClause (org.apache.lucene.search.BooleanClause)60 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)53 BoostQuery (org.apache.lucene.search.BoostQuery)50 PhraseQuery (org.apache.lucene.search.PhraseQuery)45 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)45 PrefixQuery (org.apache.lucene.search.PrefixQuery)39 TopDocs (org.apache.lucene.search.TopDocs)39 IndexReader (org.apache.lucene.index.IndexReader)38 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)34 IndexSearcher (org.apache.lucene.search.IndexSearcher)34 WildcardQuery (org.apache.lucene.search.WildcardQuery)34 Document (org.apache.lucene.document.Document)30 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)28 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)28 ArrayList (java.util.ArrayList)27 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)27