Search in sources :

Example 86 with BoostQuery

use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.

the class TermQueryBuilder method getQuery.

@Override
public Query getQuery(Element e) throws ParserException {
    String field = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
    String value = DOMUtils.getNonBlankTextOrFail(e);
    Query tq = new TermQuery(new Term(field, value));
    float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
    if (boost != 1f) {
        tq = new BoostQuery(tq, boost);
    }
    return tq;
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 87 with BoostQuery

use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.

the class TermsQueryBuilder method getQuery.

@Override
public Query getQuery(Element e) throws ParserException {
    String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
    String text = DOMUtils.getNonBlankTextOrFail(e);
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.setMinimumNumberShouldMatch(DOMUtils.getAttribute(e, "minimumNumberShouldMatch", 0));
    try (TokenStream ts = analyzer.tokenStream(fieldName, text)) {
        TermToBytesRefAttribute termAtt = ts.addAttribute(TermToBytesRefAttribute.class);
        Term term = null;
        ts.reset();
        while (ts.incrementToken()) {
            term = new Term(fieldName, BytesRef.deepCopyOf(termAtt.getBytesRef()));
            bq.add(new BooleanClause(new TermQuery(term), BooleanClause.Occur.SHOULD));
        }
        ts.end();
    } catch (IOException ioe) {
        throw new RuntimeException("Error constructing terms from index:" + ioe);
    }
    Query q = bq.build();
    float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
    return new BoostQuery(q, boost);
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) TokenStream(org.apache.lucene.analysis.TokenStream) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) QueryBuilder(org.apache.lucene.queryparser.xml.QueryBuilder) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) BoostQuery(org.apache.lucene.search.BoostQuery) BooleanClause(org.apache.lucene.search.BooleanClause) TermToBytesRefAttribute(org.apache.lucene.analysis.tokenattributes.TermToBytesRefAttribute)

Example 88 with BoostQuery

use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.

the class BooleanQueryBuilder method getQuery.

/* (non-Javadoc)
    * @see org.apache.lucene.xmlparser.QueryObjectBuilder#process(org.w3c.dom.Element)
    */
@Override
public Query getQuery(Element e) throws ParserException {
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.setMinimumNumberShouldMatch(DOMUtils.getAttribute(e, "minimumNumberShouldMatch", 0));
    NodeList nl = e.getChildNodes();
    final int nlLen = nl.getLength();
    for (int i = 0; i < nlLen; i++) {
        Node node = nl.item(i);
        if (node.getNodeName().equals("Clause")) {
            Element clauseElem = (Element) node;
            BooleanClause.Occur occurs = getOccursValue(clauseElem);
            Element clauseQuery = DOMUtils.getFirstChildOrFail(clauseElem);
            Query q = factory.getQuery(clauseQuery);
            bq.add(new BooleanClause(q, occurs));
        }
    }
    Query q = bq.build();
    float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
    if (boost != 1f) {
        q = new BoostQuery(q, boost);
    }
    return q;
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) QueryBuilder(org.apache.lucene.queryparser.xml.QueryBuilder) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 89 with BoostQuery

use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.

the class BoostingQueryBuilder method getQuery.

@Override
public Query getQuery(Element e) throws ParserException {
    Element mainQueryElem = DOMUtils.getChildByTagOrFail(e, "Query");
    mainQueryElem = DOMUtils.getFirstChildOrFail(mainQueryElem);
    Query mainQuery = factory.getQuery(mainQueryElem);
    Element boostQueryElem = DOMUtils.getChildByTagOrFail(e, "BoostQuery");
    float boost = DOMUtils.getAttribute(boostQueryElem, "boost", DEFAULT_BOOST);
    boostQueryElem = DOMUtils.getFirstChildOrFail(boostQueryElem);
    Query boostQuery = factory.getQuery(boostQueryElem);
    Query bq = new BoostingQuery(mainQuery, boostQuery, boost);
    boost = DOMUtils.getAttribute(e, "boost", 1.0f);
    if (boost != 1f) {
        return new BoostQuery(bq, boost);
    }
    return bq;
}
Also used : Query(org.apache.lucene.search.Query) BoostingQuery(org.apache.lucene.queries.BoostingQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Element(org.w3c.dom.Element) BoostingQuery(org.apache.lucene.queries.BoostingQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 90 with BoostQuery

use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.

the class ConstantScoreQueryBuilder method getQuery.

@Override
public Query getQuery(Element e) throws ParserException {
    Element queryElem = DOMUtils.getFirstChildOrFail(e);
    Query q = new ConstantScoreQuery(queryFactory.getQuery(queryElem));
    float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
    if (boost != 1f) {
        q = new BoostQuery(q, boost);
    }
    return q;
}
Also used : Query(org.apache.lucene.search.Query) BoostQuery(org.apache.lucene.search.BoostQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Element(org.w3c.dom.Element) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

Aggregations

BoostQuery (org.apache.lucene.search.BoostQuery)128 Query (org.apache.lucene.search.Query)107 BooleanQuery (org.apache.lucene.search.BooleanQuery)96 TermQuery (org.apache.lucene.search.TermQuery)84 Term (org.apache.lucene.index.Term)54 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)45 PhraseQuery (org.apache.lucene.search.PhraseQuery)35 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)32 PrefixQuery (org.apache.lucene.search.PrefixQuery)29 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)27 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)24 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)23 BooleanClause (org.apache.lucene.search.BooleanClause)20 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)20 SynonymQuery (org.apache.lucene.search.SynonymQuery)19 WildcardQuery (org.apache.lucene.search.WildcardQuery)19 ArrayList (java.util.ArrayList)18 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)18 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)17 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)16