Search in sources :

Example 96 with BooleanClause

use of org.apache.lucene.search.BooleanClause 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 97 with BooleanClause

use of org.apache.lucene.search.BooleanClause 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 98 with BooleanClause

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

the class QueryElevationComponent method prepare.

//---------------------------------------------------------------------------------
// SearchComponent
//---------------------------------------------------------------------------------
@Override
public void prepare(ResponseBuilder rb) throws IOException {
    SolrQueryRequest req = rb.req;
    SolrParams params = req.getParams();
    // A runtime param can skip 
    if (!params.getBool(QueryElevationParams.ENABLE, true)) {
        return;
    }
    boolean exclusive = params.getBool(QueryElevationParams.EXCLUSIVE, false);
    // A runtime parameter can alter the config value for forceElevation
    boolean force = params.getBool(QueryElevationParams.FORCE_ELEVATION, forceElevation);
    boolean markExcludes = params.getBool(QueryElevationParams.MARK_EXCLUDES, false);
    String boostStr = params.get(QueryElevationParams.IDS);
    String exStr = params.get(QueryElevationParams.EXCLUDE);
    Query query = rb.getQuery();
    SolrParams localParams = rb.getQparser().getLocalParams();
    String qstr = localParams == null ? rb.getQueryString() : localParams.get(QueryParsing.V);
    if (query == null || qstr == null) {
        return;
    }
    ElevationObj booster = null;
    try {
        if (boostStr != null || exStr != null) {
            List<String> boosts = (boostStr != null) ? StrUtils.splitSmart(boostStr, ",", true) : new ArrayList<String>(0);
            List<String> excludes = (exStr != null) ? StrUtils.splitSmart(exStr, ",", true) : new ArrayList<String>(0);
            booster = new ElevationObj(qstr, boosts, excludes);
        } else {
            IndexReader reader = req.getSearcher().getIndexReader();
            qstr = getAnalyzedQuery(qstr);
            booster = getElevationMap(reader, req.getCore()).get(qstr);
        }
    } catch (Exception ex) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error loading elevation", ex);
    }
    if (booster != null) {
        rb.req.getContext().put(BOOSTED, booster.ids);
        rb.req.getContext().put(BOOSTED_PRIORITY, booster.priority);
        // Change the query to insert forced documents
        if (exclusive == true) {
            //we only want these results
            rb.setQuery(new BoostQuery(booster.include, 0f));
        } else {
            BooleanQuery.Builder newq = new BooleanQuery.Builder();
            newq.add(query, BooleanClause.Occur.SHOULD);
            newq.add(new BoostQuery(booster.include, 0f), BooleanClause.Occur.SHOULD);
            if (booster.exclude != null) {
                if (markExcludes == false) {
                    for (TermQuery tq : booster.exclude) {
                        newq.add(new BooleanClause(tq, BooleanClause.Occur.MUST_NOT));
                    }
                } else {
                    //we are only going to mark items as excluded, not actually exclude them.  This works
                    //with the EditorialMarkerFactory
                    rb.req.getContext().put(EXCLUDED, booster.excludeIds);
                }
            }
            rb.setQuery(newq.build());
        }
        ElevationComparatorSource comparator = new ElevationComparatorSource(booster);
        // if the sort is 'score desc' use a custom sorting method to 
        // insert documents in their proper place 
        SortSpec sortSpec = rb.getSortSpec();
        if (sortSpec.getSort() == null) {
            sortSpec.setSortAndFields(new Sort(new SortField[] { new SortField("_elevate_", comparator, true), new SortField(null, SortField.Type.SCORE, false) }), Arrays.asList(new SchemaField[2]));
        } else {
            // Check if the sort is based on score
            SortSpec modSortSpec = this.modifySortSpec(sortSpec, force, comparator);
            if (null != modSortSpec) {
                rb.setSortSpec(modSortSpec);
            }
        }
        // alter the sorting in the grouping specification if there is one
        GroupingSpecification groupingSpec = rb.getGroupingSpec();
        if (groupingSpec != null) {
            SortSpec groupSortSpec = groupingSpec.getGroupSortSpec();
            SortSpec modGroupSortSpec = this.modifySortSpec(groupSortSpec, force, comparator);
            if (modGroupSortSpec != null) {
                groupingSpec.setGroupSortSpec(modGroupSortSpec);
            }
            SortSpec withinGroupSortSpec = groupingSpec.getWithinGroupSortSpec();
            SortSpec modWithinGroupSortSpec = this.modifySortSpec(withinGroupSortSpec, force, comparator);
            if (modWithinGroupSortSpec != null) {
                groupingSpec.setWithinGroupSortSpec(modWithinGroupSortSpec);
            }
        }
    }
    // Add debugging information
    if (rb.isDebug()) {
        List<String> match = null;
        if (booster != null) {
            // Extract the elevated terms into a list
            match = new ArrayList<>(booster.priority.size());
            for (Object o : booster.include.clauses()) {
                TermQuery tq = (TermQuery) ((BooleanClause) o).getQuery();
                match.add(tq.getTerm().text());
            }
        }
        SimpleOrderedMap<Object> dbg = new SimpleOrderedMap<>();
        dbg.add("q", qstr);
        dbg.add("match", match);
        if (rb.isDebugQuery()) {
            rb.addDebugInfo("queryBoosting", dbg);
        }
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) SortField(org.apache.lucene.search.SortField) BoostQuery(org.apache.lucene.search.BoostQuery) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) Sort(org.apache.lucene.search.Sort) GroupingSpecification(org.apache.solr.search.grouping.GroupingSpecification) SolrException(org.apache.solr.common.SolrException) TermQuery(org.apache.lucene.search.TermQuery) XPathExpressionException(javax.xml.xpath.XPathExpressionException) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) BooleanClause(org.apache.lucene.search.BooleanClause) SchemaField(org.apache.solr.schema.SchemaField) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) IndexReader(org.apache.lucene.index.IndexReader) SolrParams(org.apache.solr.common.params.SolrParams) SortSpec(org.apache.solr.search.SortSpec)

Example 99 with BooleanClause

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

the class CommonTermsQueryTest method testRandomIndex.

public void testRandomIndex() throws IOException {
    Directory dir = newDirectory();
    MockAnalyzer analyzer = new MockAnalyzer(random());
    analyzer.setMaxTokenLength(TestUtil.nextInt(random(), 1, IndexWriter.MAX_TERM_LENGTH));
    RandomIndexWriter w = new RandomIndexWriter(random(), dir, analyzer);
    createRandomIndex(atLeast(50), w, random().nextLong());
    w.forceMerge(1);
    DirectoryReader reader = w.getReader();
    LeafReader wrapper = getOnlyLeafReader(reader);
    String field = "body";
    Terms terms = wrapper.terms(field);
    PriorityQueue<TermAndFreq> lowFreqQueue = new PriorityQueue<CommonTermsQueryTest.TermAndFreq>(5) {

        @Override
        protected boolean lessThan(TermAndFreq a, TermAndFreq b) {
            return a.freq > b.freq;
        }
    };
    PriorityQueue<TermAndFreq> highFreqQueue = new PriorityQueue<CommonTermsQueryTest.TermAndFreq>(5) {

        @Override
        protected boolean lessThan(TermAndFreq a, TermAndFreq b) {
            return a.freq < b.freq;
        }
    };
    try {
        TermsEnum iterator = terms.iterator();
        while (iterator.next() != null) {
            if (highFreqQueue.size() < 5) {
                highFreqQueue.add(new TermAndFreq(BytesRef.deepCopyOf(iterator.term()), iterator.docFreq()));
                lowFreqQueue.add(new TermAndFreq(BytesRef.deepCopyOf(iterator.term()), iterator.docFreq()));
            } else {
                if (highFreqQueue.top().freq < iterator.docFreq()) {
                    highFreqQueue.top().freq = iterator.docFreq();
                    highFreqQueue.top().term = BytesRef.deepCopyOf(iterator.term());
                    highFreqQueue.updateTop();
                }
                if (lowFreqQueue.top().freq > iterator.docFreq()) {
                    lowFreqQueue.top().freq = iterator.docFreq();
                    lowFreqQueue.top().term = BytesRef.deepCopyOf(iterator.term());
                    lowFreqQueue.updateTop();
                }
            }
        }
        int lowFreq = lowFreqQueue.top().freq;
        int highFreq = highFreqQueue.top().freq;
        assumeTrue("unlucky index", highFreq - 1 > lowFreq);
        List<TermAndFreq> highTerms = queueToList(highFreqQueue);
        List<TermAndFreq> lowTerms = queueToList(lowFreqQueue);
        IndexSearcher searcher = newSearcher(reader);
        Occur lowFreqOccur = randomOccur(random());
        BooleanQuery.Builder verifyQuery = new BooleanQuery.Builder();
        CommonTermsQuery cq = new CommonTermsQuery(randomOccur(random()), lowFreqOccur, highFreq - 1);
        for (TermAndFreq termAndFreq : lowTerms) {
            cq.add(new Term(field, termAndFreq.term));
            verifyQuery.add(new BooleanClause(new TermQuery(new Term(field, termAndFreq.term)), lowFreqOccur));
        }
        for (TermAndFreq termAndFreq : highTerms) {
            cq.add(new Term(field, termAndFreq.term));
        }
        TopDocs cqSearch = searcher.search(cq, reader.maxDoc());
        TopDocs verifySearch = searcher.search(verifyQuery.build(), reader.maxDoc());
        assertEquals(verifySearch.totalHits, cqSearch.totalHits);
        Set<Integer> hits = new HashSet<>();
        for (ScoreDoc doc : verifySearch.scoreDocs) {
            hits.add(doc.doc);
        }
        for (ScoreDoc doc : cqSearch.scoreDocs) {
            assertTrue(hits.remove(doc.doc));
        }
        assertTrue(hits.isEmpty());
        /*
       *  need to force merge here since QueryUtils adds checks based
       *  on leave readers which have different statistics than the top
       *  level reader if we have more than one segment. This could 
       *  result in a different query / results.
       */
        w.forceMerge(1);
        DirectoryReader reader2 = w.getReader();
        QueryUtils.check(random(), cq, newSearcher(reader2));
        reader2.close();
    } finally {
        IOUtils.close(reader, w, dir, analyzer);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) Occur(org.apache.lucene.search.BooleanClause.Occur) TermsEnum(org.apache.lucene.index.TermsEnum) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Directory(org.apache.lucene.store.Directory) HashSet(java.util.HashSet) TermQuery(org.apache.lucene.search.TermQuery) LeafReader(org.apache.lucene.index.LeafReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) Terms(org.apache.lucene.index.Terms) Term(org.apache.lucene.index.Term) PriorityQueue(org.apache.lucene.util.PriorityQueue) BooleanClause(org.apache.lucene.search.BooleanClause) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Example 100 with BooleanClause

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

the class MoreLikeThisQuery method rewrite.

@Override
public Query rewrite(IndexReader reader) throws IOException {
    MoreLikeThis mlt = new MoreLikeThis(reader);
    mlt.setFieldNames(moreLikeFields);
    mlt.setAnalyzer(analyzer);
    mlt.setMinTermFreq(minTermFrequency);
    if (minDocFreq >= 0) {
        mlt.setMinDocFreq(minDocFreq);
    }
    mlt.setMaxQueryTerms(maxQueryTerms);
    mlt.setStopWords(stopWords);
    BooleanQuery bq = (BooleanQuery) mlt.like(fieldName, new StringReader(likeText));
    BooleanQuery.Builder newBq = new BooleanQuery.Builder();
    for (BooleanClause clause : bq) {
        newBq.add(clause);
    }
    //make at least half the terms match
    newBq.setMinimumNumberShouldMatch((int) (bq.clauses().size() * percentTermsToMatch));
    return newBq.build();
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) StringReader(java.io.StringReader)

Aggregations

BooleanClause (org.apache.lucene.search.BooleanClause)102 BooleanQuery (org.apache.lucene.search.BooleanQuery)92 Query (org.apache.lucene.search.Query)56 TermQuery (org.apache.lucene.search.TermQuery)46 Term (org.apache.lucene.index.Term)44 BoostQuery (org.apache.lucene.search.BoostQuery)33 ArrayList (java.util.ArrayList)23 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)21 PhraseQuery (org.apache.lucene.search.PhraseQuery)20 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)19 SynonymQuery (org.apache.lucene.search.SynonymQuery)18 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)18 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)17 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)16 SpanQuery (org.apache.lucene.search.spans.SpanQuery)15 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)14 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)14 WildcardQuery (org.apache.lucene.search.WildcardQuery)14 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)13 PrefixQuery (org.apache.lucene.search.PrefixQuery)13