Search in sources :

Example 71 with SpanQuery

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

the class SpanFirstBuilder method getSpanQuery.

@Override
public SpanQuery getSpanQuery(Element e) throws ParserException {
    int end = DOMUtils.getAttribute(e, "end", 1);
    Element child = DOMUtils.getFirstChildElement(e);
    SpanQuery q = factory.getSpanQuery(child);
    SpanFirstQuery sfq = new SpanFirstQuery(q, end);
    float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
    return new SpanBoostQuery(sfq, boost);
}
Also used : SpanFirstQuery(org.apache.lucene.search.spans.SpanFirstQuery) Element(org.w3c.dom.Element) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 72 with SpanQuery

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

the class SpanNearBuilder method getSpanQuery.

@Override
public SpanQuery getSpanQuery(Element e) throws ParserException {
    String slopString = DOMUtils.getAttributeOrFail(e, "slop");
    int slop = Integer.parseInt(slopString);
    boolean inOrder = DOMUtils.getAttribute(e, "inOrder", false);
    List<SpanQuery> spans = new ArrayList<>();
    for (Node kid = e.getFirstChild(); kid != null; kid = kid.getNextSibling()) {
        if (kid.getNodeType() == Node.ELEMENT_NODE) {
            spans.add(factory.getSpanQuery((Element) kid));
        }
    }
    SpanQuery[] spanQueries = spans.toArray(new SpanQuery[spans.size()]);
    SpanQuery snq = new SpanNearQuery(spanQueries, slop, inOrder);
    float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
    return new SpanBoostQuery(snq, boost);
}
Also used : Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) ArrayList(java.util.ArrayList) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 73 with SpanQuery

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

the class SpanOrBuilder method getSpanQuery.

@Override
public SpanQuery getSpanQuery(Element e) throws ParserException {
    List<SpanQuery> clausesList = new ArrayList<>();
    for (Node kid = e.getFirstChild(); kid != null; kid = kid.getNextSibling()) {
        if (kid.getNodeType() == Node.ELEMENT_NODE) {
            SpanQuery clause = factory.getSpanQuery((Element) kid);
            clausesList.add(clause);
        }
    }
    SpanQuery[] clauses = clausesList.toArray(new SpanQuery[clausesList.size()]);
    SpanOrQuery soq = new SpanOrQuery(clauses);
    float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
    return new SpanBoostQuery(soq, boost);
}
Also used : Node(org.w3c.dom.Node) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) ArrayList(java.util.ArrayList) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 74 with SpanQuery

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

the class SpanOrTermsBuilder method getSpanQuery.

@Override
public SpanQuery getSpanQuery(Element e) throws ParserException {
    String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
    String value = DOMUtils.getNonBlankTextOrFail(e);
    List<SpanQuery> clausesList = new ArrayList<>();
    try (TokenStream ts = analyzer.tokenStream(fieldName, value)) {
        TermToBytesRefAttribute termAtt = ts.addAttribute(TermToBytesRefAttribute.class);
        ts.reset();
        while (ts.incrementToken()) {
            SpanTermQuery stq = new SpanTermQuery(new Term(fieldName, BytesRef.deepCopyOf(termAtt.getBytesRef())));
            clausesList.add(stq);
        }
        ts.end();
        SpanOrQuery soq = new SpanOrQuery(clausesList.toArray(new SpanQuery[clausesList.size()]));
        float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
        return new SpanBoostQuery(soq, boost);
    } catch (IOException ioe) {
        throw new ParserException("IOException parsing value:" + value);
    }
}
Also used : ParserException(org.apache.lucene.queryparser.xml.ParserException) TokenStream(org.apache.lucene.analysis.TokenStream) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermToBytesRefAttribute(org.apache.lucene.analysis.tokenattributes.TermToBytesRefAttribute) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery)

Example 75 with SpanQuery

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

the class SpanNearClauseFactory method addSpanQuery.

public void addSpanQuery(Query q) {
    if (q.getClass() == MatchNoDocsQuery.class)
        return;
    if (!(q instanceof SpanQuery))
        throw new AssertionError("Expected SpanQuery: " + q.toString(getFieldName()));
    float boost = 1f;
    if (q instanceof SpanBoostQuery) {
        SpanBoostQuery bq = (SpanBoostQuery) q;
        boost = bq.getBoost();
        q = bq.getQuery();
    }
    addSpanQueryWeighted((SpanQuery) q, boost);
}
Also used : SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Aggregations

SpanQuery (org.apache.lucene.search.spans.SpanQuery)81 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)53 Term (org.apache.lucene.index.Term)51 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)43 Query (org.apache.lucene.search.Query)27 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)25 IndexReader (org.apache.lucene.index.IndexReader)18 SpanBoostQuery (org.apache.lucene.search.spans.SpanBoostQuery)18 Document (org.apache.lucene.document.Document)17 IndexSearcher (org.apache.lucene.search.IndexSearcher)16 BooleanQuery (org.apache.lucene.search.BooleanQuery)15 TopDocs (org.apache.lucene.search.TopDocs)15 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)13 TermQuery (org.apache.lucene.search.TermQuery)13 ArrayList (java.util.ArrayList)12 Spans (org.apache.lucene.search.spans.Spans)12 PhraseQuery (org.apache.lucene.search.PhraseQuery)11 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)10 SpanNotQuery (org.apache.lucene.search.spans.SpanNotQuery)10 Directory (org.apache.lucene.store.Directory)10