Package org.apache.lucene.search

Examples of org.apache.lucene.search.BooleanQuery


      return newTermQuery(new Term(field, term));
    } else {
      if (severalTokensAtSamePosition) {
        if (positionCount == 1) {
          // no phrase query:
          BooleanQuery q = newBooleanQuery(true);
          for (int i = 0; i < numTokens; i++) {
            String term = null;
            try {
              boolean hasNext = buffer.incrementToken();
              assert hasNext == true;
              term = termAtt.term();
            } catch (IOException e) {
              // safe to ignore, because we know the number of tokens
            }

            Query currentQuery = newTermQuery(
                new Term(field, term));
            q.add(currentQuery, BooleanClause.Occur.SHOULD);
          }
          return q;
        }
        else {
          // phrase query:
View Full Code Here


  * Builds a new BooleanQuery instance
  * @param disableCoord disable coord
  * @return new BooleanQuery instance
  */
  protected BooleanQuery newBooleanQuery(boolean disableCoord) {
    return new BooleanQuery(disableCoord);
  }
View Full Code Here

    throws ParseException
  {
    if (clauses.size()==0) {
      return null; // all clause words were filtered away by the analyzer.
    }
    BooleanQuery query = newBooleanQuery(disableCoord);
    for(final BooleanClause clause: clauses) {
      query.add(clause);
    }
    return query;
  }
View Full Code Here

  // this mean that every term boost factor will be multiplied by this
  // number
  float boostFactor = 5;
  mlt.setBoostFactor(boostFactor);

  BooleanQuery query = (BooleanQuery) mlt.like(new StringReader(
    "lucene release"));
  List clauses = query.clauses();

  assertEquals("Expected " + originalValues.size() + " clauses.",
    originalValues.size(), clauses.size());

  for (int i = 0; i < clauses.size(); i++) {
View Full Code Here

  mlt.setMinDocFreq(1);
  mlt.setMinTermFreq(1);
  mlt.setMinWordLen(1);
  mlt.setFieldNames(new String[] { "text" });
  mlt.setBoost(true);
  BooleanQuery query = (BooleanQuery) mlt.like(new StringReader(
    "lucene release"));
  List clauses = query.clauses();

  for (int i = 0; i < clauses.size(); i++) {
      BooleanClause clause = (BooleanClause) clauses.get(i);
      TermQuery tq = (TermQuery) clause.getQuery();
      originalValues.put(tq.getTerm().text(), Float.valueOf(tq.getBoost()));
View Full Code Here

    while (ts.incrementToken()) {
      String word = termAtt.term();
      if ( already.add( word))
        top.add( word);
    }
    final BooleanQuery tmp = new BooleanQuery();
   
    // [2] form query
    Iterator<String> it = top.iterator();
    while ( it.hasNext())
    {
      // [2a] add to level words in
      String word = (String) it.next();
      TermQuery tq = new TermQuery( new Term( field, word));
      tmp.add( tq, BooleanClause.Occur.SHOULD);

      syns.search(new TermQuery( new Term(Syns2Index.F_WORD, word)), new Collector() {
        IndexReader reader;
       
        @Override
        public boolean acceptsDocsOutOfOrder() {
          return true;
        }

        @Override
        public void collect(int doc) throws IOException {
          Document d = reader.document(doc);
          String[] values = d.getValues( Syns2Index.F_SYN);
          for ( int j = 0; j < values.length; j++)
          {
            String syn = values[ j];
            if ( already.add( syn)) // avoid dups of top level words and synonyms
            {
              TermQuery tq = new TermQuery( new Term( field, syn));
              if ( boost > 0) // else keep normal 1.0
                tq.setBoost( boost);
              tmp.add( tq, BooleanClause.Occur.SHOULD);
            }
          }
        }

        @Override
View Full Code Here

    while (ts.incrementToken()) {
      String word = termAtt.term();
      if ( already.add( word))
        top.add( word);
    }
    final BooleanQuery tmp = new BooleanQuery();
   
    // [2] form query
    Iterator<String> it = top.iterator();
    while ( it.hasNext())
    {
      // [2a] add to level words in
      String word = it.next();
      TermQuery tq = new TermQuery( new Term( field, word));
      tmp.add( tq, BooleanClause.Occur.SHOULD);

      // [2b] add in unique synonums
      syns.search(new TermQuery( new Term(Syns2Index.F_WORD, word)), new Collector() {
        IndexReader reader;
       
        @Override
        public boolean acceptsDocsOutOfOrder() {
          return true;
        }

        @Override
        public void collect(int doc) throws IOException {
          Document d = reader.document(doc);
          String[] values = d.getValues( Syns2Index.F_SYN);
          for ( int j = 0; j < values.length; j++)
          {
            String syn = values[ j];
            if ( already.add( syn))
            {
              TermQuery tq = new TermQuery( new Term( field, syn));
              if ( boost > 0) // else keep normal 1.0
                tq.setBoost( boost);
              tmp.add( tq, BooleanClause.Occur.SHOULD);
            }
          }
        }

        @Override
View Full Code Here

        new SpanTermQuery(new Term(FIELD_NAME, "wordx")),
        new SpanTermQuery(new Term(FIELD_NAME, "wordy")) }, 1, false);
    Query query2 = new SpanNearQuery(new SpanQuery[] {
        new SpanTermQuery(new Term(FIELD_NAME, "wordy")),
        new SpanTermQuery(new Term(FIELD_NAME, "wordc")) }, 1, false);
    BooleanQuery bquery = new BooleanQuery();
    bquery.add(query1, Occur.SHOULD);
    bquery.add(query2, Occur.SHOULD);
    doSearching(bquery);
    TestHighlightRunner helper = new TestHighlightRunner() {

      @Override
      public void run() throws Exception {
View Full Code Here

    queryTest(new TermQuery(new Term("f3", "v1")));
    queryTest(new TermQuery(new Term("f3", "v2")));
    queryTest(new TermQuery(new Term("f4", "v1")));
    queryTest(new TermQuery(new Term("f4", "v2")));

    BooleanQuery bq1 = new BooleanQuery();
    bq1.add(new TermQuery(new Term("f1", "v1")), Occur.MUST);
    bq1.add(new TermQuery(new Term("f4", "v1")), Occur.MUST);
    queryTest(bq1);
  }
View Full Code Here

  public void testMatchAllDocs() throws Exception {
    QueryParserWrapper qp = new QueryParserWrapper("field",
        new WhitespaceAnalyzer());
    assertEquals(new MatchAllDocsQuery(), qp.parse("*:*"));
    assertEquals(new MatchAllDocsQuery(), qp.parse("(*:*)"));
    BooleanQuery bq = (BooleanQuery) qp.parse("+*:* -*:*");
    assertTrue(bq.getClauses()[0].getQuery() instanceof MatchAllDocsQuery);
    assertTrue(bq.getClauses()[1].getQuery() instanceof MatchAllDocsQuery);
  }
View Full Code Here

TOP

Related Classes of org.apache.lucene.search.BooleanQuery

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.