Example usage for org.apache.lucene.search BooleanQuery BooleanQuery

List of usage examples for org.apache.lucene.search BooleanQuery BooleanQuery

Introduction

In this page you can find the example usage for org.apache.lucene.search BooleanQuery BooleanQuery.

Prototype

BooleanQuery

Source Link

Usage

From source file:byrne.mitre.MitreQuery.java

License:Apache License

public void run() {

    try {/*from ww  w .ja  v a 2 s  .  com*/

        TokenStream tokenStream = analyzer.tokenStream("ngrams", new StringReader(entry.getFullName()));

        BooleanQuery bq = new BooleanQuery();
        while (tokenStream.incrementToken()) {
            Term t = new Term("ngrams", tokenStream.getAttribute(TermAttribute.class).term());
            bq.add(new TermQuery(t), BooleanClause.Occur.SHOULD);
        }

        TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
        searcher.search(bq, collector);
        ScoreDoc[] hits = collector.topDocs().scoreDocs;

        for (int i = 0; i < hits.length; ++i) {

            int docId = hits[i].doc;
            Document d = searcher.doc(docId);

            out.write(entry.getID() + "|" + d.get("id") + "|" + df.format(hits[i].score) + "\n");
        }
    } catch (IOException IOE) {
    }
}

From source file:ca.dracode.ais.indexer.FileSearcher.java

License:Open Source License

/**
 * Returns the first document that matches the given field/value combination
 * @param field The field to check/*from www  . jav  a2 s  . c  o m*/
 * @param value The value to check with
 * @return the Document found by the IndexSearcher
 * @throws IOException
 */
public Document getDocument(String field, String value) throws IOException {
    //Log.i(TAG, "Checking for existance of " + value);
    BooleanQuery qry = new BooleanQuery();
    qry.add(new TermQuery(new Term(field, value)), BooleanClause.Occur.MUST);
    if (this.indexSearcher != null) {
        ScoreDoc[] hits;
        hits = indexSearcher.search(qry, 1).scoreDocs;
        if (hits != null && hits.length > 0) {
            return indexSearcher.doc(hits[0].doc);
        }
    }
    return null;
}

From source file:ca.dracode.ais.indexer.FileSearcher.java

License:Open Source License

/**
 * Searches the index for a metadata Document with "id" matching the passed id
 * @param id The "id" of the metadata Document
 * @return The metadata Document matching id or null if it does not exist
 *///  w w  w. ja  v a 2s . co m
public Document getMetaFile(String id) {
    BooleanQuery qry = new BooleanQuery();
    qry.add(new TermQuery(new Term("id", id + ":meta")), BooleanClause.Occur.MUST);
    ScoreDoc[] hits = null;
    try {
        hits = indexSearcher.search(qry, 1).scoreDocs;
    } catch (IOException e) {
        Log.e(TAG, "Error ", e);
    }
    if (hits == null || hits.length == 0) {
        return null;
    }
    Document doc = null;
    try {
        doc = indexSearcher.doc(hits[0].doc);
    } catch (IOException e) {
        Log.e(TAG, "Error ", e);
    }
    return doc;
}

From source file:ca.dracode.ais.indexer.FileSearcher.java

License:Open Source License

/**
 * Creates a query based on the given term field and type
 * @param term Search Term for the query
 * @param field Document Field for the Query which the term is matched against
 * @param type The type of query to be created, either QUERY_BOOLEAN, or QUERY_STANDARD,
 * @return a query for the given field and term using either a BooleanQuery with a
 * WildcardQuery for the term or a Query built from a QueryParser and SimpleAnalyzer
 *///from ww w  .j  a va  2 s. c  o  m
private Query getQuery(String term, String field, int type) {
    Query qry = null;
    if (type == FileSearcher.QUERY_BOOLEAN) {
        qry = new BooleanQuery();
        String[] words = term.split(" ");
        ((BooleanQuery) qry).add(new WildcardQuery(new Term(field, "*" + words[0])), BooleanClause.Occur.MUST);
        if (words.length > 1) {
            for (int i = 1; i < words.length - 1; i++) {
                ((BooleanQuery) qry).add(new WildcardQuery(new Term(field, words[i])),
                        BooleanClause.Occur.MUST);
            }
            ((BooleanQuery) qry).add(new WildcardQuery(new Term(field, words[words.length - 1] + "*")),
                    BooleanClause.Occur.MUST);
        }
    } else if (type == FileSearcher.QUERY_STANDARD) {
        try {
            qry = new QueryParser(Version.LUCENE_47, field, new SimpleAnalyzer(Version.LUCENE_47)).parse(term);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    return qry;
}

From source file:ca.dracode.ais.indexer.FileSearcher.java

License:Open Source License

/**
 * Creates a directory filter; also filters a range of pages
 * @param constrainField The field that contains the directory info
 * @param constrainValues The directories to which the filters shold limit
 * @return The created filter/*from   w ww. ja v a 2 s. co m*/
 */
private Filter getFilter(String constrainField, List<String> constrainValues, int type, int startPage,
        int endPage) {
    BooleanQuery cqry = new BooleanQuery();
    if (constrainValues.size() == 1) {
        cqry.add(new TermQuery(new Term(constrainField, constrainValues.get(0))), BooleanClause.Occur.MUST);
    } else {
        for (String s : constrainValues) {
            cqry.add(new TermQuery(new Term(constrainField, s)), BooleanClause.Occur.SHOULD);
        }
    }
    if (type == FileSearcher.QUERY_BOOLEAN && startPage != -1 && endPage != -1) {
        cqry.add(NumericRangeQuery.newIntRange("page", startPage, endPage, true, true),
                BooleanClause.Occur.MUST);
    }
    return new QueryWrapperFilter(cqry);
}

From source file:cc.pp.analyzer.ik.query.IKQueryExpressionParser.java

License:Apache License

/**
 * ???BooleanQuery/*from   w ww .j a  va2s. c  o m*/
 * @param op
 * @return
 */
private Query toBooleanQuery(Element op) {
    if (this.querys.size() == 0) {
        return null;
    }

    BooleanQuery resultQuery = new BooleanQuery();

    if (this.querys.size() == 1) {
        return this.querys.get(0);
    }

    Query q2 = this.querys.pop();
    Query q1 = this.querys.pop();
    if ('&' == op.type) {
        if (q1 != null) {
            if (q1 instanceof BooleanQuery) {
                BooleanClause[] clauses = ((BooleanQuery) q1).getClauses();
                if (clauses.length > 0 && clauses[0].getOccur() == Occur.MUST) {
                    for (BooleanClause c : clauses) {
                        resultQuery.add(c);
                    }
                } else {
                    resultQuery.add(q1, Occur.MUST);
                }

            } else {
                //q1 instanceof TermQuery
                //q1 instanceof TermRangeQuery
                //q1 instanceof PhraseQuery
                //others
                resultQuery.add(q1, Occur.MUST);
            }
        }

        if (q2 != null) {
            if (q2 instanceof BooleanQuery) {
                BooleanClause[] clauses = ((BooleanQuery) q2).getClauses();
                if (clauses.length > 0 && clauses[0].getOccur() == Occur.MUST) {
                    for (BooleanClause c : clauses) {
                        resultQuery.add(c);
                    }
                } else {
                    resultQuery.add(q2, Occur.MUST);
                }

            } else {
                //q1 instanceof TermQuery
                //q1 instanceof TermRangeQuery
                //q1 instanceof PhraseQuery
                //others
                resultQuery.add(q2, Occur.MUST);
            }
        }

    } else if ('|' == op.type) {
        if (q1 != null) {
            if (q1 instanceof BooleanQuery) {
                BooleanClause[] clauses = ((BooleanQuery) q1).getClauses();
                if (clauses.length > 0 && clauses[0].getOccur() == Occur.SHOULD) {
                    for (BooleanClause c : clauses) {
                        resultQuery.add(c);
                    }
                } else {
                    resultQuery.add(q1, Occur.SHOULD);
                }

            } else {
                //q1 instanceof TermQuery
                //q1 instanceof TermRangeQuery
                //q1 instanceof PhraseQuery
                //others
                resultQuery.add(q1, Occur.SHOULD);
            }
        }

        if (q2 != null) {
            if (q2 instanceof BooleanQuery) {
                BooleanClause[] clauses = ((BooleanQuery) q2).getClauses();
                if (clauses.length > 0 && clauses[0].getOccur() == Occur.SHOULD) {
                    for (BooleanClause c : clauses) {
                        resultQuery.add(c);
                    }
                } else {
                    resultQuery.add(q2, Occur.SHOULD);
                }
            } else {
                //q2 instanceof TermQuery
                //q2 instanceof TermRangeQuery
                //q2 instanceof PhraseQuery
                //others
                resultQuery.add(q2, Occur.SHOULD);

            }
        }

    } else if ('-' == op.type) {
        if (q1 == null || q2 == null) {
            throw new IllegalStateException("?SubQuery ??");
        }

        if (q1 instanceof BooleanQuery) {
            BooleanClause[] clauses = ((BooleanQuery) q1).getClauses();
            if (clauses.length > 0) {
                for (BooleanClause c : clauses) {
                    resultQuery.add(c);
                }
            } else {
                resultQuery.add(q1, Occur.MUST);
            }

        } else {
            //q1 instanceof TermQuery
            //q1 instanceof TermRangeQuery
            //q1 instanceof PhraseQuery
            //others
            resultQuery.add(q1, Occur.MUST);
        }

        resultQuery.add(q2, Occur.MUST_NOT);
    }
    return resultQuery;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.search.detailed.DetailedQueryBuilder.java

License:Apache License

private Query createQuery(DetailedSearchCriteria searchCriteria) {
    List<DetailedSearchCriterion> criteria = searchCriteria.getCriteria();
    Occur occureCondition = createOccureCondition(searchCriteria.getConnection());

    Analyzer analyzer = LuceneQueryBuilder.createSearchAnalyzer();
    BooleanQuery resultQuery = new BooleanQuery();
    for (DetailedSearchCriterion criterion : criteria) {
        List<String> fieldNames = getIndexFieldNames(criterion.getField());
        String searchPattern = LuceneQueryBuilder.adaptQuery(criterion.getValue());
        Query luceneQuery = LuceneQueryBuilder.parseQuery(fieldNames, searchPattern, analyzer);
        resultQuery.add(luceneQuery, occureCondition);
    }/*from w ww.j  a v a  2 s  . co  m*/
    return resultQuery;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.search.LuceneQueryBuilder.java

License:Apache License

public static Query parseQuery(final List<String> fieldNames, final String searchPattern, Analyzer analyzer)
        throws UserFailureException {
    BooleanQuery resultQuery = new BooleanQuery();
    for (String fieldName : fieldNames) {
        Query query = parseQuery(fieldName, searchPattern, analyzer);
        resultQuery.add(query, Occur.SHOULD);
    }//from w  ww .  j  av  a 2 s .c  om
    return resultQuery;
}

From source file:cn.b2b.index.product.index.ProductQueryOptimizer.java

@Override
public Writable optimize(Searcher searcher, Writable param) throws Exception {

    byte queryType = ProductConstants.QUERY_TYPE_MUST_AND;
    //queryType = ProductConstants.QUERY_TYPE_MUST_OR;
    long begin = System.currentTimeMillis();

    ProductQueryParam queryParam = (ProductQueryParam) param;
    Query oriquery = queryParam.getQuery();

    BooleanQuery original = QueryFilters.filter(oriquery, queryType);

    BooleanQuery oriQuery = new BooleanQuery();
    BooleanQuery query = new BooleanQuery();
    BooleanQuery filterQuery = filterQuery(searcher, original, oriQuery);

    query.add(oriQuery, BooleanClause.Occur.MUST);

    Query attrQuery = queryParam.getAttrQuery();
    BooleanQuery attribute = QueryFilters.filter(attrQuery, Constants.SLOP_SCORE_MATCH_CLASS);
    if (attribute.getClauses() != null && attribute.getClauses().length > 0) {
        query.add(attribute, BooleanClause.Occur.MUST);
    }//from  w  w w .  j  a  va2  s  .  c  o m

    Query notquery = queryParam.getNotQuery();
    BooleanQuery notQuery = QueryFilters.filter(notquery, Constants.SLOP_SCORE_MATCH_CLASS);
    if (notQuery.getClauses() != null && notQuery.getClauses().length > 0) {
        query.add(notQuery, Occur.MUST_NOT);
    }

    Filter filter = null;
    if (filterQuery != null) {
        synchronized (cache) { // check cache
            filter = (Filter) cache.get(filterQuery);
        }
        if (filter == null) { // miss
            filter = new QueryFilter(filterQuery); // construct new entry
            synchronized (cache) {
                cache.put(filterQuery, filter); // cache it
            }
        }
    }

    IndexSearcher.LOG.info("QUERY:" + query);

    ScoreParam scoreParam = new ScoreParam();
    scoreParam.setSortType(queryParam.getSortType());
    scoreParam.setSearchType(queryParam.getSearchType());
    scoreParam.setTradeLevel(queryParam.getTradeLevel());
    scoreParam.setScorePercent(2.0f);
    // store segment query word
    query.setQueryStr(original.getQueryStr());
    TopDocs[] topDocss = new TopDocs[2];
    TopDocs topDocs = null;
    TopDocs or = null;
    BitVector bites = makeBitQuery(searcher.maxDoc(), queryParam);

    long end = System.currentTimeMillis();

    topDocs = searcher.searchProduct(query, filter, indexScorer, queryParam.getHitsNum(), scoreParam, bites);

    IndexSearcher.LOG.info("totalhits:\t" + topDocs.totalHits);

    if (queryType == ProductConstants.QUERY_TYPE_MUST_AND) {
        scoreParam.setScorePercent(0.5f);
        BooleanQuery bqs = new BooleanQuery();
        BooleanQuery query_or = QueryFilters.filter(oriquery,
                ProductConstants.QUERY_TYPE_MUST_AND_MUST_HAS_NEED_WORD);
        bqs.add(query, Occur.MUST_NOT);
        bqs.add(query_or, Occur.MUST);
        IndexSearcher.LOG.info("QUERY_AND:" + bqs);
        or = searcher.searchProduct(bqs, filter, indexScorer, queryParam.getHitsNum(), scoreParam, bites);
        topDocss[0] = topDocs;
        topDocss[1] = or;
        //topDocss[1] = null;
        return this.serializeHits(topDocss);
    }
    return this.serializeHits(topDocs);
}

From source file:cn.b2b.index.product.index.ProductQueryOptimizer.java

private BooleanQuery filterQuery(Searcher searcher, BooleanQuery original, BooleanQuery query)
        throws IOException {
    BooleanQuery filterQuery = null;/*from w w w.  j  a v a  2s.co m*/
    BooleanClause[] clauses = original.getClauses();
    for (int i = 0; i < clauses.length; i++) {
        BooleanClause c = clauses[i];
        if (c.isRequired() // required
                && c.getQuery().getBoost() == 0.0f // boost is zero
                && c.getQuery() instanceof TermQuery // TermQuery
                && (searcher.docFreq(((TermQuery) c.getQuery()).getTerm())
                        / (float) searcher.maxDoc()) >= threshold) { // check threshold
            if (filterQuery == null)
                filterQuery = new BooleanQuery();
            // lucene1.4.3 -> lucene2.0.0
            // filterQuery.add(c.getQuery(), true, false); // filter it
            filterQuery.add(c.getQuery(), BooleanClause.Occur.MUST); // filter
            // it
        } else {
            query.add(c); // query it
        }
    }
    return filterQuery;
}