Example usage for org.apache.lucene.queryparser.classic QueryParser setAutoGeneratePhraseQueries

List of usage examples for org.apache.lucene.queryparser.classic QueryParser setAutoGeneratePhraseQueries

Introduction

In this page you can find the example usage for org.apache.lucene.queryparser.classic QueryParser setAutoGeneratePhraseQueries.

Prototype

@Override
public void setAutoGeneratePhraseQueries(boolean value) 

Source Link

Document

Set to true if phrase queries will be automatically generated when the analyzer returns more than one term from whitespace delimited text.

Usage

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

License:Apache License

/**
 * ???SWMC?/* www . ja  va 2 s .  co  m*/
 * @param fieldName
 * @param pathOption
 * @param quickMode
 * @return
 */
private static Query getSWMCQuery(String fieldName, List<Lexeme> lexemes, boolean quickMode) {
    //SWMC?
    StringBuffer keywordBuffer = new StringBuffer();
    //SWMC?
    StringBuffer keywordBuffer_Short = new StringBuffer();
    //??
    int lastLexemeLength = 0;
    //????
    int lastLexemeEnd = -1;

    int shortCount = 0;
    int totalCount = 0;
    for (Lexeme l : lexemes) {
        totalCount += l.getLength();
        //?
        if (l.getLength() > 1) {
            keywordBuffer_Short.append(' ').append(l.getLexemeText());
            shortCount += l.getLength();
        }

        if (lastLexemeLength == 0) {
            keywordBuffer.append(l.getLexemeText());
        } else if (lastLexemeLength == 1 && l.getLength() == 1 && lastLexemeEnd == l.getBeginPosition()) {//???)
            keywordBuffer.append(l.getLexemeText());
        } else {
            keywordBuffer.append(' ').append(l.getLexemeText());

        }
        lastLexemeLength = l.getLength();
        lastLexemeEnd = l.getEndPosition();
    }

    //lucene queryparser ?SWMC Query
    QueryParser qp = new QueryParser(Version.LUCENE_48, fieldName, new StandardAnalyzer(Version.LUCENE_48));
    qp.setDefaultOperator(QueryParser.AND_OPERATOR);
    qp.setAutoGeneratePhraseQueries(true);

    if (quickMode && (shortCount * 1.0f / totalCount) > 0.5f) {
        try {
            //System.out.println(keywordBuffer.toString());
            Query q = qp.parse(keywordBuffer_Short.toString());
            return q;
        } catch (ParseException e) {
            e.printStackTrace();
        }

    } else {
        if (keywordBuffer.length() > 0) {
            try {
                //System.out.println(keywordBuffer.toString());
                Query q = qp.parse(keywordBuffer.toString());
                return q;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:com.github.buzztaiki.lucene.lastuni.CJKSingleCharQueryTest.java

License:Apache License

private QueryParser newQueryParser(Analyzer analyzer) {
    // TODO: use flexible parser?
    QueryParser qp = new CJKSingleCharSupportQueryParser(TEST_VERSION_CURRENT, "content", analyzer);
    qp.setDefaultOperator(QueryParser.AND_OPERATOR);
    qp.setAutoGeneratePhraseQueries(true);
    qp.setAllowLeadingWildcard(true);/*from   ww  w.  jav  a  2 s.c  o m*/
    return qp;
}

From source file:com.lorelib.analyzer.query.SWMCQueryBuilder.java

License:Apache License

/**
 * ???SWMC?//from  w  w  w .  j  ava2 s  . com
 * @param fieldName
 * @param pathOption
 * @param quickMode
 * @return
 */
private static Query getSWMCQuery(String fieldName, List<Lexeme> lexemes, boolean quickMode) {
    //SWMC?
    StringBuffer keywordBuffer = new StringBuffer();
    //SWMC?
    StringBuffer keywordBuffer_Short = new StringBuffer();
    //??
    int lastLexemeLength = 0;
    //????
    int lastLexemeEnd = -1;

    int shortCount = 0;
    int totalCount = 0;
    for (Lexeme l : lexemes) {
        totalCount += l.getLength();
        //?
        if (l.getLength() > 1) {
            keywordBuffer_Short.append(' ').append(l.getLexemeText());
            shortCount += l.getLength();
        }

        if (lastLexemeLength == 0) {
            keywordBuffer.append(l.getLexemeText());
        } else if (lastLexemeLength == 1 && l.getLength() == 1 && lastLexemeEnd == l.getBeginPosition()) {//???)
            keywordBuffer.append(l.getLexemeText());
        } else {
            keywordBuffer.append(' ').append(l.getLexemeText());

        }
        lastLexemeLength = l.getLength();
        lastLexemeEnd = l.getEndPosition();
    }

    //lucene queryparser ?SWMC Query
    QueryParser qp = new QueryParser(fieldName, new StandardAnalyzer());
    qp.setDefaultOperator(QueryParser.AND_OPERATOR);
    qp.setAutoGeneratePhraseQueries(true);

    if (quickMode && (shortCount * 1.0f / totalCount) > 0.5f) {
        try {
            //System.out.println(keywordBuffer.toString());
            Query q = qp.parse(keywordBuffer_Short.toString());
            return q;
        } catch (ParseException e) {
            e.printStackTrace();
        }

    } else {
        if (keywordBuffer.length() > 0) {
            try {
                //System.out.println(keywordBuffer.toString());
                Query q = qp.parse(keywordBuffer.toString());
                return q;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:net.simpleframework.ado.lucene.AbstractLuceneManager.java

License:Apache License

protected Query getQuery(final String domain, final String[] queryFields, final String queryString) {
    Query query = null;/*from   w w w.j a v a 2s .  c o m*/
    QueryParser qp;
    if (StringUtils.hasText(queryString) && indexExists()
            && (qp = new MultiFieldQueryParser(queryFields, getDefaultAnalyzer())) != null) {
        try {
            // qp.setSplitOnWhitespace(true);
            qp.setAutoGeneratePhraseQueries(true);
            query = qp.parse(queryString.trim());
        } catch (final ParseException e) {
            getLog().warn(e);
        }
    }
    return query;
}

From source file:org.wltea.analyzer.query.SWMCQueryBuilder.java

License:Apache License

/**
 * ???SWMC?//from   w  ww . jav  a 2  s  .co  m
 * @param fieldName
//    * @param pathOption
 * @param quickMode
 * @return
 */
private static Query getSWMCQuery(String fieldName, List<Lexeme> lexemes, boolean quickMode) {
    //SWMC?
    StringBuffer keywordBuffer = new StringBuffer();
    //SWMC?
    StringBuffer keywordBuffer_Short = new StringBuffer();
    //??
    int lastLexemeLength = 0;
    //????
    int lastLexemeEnd = -1;

    int shortCount = 0;
    int totalCount = 0;
    for (Lexeme l : lexemes) {
        totalCount += l.getLength();
        //?
        if (l.getLength() > 1) {
            keywordBuffer_Short.append(' ').append(l.getLexemeText());
            shortCount += l.getLength();
        }

        if (lastLexemeLength == 0) {
            keywordBuffer.append(l.getLexemeText());
        } else if (lastLexemeLength == 1 && l.getLength() == 1 && lastLexemeEnd == l.getBeginPosition()) {//???)
            keywordBuffer.append(l.getLexemeText());
        } else {
            keywordBuffer.append(' ').append(l.getLexemeText());

        }
        lastLexemeLength = l.getLength();
        lastLexemeEnd = l.getEndPosition();
    }

    //lucene queryparser ?SWMC Query
    QueryParser qp = new QueryParser(Version.LUCENE_40, fieldName, new StandardAnalyzer(Version.LUCENE_40));
    qp.setDefaultOperator(QueryParser.AND_OPERATOR);
    qp.setAutoGeneratePhraseQueries(true);

    if (quickMode && (shortCount * 1.0f / totalCount) > 0.5f) {
        try {
            //System.out.println(keywordBuffer.toString());
            Query q = qp.parse(keywordBuffer_Short.toString());
            return q;
        } catch (ParseException e) {
            e.printStackTrace();
        }

    } else {
        if (keywordBuffer.length() > 0) {
            try {
                //System.out.println(keywordBuffer.toString());
                Query q = qp.parse(keywordBuffer.toString());
                return q;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:org.wltea.analyzer.query.SWMCQueryBuilderP.java

License:Apache License

/**
 * ???SWMC?/*from   w  w  w. j  a  v  a2s .  co m*/
 * @param fieldName
//    * @param pathOption
 * @param quickMode
 * @return
 */
private static Query getSWMCQuery(String fieldName, List<LexemeP> lexemes, boolean quickMode) {
    //SWMC?
    StringBuffer keywordBuffer = new StringBuffer();
    //SWMC?
    StringBuffer keywordBuffer_Short = new StringBuffer();
    //??
    int lastLexemeLength = 0;
    //????
    int lastLexemeEnd = -1;

    int shortCount = 0;
    int totalCount = 0;
    for (LexemeP l : lexemes) {
        totalCount += l.getLength();
        //?
        if (l.getLength() > 1) {
            keywordBuffer_Short.append(' ').append(l.getLexemeText());
            shortCount += l.getLength();
        }

        if (lastLexemeLength == 0) {
            keywordBuffer.append(l.getLexemeText());
        } else if (lastLexemeLength == 1 && l.getLength() == 1 && lastLexemeEnd == l.getBeginPosition()) {//???)
            keywordBuffer.append(l.getLexemeText());
        } else {
            keywordBuffer.append(' ').append(l.getLexemeText());

        }
        lastLexemeLength = l.getLength();
        lastLexemeEnd = l.getEndPosition();
    }

    //lucene queryparser ?SWMC Query
    QueryParser qp = new QueryParser(Version.LUCENE_40, fieldName, new StandardAnalyzer(Version.LUCENE_40));
    qp.setDefaultOperator(QueryParser.AND_OPERATOR);
    qp.setAutoGeneratePhraseQueries(true);

    if (quickMode && (shortCount * 1.0f / totalCount) > 0.5f) {
        try {
            //System.out.println(keywordBuffer.toString());
            Query q = qp.parse(keywordBuffer_Short.toString());
            return q;
        } catch (ParseException e) {
            e.printStackTrace();
        }

    } else {
        if (keywordBuffer.length() > 0) {
            try {
                //System.out.println(keywordBuffer.toString());
                Query q = qp.parse(keywordBuffer.toString());
                return q;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}