Package org.apache.lucene.search

Examples of org.apache.lucene.search.BooleanQuery


      throw new IllegalArgumentException("parameter \"queries\" is null");
    }       
    if (queries.length != fields.length){
          throw new IllegalArgumentException("queries.length != fields.length");
    }
    BooleanQuery resultQuery = new BooleanQuery();   
    for(int i = 0; i < fields.length; i++){
      if(fields[i] != null){
        Query partQuery = parse(fields[i] , queries[i]);
        if(partQuery != null &&
                  (!(partQuery instanceof BooleanQuery) || ((BooleanQuery)partQuery).getClauses().length>0)){
          resultQuery.add(partQuery, Occur.SHOULD);
        }
      }     
    }   
    return resultQuery;
  }
View Full Code Here


   
      if (!(queries.length == fields.length && queries.length == flags.length)){
          throw new IllegalArgumentException("queries, fields, and flags array have have different length");
      }

      BooleanQuery resultQuery = new BooleanQuery();   
    for(int i = 0; i < fields.length; i++){
      if(fields[i] != null){
        Query partQuery = parse(fields[i] , queries[i]);
        if(partQuery != null &&
                  (!(partQuery instanceof BooleanQuery) || ((BooleanQuery)partQuery).getClauses().length>0)){
          resultQuery.add(partQuery, flags[i]);
        }
      }     
    }   
    return resultQuery;
 
View Full Code Here

          Query onlyOneQuery = optimizeQueries(acceptedBranchs.get(0).toQueries(fieldName));
          if(onlyOneQuery != null){
            queries.add(onlyOneQuery);
          }         
        }else{
          BooleanQuery orQuery = new BooleanQuery();
          for(TokenBranch childBranch : acceptedBranchs){
            Query childQuery = optimizeQueries(childBranch.toQueries(fieldName));
            if(childQuery != null){
              orQuery.add(childQuery, Occur.SHOULD);
            }
          }
          if(orQuery.getClauses().length > 0){
            queries.add(orQuery);
          }
        }
      }     
      //生成nextBranch的query
View Full Code Here

    private Query toQuery(Element op){
      if(this.querys.size() == 0){
        return null;
      }
     
      BooleanQuery resultQuery = new BooleanQuery();

      if(this.querys.size() == 1){
        return this.querys.get(0);
      }
     
      Query q2 = this.querys.pop();
      Query q1 = this.querys.pop();
      if('&' == op.type){
        if(q1 != null){
          if(q1 instanceof BooleanQuery){
            BooleanClause[] clauses = ((BooleanQuery)q1).getClauses();
            if(clauses.length > 0
                && clauses[0].getOccur() == Occur.MUST){
              for(BooleanClause c : clauses){
                resultQuery.add(c);
              }         
            }else{
              resultQuery.add(q1,Occur.MUST);
            }

          }else{
            //q1 instanceof TermQuery
            //q1 instanceof TermRangeQuery
            //q1 instanceof PhraseQuery
            //others
            resultQuery.add(q1,Occur.MUST);
          }
        }
       
        if(q2 != null){
          if(q2 instanceof BooleanQuery){
            BooleanClause[] clauses = ((BooleanQuery)q2).getClauses();
            if(clauses.length > 0
                && clauses[0].getOccur() == Occur.MUST){
              for(BooleanClause c : clauses){
                resultQuery.add(c);
              }         
            }else{
              resultQuery.add(q2,Occur.MUST);
            }
           
          }else{
            //q1 instanceof TermQuery
            //q1 instanceof TermRangeQuery
            //q1 instanceof PhraseQuery
            //others
            resultQuery.add(q2,Occur.MUST);
          }
        }
       
      }else if('|' == op.type){
        if(q1 != null){
          if(q1 instanceof BooleanQuery){
            BooleanClause[] clauses = ((BooleanQuery)q1).getClauses();
            if(clauses.length > 0
                && clauses[0].getOccur() == Occur.SHOULD){
              for(BooleanClause c : clauses){
                resultQuery.add(c);
              }         
            }else{
              resultQuery.add(q1,Occur.SHOULD);
            }
           
          }else{
            //q1 instanceof TermQuery
            //q1 instanceof TermRangeQuery
            //q1 instanceof PhraseQuery
            //others
            resultQuery.add(q1,Occur.SHOULD);
          }
        }
       
        if(q2 != null){
          if(q2 instanceof BooleanQuery){
            BooleanClause[] clauses = ((BooleanQuery)q2).getClauses();
            if(clauses.length > 0
                && clauses[0].getOccur() == Occur.SHOULD){
              for(BooleanClause c : clauses){
                resultQuery.add(c);
              }         
            }else{
              resultQuery.add(q2,Occur.SHOULD);
            }
          }else{
            //q2 instanceof TermQuery
            //q2 instanceof TermRangeQuery
            //q2 instanceof PhraseQuery
            //others
            resultQuery.add(q2,Occur.SHOULD);
           
          }
        }
       
      }else if('-' == op.type){
        if(q1 == null || q2 == null){
          throw new IllegalStateException("表达式异常:SubQuery 个数不匹配");
        }
       
        if(q1 instanceof BooleanQuery){
          BooleanClause[] clauses = ((BooleanQuery)q1).getClauses();
          if(clauses.length > 0){
            for(BooleanClause c : clauses){
              resultQuery.add(c);
            }         
          }else{
            resultQuery.add(q1,Occur.MUST);
          }

        }else{
          //q1 instanceof TermQuery
          //q1 instanceof TermRangeQuery
          //q1 instanceof PhraseQuery
          //others
          resultQuery.add(q1,Occur.MUST);
        }       
       
        resultQuery.add(q2,Occur.MUST_NOT);
      }
      return resultQuery;
    }
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

  public static Query parse(Version matchVersion, String[] queries, String[] fields,
      Analyzer analyzer) throws ParseException
  {
    if (queries.length != fields.length)
      throw new IllegalArgumentException("queries.length != fields.length");
    BooleanQuery bQuery = new BooleanQuery();
    for (int i = 0; i < fields.length; i++)
    {
      QueryParser qp = new QueryParser(matchVersion, fields[i], analyzer);
      Query q = qp.parse(queries[i]);
      if (q!=null && // q never null, just being defensive
          (!(q instanceof BooleanQuery) || ((BooleanQuery)q).getClauses().length>0)) {
        bQuery.add(q, BooleanClause.Occur.SHOULD);
      }
    }
    return bQuery;
  }
View Full Code Here

   */
  public static Query parse(Version matchVersion, String query, String[] fields,
      BooleanClause.Occur[] flags, Analyzer analyzer) throws ParseException {
    if (fields.length != flags.length)
      throw new IllegalArgumentException("fields.length != flags.length");
    BooleanQuery bQuery = new BooleanQuery();
    for (int i = 0; i < fields.length; i++) {
      QueryParser qp = new QueryParser(matchVersion, fields[i], analyzer);
      Query q = qp.parse(query);
      if (q!=null && // q never null, just being defensive
          (!(q instanceof BooleanQuery) || ((BooleanQuery)q).getClauses().length>0)) {
        bQuery.add(q, flags[i]);
      }
    }
    return bQuery;
  }
View Full Code Here

  public static Query parse(Version matchVersion, String[] queries, String[] fields, BooleanClause.Occur[] flags,
      Analyzer analyzer) throws ParseException
  {
    if (!(queries.length == fields.length && queries.length == flags.length))
      throw new IllegalArgumentException("queries, fields, and flags array have have different length");
    BooleanQuery bQuery = new BooleanQuery();
    for (int i = 0; i < fields.length; i++)
    {
      QueryParser qp = new QueryParser(matchVersion, fields[i], analyzer);
      Query q = qp.parse(queries[i]);
      if (q!=null && // q never null, just being defensive
          (!(q instanceof BooleanQuery) || ((BooleanQuery)q).getClauses().length>0)) {
        bQuery.add(q, flags[i]);
      }
    }
    return bQuery;
  }
View Full Code Here

    StandardQueryParser qp = new StandardQueryParser();
    qp.setAnalyzer(new WhitespaceAnalyzer());

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

            new MaxPayloadFunction());
    PayloadTermQuery q2 = new PayloadTermQuery(new Term(PayloadHelper.NO_PAYLOAD_FIELD, "foo"),
            new MaxPayloadFunction());
    BooleanClause c1 = new BooleanClause(q1, BooleanClause.Occur.MUST);
    BooleanClause c2 = new BooleanClause(q2, BooleanClause.Occur.MUST_NOT);
    BooleanQuery query = new BooleanQuery();
    query.add(c1);
    query.add(c2);
    TopDocs hits = searcher.search(query, null, 100);
    assertTrue("hits is null and it shouldn't be", hits != null);
    assertTrue("hits Size: " + hits.totalHits + " is not: " + 1, hits.totalHits == 1);
    int[] results = new int[1];
    results[0] = 0;//hits.scoreDocs[0].doc;
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.