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:com.flaptor.hounder.searcher.query.DifferenceQuery.java

License:Apache License

public org.apache.lucene.search.Query getLuceneQuery() {
    BooleanQuery bq = new BooleanQuery();
    bq.add(leftTerm.getLuceneQuery(), BooleanClause.Occur.MUST);
    bq.add(rightTerm.getLuceneQuery(), BooleanClause.Occur.MUST_NOT);
    bq.setBoost(boost);//from www  .j av  a  2 s  . c om
    return bq;
}

From source file:com.flaptor.hounder.searcher.query.OrQuery.java

License:Apache License

public org.apache.lucene.search.Query getLuceneQuery() {
    BooleanQuery bq = new BooleanQuery();
    if (leftTerm instanceof OrQuery) {
        for (BooleanClause bc : ((BooleanQuery) leftTerm.getLuceneQuery()).getClauses()) {
            bq.add(bc);/*w  w w . j a  va  2 s  .c  o  m*/
        }
    } else {
        bq.add(leftTerm.getLuceneQuery(), BooleanClause.Occur.SHOULD);
    }
    if (rightTerm instanceof OrQuery) {
        for (BooleanClause bc : ((BooleanQuery) rightTerm.getLuceneQuery()).getClauses()) {
            bq.add(bc);
        }
    } else {
        bq.add(rightTerm.getLuceneQuery(), BooleanClause.Occur.SHOULD);
    }
    bq.setBoost(boost);
    return bq;
}

From source file:com.flaptor.hounder.searcher.spell.SpellChecker.java

License:Apache License

/**
 * Suggest similar words (restricted or not to a field of a user index)
 * @param word String the word you want a spell check done on
 * @param num_sug int the number of suggest words
 * @param ir the indexReader of the user index (can be null see field param)
 * @param field String the field of the user index: if field is not null, the suggested
 * words are restricted to the words present in this field.
 * @param morePopular boolean return only the suggest words that are more frequent than the searched word
 * (only if restricted mode = (indexReader!=null and field!=null)
 * @throws IOException//from  w  ww .j  a v a  2  s  .co m
 * @return String[] the sorted list of the suggest words with this 2 criteria:
 * first criteria: the edit distance, second criteria (only if restricted mode): the popularity
 * of the suggest words in the field of the user index
 */
public String[] suggestSimilar(String word, int num_sug, IndexReader ir, String field, boolean morePopular)
        throws IOException {

    float minScore = min;
    final TRStringDistance sd = new TRStringDistance(word);
    final int lengthWord = word.length();

    final int goalFreq = (morePopular && ir != null) ? ir.docFreq(new Term(field, word)) : 0;
    if (!morePopular && goalFreq > 0) {
        return new String[] { word }; // return the word if it exist in the index and i don't want a more popular word
    }

    BooleanQuery query = new BooleanQuery();
    String[] grams;
    String key;

    for (int ng = getMin(lengthWord); ng <= getMax(lengthWord); ng++) {

        key = "gram" + ng; // form key

        grams = formGrams(word, ng); // form word into ngrams (allow dups too)

        if (grams.length == 0) {
            continue; // hmm
        }
        if (bStart > 0) { // should we boost prefixes?
            add(query, "start" + ng, grams[0], bStart); // matches start of word

        }
        if (bEnd > 0) { // should we boost suffixes
            add(query, "end" + ng, grams[grams.length - 1], bEnd); // matches end of word

        }
        for (int i = 0; i < grams.length; i++) {
            add(query, key, grams[i]);
        }

    }

    IndexSearcher searcher = new IndexSearcher(this.spellindex);
    TopDocCollector collector = new TopDocCollector(10 * num_sug); // go thru more than 'maxr' matches in case the distance filter triggers
    searcher.search(query, collector);
    ScoreDoc[] scoreDocs = collector.topDocs().scoreDocs;

    SuggestWordQueue sugqueue = new SuggestWordQueue(num_sug);
    SuggestWord sugword = new SuggestWord();
    for (int i = 0; i < scoreDocs.length; i++) {

        Document doc = searcher.doc(i);
        sugword.string = doc.get(F_WORD); // get orig word)

        if (sugword.string.equals(word)) {
            continue; // don't suggest a word for itself, that would be silly
        }

        //edit distance/normalize with the min word length
        sugword.score = doc.getBoost() * (1.0f
                - ((float) sd.getDistance(sugword.string) / Math.min(sugword.string.length(), lengthWord)));
        if (sugword.score < minScore) {
            continue;
        }
        if (ir != null) { // use the user index
            sugword.freq = ir.docFreq(new Term(field, sugword.string)); // freq in the index
            if ((morePopular && goalFreq > sugword.freq) || sugword.freq < 1) { // don't suggest a word that is not present in the field
                continue;
            }
        }
        sugqueue.insert(sugword);
        if (sugqueue.size() == num_sug) {
            //if queue full , maintain the min score
            minScore = ((SuggestWord) sugqueue.top()).score;
        }
        sugword = new SuggestWord();
    }

    // convert to array string
    String[] list = new String[sugqueue.size()];
    for (int i = sugqueue.size() - 1; i >= 0; i--) {
        list[i] = ((SuggestWord) sugqueue.pop()).string;
    }

    searcher.close();
    return list;
}

From source file:com.flaptor.indextank.query.AndQuery.java

License:Apache License

@Override
public org.apache.lucene.search.Query getLuceneQuery() {
    BooleanQuery bq = new BooleanQuery();
    if (leftQuery instanceof AndQuery) {
        for (BooleanClause bc : ((BooleanQuery) leftQuery.getLuceneQuery()).getClauses()) {
            bq.add(bc);//from  w w w .java2  s.c  o  m
        }
    } else {
        bq.add(leftQuery.getLuceneQuery(), BooleanClause.Occur.MUST);
    }
    if (rightQuery instanceof AndQuery) {
        for (BooleanClause bc : ((BooleanQuery) rightQuery.getLuceneQuery()).getClauses()) {
            bq.add(bc);
        }
    } else {
        bq.add(rightQuery.getLuceneQuery(), BooleanClause.Occur.MUST);
    }
    return bq;
}

From source file:com.flaptor.indextank.query.DifferenceQuery.java

License:Apache License

public org.apache.lucene.search.Query getLuceneQuery() {
    BooleanQuery bq = new BooleanQuery();
    bq.add(leftQuery.getLuceneQuery(), BooleanClause.Occur.MUST);
    bq.add(rightQuery.getLuceneQuery(), BooleanClause.Occur.MUST_NOT);
    return bq;/*w w  w. ja v  a 2 s . com*/
}

From source file:com.flaptor.indextank.query.OrQuery.java

License:Apache License

public org.apache.lucene.search.Query getLuceneQuery() {
    BooleanQuery bq = new BooleanQuery();
    if (leftQuery instanceof OrQuery) {
        for (BooleanClause bc : ((BooleanQuery) leftQuery.getLuceneQuery()).getClauses()) {
            bq.add(bc);//from w w  w. j av  a  2 s.  c  om
        }
    } else {
        bq.add(leftQuery.getLuceneQuery(), BooleanClause.Occur.SHOULD);
    }
    if (rightQuery instanceof OrQuery) {
        for (BooleanClause bc : ((BooleanQuery) rightQuery.getLuceneQuery()).getClauses()) {
            bq.add(bc);
        }
    } else {
        bq.add(rightQuery.getLuceneQuery(), BooleanClause.Occur.SHOULD);
    }
    return bq;
}

From source file:com.foundationdb.server.service.text.FullTextQueryBuilder.java

License:Open Source License

public FullTextQueryExpression booleanQuery(final List<FullTextQueryExpression> queries,
        final List<BooleanType> types) {
    boolean isConstant = true;
    for (FullTextQueryExpression query : queries) {
        if (!(query instanceof Constant)) {
            isConstant = false;//from w  w  w . j  av  a  2  s  .co m
            break;
        }
    }
    FullTextQueryExpression result = new FullTextQueryExpression() {
        @Override
        public boolean needsBindings() {
            for (FullTextQueryExpression query : queries) {
                if (query.needsBindings()) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public Query getQuery(QueryContext context, QueryBindings bindings) {
            BooleanQuery query = new BooleanQuery();
            for (int i = 0; i < queries.size(); i++) {
                BooleanClause.Occur occur;
                switch (types.get(i)) {
                case MUST:
                    occur = BooleanClause.Occur.MUST;
                    break;
                case NOT:
                    occur = BooleanClause.Occur.MUST_NOT;
                    break;
                case SHOULD:
                    occur = BooleanClause.Occur.SHOULD;
                    break;
                default:
                    throw new IllegalArgumentException(types.get(i).toString());
                }
                query.add(queries.get(i).getQuery(context, bindings), occur);
            }
            return query;
        }

        @Override
        public CompoundExplainer getExplainer(ExplainContext context) {
            CompoundExplainer explainer = new CompoundExplainer(Type.FUNCTION);
            explainer.addAttribute(Label.NAME, PrimitiveExplainer.getInstance("AND"));
            for (FullTextQueryExpression query : queries) {
                explainer.get().put(Label.OPERAND, query.getExplainer(context));
            }
            return explainer;
        }

        @Override
        public String toString() {
            return queries.toString();
        }
    };
    if (isConstant) {
        return new Constant(result.getQuery(buildContext, null));
    } else {
        return result;
    }
}

From source file:com.gauronit.tagmata.core.Indexer.java

License:Open Source License

public ArrayList<CardSnapshot> search(String searchText, ArrayList<String> indexNames, boolean searchInTitle,
        boolean searchInTags, boolean searchInText, boolean superFuzzy) {
    ArrayList<CardSnapshot> cardSnaps = new ArrayList();
    try {//from  ww  w.  j a va2s.c o m
        ArrayList<IndexSearcher> searchers = new ArrayList<IndexSearcher>();

        for (String indexName : indexNames) {
            IndexReader reader = IndexReader
                    .open(FSDirectory.open(new File(indexDir + File.separator + indexName),
                            new SimpleFSLockFactory(indexDir + File.separator + indexName)));
            IndexSearcher searcher = new IndexSearcher(reader);
            searchers.add(searcher);
        }

        BooleanQuery query = new BooleanQuery();
        if (searchInTitle) {
            IndexerUtil.getTokenizedQuery(query, "title", searchText, superFuzzy);
        }
        if (searchInTags) {
            IndexerUtil.getTokenizedQuery(query, "tags", searchText, superFuzzy);
        }
        if (searchInText) {
            IndexerUtil.getTokenizedQuery(query, "text", searchText, superFuzzy);
            IndexerUtil.getTokenizedQuery(query, "analyzedText", searchText, superFuzzy);
        }

        for (IndexSearcher searcher : searchers) {
            TopScoreDocCollector collector = TopScoreDocCollector.create(10000, false);
            searcher.search(query, collector);
            ScoreDoc[] hits = collector.topDocs().scoreDocs;

            for (ScoreDoc hit : hits) {
                Document doc = searcher.doc(hit.doc);

                TokenStream stream = TokenSources.getTokenStream("text", doc.get("analyzedText"),
                        new StandardAnalyzer(Version.LUCENE_20.LUCENE_35));
                QueryScorer scorer = new QueryScorer(query, "analyzedText");
                Fragmenter fragmenter = new SimpleSpanFragmenter(scorer, 20);
                Highlighter highlighter = new Highlighter(scorer);
                highlighter.setTextFragmenter(fragmenter);
                String[] fragments = highlighter.getBestFragments(stream, doc.get("text"), 5);
                String highlights = "";

                for (String fragment : fragments) {
                    highlights += fragment + "...";
                }

                if (highlights.equals("")) {
                    String text = doc.get("text");
                    if (text.length() > 100) {
                        highlights += doc.get("text").substring(0, 100);
                    } else {
                        highlights += doc.get("text");
                    }
                }

                cardSnaps.add(new CardSnapshot(highlights, doc));
            }
            searcher.getIndexReader().close();
            searcher.close();
            searcher = null;
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return cardSnaps;
}

From source file:com.gitblit.LuceneExecutor.java

License:Apache License

/**
 * Delete an issue from the repository index.
 * /*from w w w.j a  v a 2  s .  c om*/
 * @param repositoryName
 * @param issueId
 * @throws Exception
 * @return true, if deleted, false if no record was deleted
 */
private boolean deleteIssue(String repositoryName, String issueId) throws Exception {
    BooleanQuery query = new BooleanQuery();
    Term objectTerm = new Term(FIELD_OBJECT_TYPE, SearchObjectType.issue.name());
    query.add(new TermQuery(objectTerm), Occur.MUST);
    Term issueidTerm = new Term(FIELD_ISSUE, issueId);
    query.add(new TermQuery(issueidTerm), Occur.MUST);

    IndexWriter writer = getIndexWriter(repositoryName);
    int numDocsBefore = writer.numDocs();
    writer.deleteDocuments(query);
    writer.commit();
    int numDocsAfter = writer.numDocs();
    if (numDocsBefore == numDocsAfter) {
        logger.debug(MessageFormat.format("no records found to delete {0}", query.toString()));
        return false;
    } else {
        logger.debug(MessageFormat.format("deleted {0} records with {1}", numDocsBefore - numDocsAfter,
                query.toString()));
        return true;
    }
}

From source file:com.gitblit.LuceneExecutor.java

License:Apache License

/**
 * Delete a blob from the specified branch of the repository index.
 * // ww  w . jav a 2  s .c om
 * @param repositoryName
 * @param branch
 * @param path
 * @throws Exception
 * @return true, if deleted, false if no record was deleted
 */
public boolean deleteBlob(String repositoryName, String branch, String path) throws Exception {
    String pattern = MessageFormat.format("{0}:'{'0} AND {1}:\"'{'1'}'\" AND {2}:\"'{'2'}'\"",
            FIELD_OBJECT_TYPE, FIELD_BRANCH, FIELD_PATH);
    String q = MessageFormat.format(pattern, SearchObjectType.blob.name(), branch, path);

    BooleanQuery query = new BooleanQuery();
    StandardAnalyzer analyzer = new StandardAnalyzer(LUCENE_VERSION);
    QueryParser qp = new QueryParser(LUCENE_VERSION, FIELD_SUMMARY, analyzer);
    query.add(qp.parse(q), Occur.MUST);

    IndexWriter writer = getIndexWriter(repositoryName);
    int numDocsBefore = writer.numDocs();
    writer.deleteDocuments(query);
    writer.commit();
    int numDocsAfter = writer.numDocs();
    if (numDocsBefore == numDocsAfter) {
        logger.debug(MessageFormat.format("no records found to delete {0}", query.toString()));
        return false;
    } else {
        logger.debug(MessageFormat.format("deleted {0} records with {1}", numDocsBefore - numDocsAfter,
                query.toString()));
        return true;
    }
}