Examples of BooleanQuery


Examples of org.apache.lucene.search.BooleanQuery

        return new WildcardQuery(term);
      }

        /* Fuzzy Query */
      case SIMILIAR_TO: {
        BooleanQuery similarityQuery = new BooleanQuery();

        LowercaseWhitespaceAnalyzer analyzer = new LowercaseWhitespaceAnalyzer();
        TokenStream tokenStream = analyzer.tokenStream(String.valueOf(IEntity.ALL_FIELDS), new StringReader(value));
        Token token = null;
        while ((token = tokenStream.next()) != null) {
          Term term = new Term(fieldname, token.termText());
          similarityQuery.add(new BooleanClause(new FuzzyQuery(term), Occur.MUST));
        }

        return similarityQuery;
      }
    }
View Full Code Here

Examples of org.apache.lucene.search.BooleanQuery

    /* Make sure the searcher is in sync */
    isSearcherCurrent();

    /* Perform the search */
    try {
      BooleanQuery bQuery = new BooleanQuery();

      /* Handle State-Field separately (group) */
      BooleanQuery statesQuery = null;
      for (ISearchCondition condition : conditions) {
        if (requiresStateGrouping(condition)) {

          /* Create and add new BooleanQuery for State */
          if (statesQuery == null) {
            statesQuery = new BooleanQuery();
            bQuery.add(statesQuery, matchAllConditions ? Occur.MUST : Occur.SHOULD);
          }

          /* Add Boolean Clause per State */
          addStateClause(statesQuery, condition);
        }
      }

      /* Create a Query for each condition */
      BooleanQuery fieldQuery = null;
      for (ISearchCondition condition : conditions) {

        /* State Queries already handled */
        if (requiresStateGrouping(condition))
          continue;

        /* Create and add new BooleanQuery for other Fields */
        if (fieldQuery == null) {
          fieldQuery = new BooleanQuery();
          bQuery.add(fieldQuery, matchAllConditions ? Occur.MUST : Occur.SHOULD);
        }

        /* Create the Clause */
        BooleanClause clause = null;
        if (condition.getField().getId() == IEntity.ALL_FIELDS)
          clause = createAllNewsFieldsClause(condition, matchAllConditions);
        else
          clause = createBooleanClause(condition, matchAllConditions);

        /*
         * Specially treat this case where the specifier is a negation but any
         * of the supplied conditions should match in the result set.
         */
        if (condition.getSpecifier().isNegation() && !matchAllConditions) {
          BooleanQuery nestedquery = new BooleanQuery();
          nestedquery.add(clause);
          nestedquery.add(new BooleanClause(new MatchAllDocsQuery(), Occur.MUST));
          fieldQuery.add(new BooleanClause(nestedquery, Occur.SHOULD));
        }

        /* Normal Case */
        else {
View Full Code Here

Examples of org.apache.lucene.search.BooleanQuery

    return condition.getField().getId() == INews.STATE;
  }

  //TODO Think about a better performing solution here!
  private BooleanClause createAllNewsFieldsClause(ISearchCondition condition, boolean matchAllConditions) throws IOException {
    BooleanQuery allFieldsQuery = new BooleanQuery();
    String value = String.valueOf(condition.getValue());

    LowercaseWhitespaceAnalyzer analyzer = new LowercaseWhitespaceAnalyzer();
    TokenStream tokenStream = analyzer.tokenStream(String.valueOf(IEntity.ALL_FIELDS), new StringReader(value));
    Token token = null;
    while ((token = tokenStream.next()) != null) {
      String termText = token.termText();

      /* Contained in Title */
      WildcardQuery titleQuery = new WildcardQuery(new Term(String.valueOf(INews.TITLE), termText));
      allFieldsQuery.add(new BooleanClause(titleQuery, Occur.SHOULD));

      /* Contained in Description */
      WildcardQuery descriptionQuery = new WildcardQuery(new Term(String.valueOf(INews.DESCRIPTION), termText));
      allFieldsQuery.add(new BooleanClause(descriptionQuery, Occur.SHOULD));

      /* Contained in Attachment */
      WildcardQuery attachmentQuery = new WildcardQuery(new Term(String.valueOf(INews.ATTACHMENTS_CONTENT), termText));
      allFieldsQuery.add(new BooleanClause(attachmentQuery, Occur.SHOULD));

      /* Matches Author */
      WildcardQuery authorQuery = new WildcardQuery(new Term(String.valueOf(INews.AUTHOR), termText));
      allFieldsQuery.add(new BooleanClause(authorQuery, Occur.SHOULD));

      /* Matches Category */
      WildcardQuery categoryQuery = new WildcardQuery(new Term(String.valueOf(INews.CATEGORIES), termText));
      allFieldsQuery.add(new BooleanClause(categoryQuery, Occur.SHOULD));
    }

    /* Determine Occur (MUST, SHOULD, MUST NOT) */
    Occur occur = getOccur(condition.getSpecifier(), matchAllConditions);
    return new BooleanClause(allFieldsQuery, occur);
View Full Code Here

Examples of org.apache.lucene.search.BooleanQuery

      throw e;
    }
  }

  private static Query internalCreateQuery(Collection<ISearchCondition> conditions, boolean matchAllConditions) throws IOException {
    BooleanQuery bQuery = new BooleanQuery();

    /* Handle Scope Query separately */
    boolean isScoped = false;
    for (ISearchCondition condition : conditions) {
      if (isScopeCondition(condition)) {
        BooleanQuery scopeClause = createLocationClause(condition);
        if (!scopeClause.clauses().isEmpty()) {
          bQuery.add(scopeClause, Occur.MUST);
          isScoped = true;
        }
      }
    }

    /* If scoped, the fieldQuery is a MUST-Clause to the outer Query */
    BooleanQuery fieldQuery = bQuery;
    if (isScoped)
      fieldQuery = new BooleanQuery();

    /* Add Conditions into the Boolean Query */
    addFieldClauses(conditions, matchAllConditions, fieldQuery);

    /* Only add if not empty if scoped */
    if (isScoped && !fieldQuery.clauses().isEmpty())
      bQuery.add(fieldQuery, Occur.MUST);

    return bQuery;
  }
View Full Code Here

Examples of org.apache.lucene.search.BooleanQuery

  }

  private static void addFieldClauses(Collection<ISearchCondition> conditions, boolean matchAllConditions, BooleanQuery bQuery) throws IOException {

    /* Handle State-Field separately (group) */
    BooleanQuery statesQuery = null;
    for (ISearchCondition condition : conditions) {
      if (requiresStateGrouping(condition)) {

        /* Create and add new BooleanQuery for State */
        if (statesQuery == null) {
          statesQuery = new BooleanQuery();
          bQuery.add(statesQuery, matchAllConditions ? Occur.MUST : Occur.SHOULD);
        }

        /* Add Boolean Clause per State */
        addStateClause(statesQuery, condition);
      }
    }

    /* Create a Query for each condition */
    BooleanQuery fieldQuery = null;
    Analyzer analyzer = Indexer.createAnalyzer();
    for (ISearchCondition condition : conditions) {

      /* State and Scope Queries already handled */
      if (requiresStateGrouping(condition) || isScopeCondition(condition))
        continue;

      /* Create and add new BooleanQuery for other Fields */
      if (fieldQuery == null) {
        fieldQuery = new BooleanQuery();
        bQuery.add(fieldQuery, matchAllConditions ? Occur.MUST : Occur.SHOULD);
      }

      /* Create the Clause */
      BooleanClause clause = null;
      if (condition.getField().getId() == IEntity.ALL_FIELDS)
        clause = createAllNewsFieldsClause(analyzer, condition, matchAllConditions);
      else
        clause = createBooleanClause(analyzer, condition, matchAllConditions);

      /* Check if the Clause has any valid Query */
      Query query = clause.getQuery();
      if (query instanceof BooleanQuery && ((BooleanQuery) query).clauses().isEmpty())
        continue;

      /*
       * Specially treat this case where the specifier is a negation but any of
       * the supplied conditions should match in the result set.
       */
      if (condition.getSpecifier().isNegation() && !matchAllConditions) {
        BooleanQuery nestedquery = new BooleanQuery();
        nestedquery.add(clause);
        nestedquery.add(new BooleanClause(new MatchAllDocsQuery(), Occur.MUST));
        fieldQuery.add(new BooleanClause(nestedquery, Occur.SHOULD));
      }

      /* Normal Case */
      else {
View Full Code Here

Examples of org.apache.lucene.search.BooleanQuery

    return condition.getSpecifier() == SearchSpecifier.SCOPE;
  }

  private static BooleanClause createAllNewsFieldsClause(Analyzer analyzer, ISearchCondition condition, boolean matchAllConditions) throws IOException {
    IModelFactory factory = Owl.getModelFactory();
    BooleanQuery allFieldsQuery = new BooleanQuery();

    /* Require all words to be contained or not contained */
    if (condition.getSpecifier() == SearchSpecifier.CONTAINS_ALL) {
      List<ISearchCondition> tokenConditions = new ArrayList<ISearchCondition>();

      List<String> tokens = StringUtils.tokenizePhraseAware((String) condition.getValue());
      for (String token : tokens) {
        ISearchCondition tokenCondition = factory.createSearchCondition(condition.getField(), condition.getSpecifier(), token);

        /* Rewrite Specifier */
        if (condition.getSpecifier() == SearchSpecifier.CONTAINS_ALL)
          tokenCondition.setSpecifier(SearchSpecifier.CONTAINS);
        else
          tokenCondition.setSpecifier(SearchSpecifier.CONTAINS_NOT);

        tokenConditions.add(tokenCondition);
      }

      /* Build custom Query out of Conditions */
      for (ISearchCondition tokenCondition : tokenConditions) {
        BooleanClause tokenClause = createAllNewsFieldsClause(analyzer, tokenCondition, matchAllConditions);

        /* Ignore empty clauses (e.g. due to Stop Words) */
        if (tokenClause.getQuery() instanceof BooleanQuery && ((BooleanQuery) tokenClause.getQuery()).getClauses().length == 0)
          continue;

        tokenClause.setOccur(Occur.MUST);
        allFieldsQuery.add(tokenClause);
      }
    }

    /* Require any word to be contained or not contained */
    else {
      List<ISearchCondition> allFieldsConditions = new ArrayList<ISearchCondition>(5);

      /* Title */
      ISearchField field = factory.createSearchField(INews.TITLE, condition.getField().getEntityName());
      allFieldsConditions.add(factory.createSearchCondition(field, condition.getSpecifier(), condition.getValue()));

      /* Description */
      field = factory.createSearchField(INews.DESCRIPTION, condition.getField().getEntityName());
      allFieldsConditions.add(factory.createSearchCondition(field, condition.getSpecifier(), condition.getValue()));

      /* Author */
      field = factory.createSearchField(INews.AUTHOR, condition.getField().getEntityName());
      allFieldsConditions.add(factory.createSearchCondition(field, condition.getSpecifier(), condition.getValue()));

      /* Category */
      field = factory.createSearchField(INews.CATEGORIES, condition.getField().getEntityName());
      List<String> tokens = StringUtils.tokenizePhraseAware(condition.getValue().toString());
      if (!tokens.contains(condition.getValue().toString()))
        tokens.add(condition.getValue().toString());
      for (String token : tokens) {
        allFieldsConditions.add(factory.createSearchCondition(field, condition.getSpecifier().isNegation() ? SearchSpecifier.IS_NOT : SearchSpecifier.IS, token));
      }

      /* Attachment Content */
      field = factory.createSearchField(INews.ATTACHMENTS_CONTENT, condition.getField().getEntityName());
      allFieldsConditions.add(factory.createSearchCondition(field, condition.getSpecifier(), condition.getValue()));

      /* Create Clauses out of Conditions */
      boolean anyClauseIsEmpty = false;
      List<BooleanClause> clauses = new ArrayList<BooleanClause>();
      for (ISearchCondition allFieldCondition : allFieldsConditions) {
        BooleanClause clause = createBooleanClause(analyzer, allFieldCondition, matchAllConditions);
        clause.setOccur(Occur.SHOULD);
        clauses.add(clause);

        /* Ignore empty clauses (e.g. due to Stop Words) */
        if (clause.getQuery() instanceof BooleanQuery && ((BooleanQuery) clause.getQuery()).getClauses().length == 0)
          anyClauseIsEmpty = true;
      }

      /* Only add if none of the clauses is empty */
      if (!anyClauseIsEmpty) {
        for (BooleanClause clause : clauses) {
          allFieldsQuery.add(clause);
        }
      }
    }

    /* Determine Occur (MUST, SHOULD, MUST NOT) */
 
View Full Code Here

Examples of org.apache.lucene.search.BooleanQuery

    throw new UnsupportedOperationException("Unsupported Specifier for Age Query"); //$NON-NLS-1$
  }

  /* This Clause needs to be generated dynamically */
  private static BooleanQuery createLocationClause(ISearchCondition condition) {
    BooleanQuery bQuery = new BooleanQuery();
    Long[][] value = (Long[][]) condition.getValue();
    if (value != null) {

      /* Receive Folders */
      for (int i = 0; value[0] != null && i < value[0].length; i++) {
        if (value[0][i] != null) {
          IFolder folder = new FolderReference(value[0][i]).resolve();
          if (folder != null)
            addFolderLocationClause(bQuery, folder);
        }
      }

      /* Receive BookMarks */
      for (int i = 0; value[1] != null && i < value[1].length; i++) {
        if (value[1][i] != null) {
          IBookMark bookmark = new BookMarkReference(value[1][i]).resolve();
          if (bookmark != null)
            addBookMarkLocationClause(bQuery, bookmark);
        }
      }

      /* Receive NewsBins */
      if (value.length == 3) {
        for (int i = 0; value[2] != null && i < value[2].length; i++) {
          if (value[2][i] != null) {
            INewsBin newsbin = new NewsBinReference(value[2][i]).resolve();
            if (newsbin != null)
              addNewsBinLocationClause(bQuery, newsbin);
          }
        }
      }
    }

    /* The folder could be empty, make sure to add at least 1 Clause */
    if (bQuery.clauses().isEmpty())
      bQuery.add(new TermQuery(new Term(String.valueOf(INews.FEED), "")), Occur.SHOULD); //$NON-NLS-1$

    return bQuery;
  }
View Full Code Here

Examples of org.apache.lucene.search.BooleanQuery

    }
  }

  private static void addBookMarkLocationClause(BooleanQuery bQuery, IBookMark bookmark) {
    if (bookmark != null) {
      BooleanQuery bookMarkLocationQuery = new BooleanQuery();

      /* Match on Feed */
      String feed = bookmark.getFeedLinkReference().getLinkAsText().toLowerCase();
      bookMarkLocationQuery.add(new TermQuery(new Term(String.valueOf(INews.FEED), feed)), Occur.MUST);

      /* But ignore News from Bins */
      bookMarkLocationQuery.add(new TermQuery(new Term(String.valueOf(INews.PARENT_ID), NumberTools.longToString(0))), Occur.MUST);

      bQuery.add(bookMarkLocationQuery, Occur.SHOULD);
    }
  }
View Full Code Here

Examples of org.apache.lucene.search.BooleanQuery

        return createWildcardQuery(fieldname, value);
      }

        /* Fuzzy Query */
      case SIMILIAR_TO: {
        BooleanQuery similarityQuery = new BooleanQuery();

        LowercaseWhitespaceAnalyzer similarAnalyzer = new LowercaseWhitespaceAnalyzer();
        TokenStream tokenStream = similarAnalyzer.tokenStream(String.valueOf(IEntity.ALL_FIELDS), new StringReader(value));
        Token token = null;
        while ((token = tokenStream.next()) != null) {
          String termText = new String(token.termBuffer(), 0, token.termLength());
          Term term = new Term(fieldname, termText);
          similarityQuery.add(new BooleanClause(new FuzzyQuery(term), Occur.MUST));
        }

        return similarityQuery;
      }
    }
View Full Code Here

Examples of org.apache.lucene.search.BooleanQuery

        /* Return early on cancellation */
        if (monitor.isCanceled())
          return linkToRefs;

        BooleanQuery query = createGuidQuery(guid, copy);
        List<NewsReference> newsRefs = simpleSearch(currentSearcher, query);
        if (!newsRefs.isEmpty())
          linkToRefs.put(guid, newsRefs);
      }
      return linkToRefs;
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.