public Query getQuery(Element e) throws ParserException {
    String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
    String text = DOMUtils.getNonBlankTextOrFail(e);

    BooleanQuery bq = new BooleanQuery(DOMUtils.getAttribute(e, "disableCoord", false));
    bq.setMinimumNumberShouldMatch(DOMUtils.getAttribute(e, "minimumNumberShouldMatch", 0));
    try {
      TokenStream ts = analyzer.tokenStream(fieldName, new StringReader(text));
      TermToBytesRefAttribute termAtt = ts.addAttribute(TermToBytesRefAttribute.class);
      Term term = null;
      BytesRef bytes = termAtt.getBytesRef();
      ts.reset();
      while (ts.incrementToken()) {
        termAtt.fillBytesRef();
        term = new Term(fieldName, BytesRef.deepCopyOf(bytes));
        bq.add(new BooleanClause(new TermQuery(term), BooleanClause.Occur.SHOULD));
      }
      ts.end();
      ts.close();
    } catch (IOException ioe) {
      throw new RuntimeException("Error constructing terms from index:" + ioe);
    }

    bq.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f));
    return bq;
  }
  @Override
  public BooleanQuery build(QueryNode queryNode) throws QueryNodeException {
    AnyQueryNode andNode = (AnyQueryNode) queryNode;

    BooleanQuery bQuery = new BooleanQuery();
    List<QueryNode> children = andNode.getChildren();

    if (children != null) {

      for (QueryNode child : children) {
        Object obj = child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);

        if (obj != null) {
          Query query = (Query) obj;

          try {
            bQuery.add(query, BooleanClause.Occur.SHOULD);
          } catch (TooManyClauses ex) {

            throw new QueryNodeException(
                new MessageImpl(/*
             * IQQQ.Q0028E_TOO_MANY_BOOLEAN_CLAUSES,
             * BooleanQuery.getMaxClauseCount()
             */ QueryParserMessages.EMPTY_MESSAGE), ex);
          }
        }
      }
    }

    bQuery.setMinimumNumberShouldMatch(andNode.getMinimumMatchingElements());

    return bQuery;
  }
Exemplo n.º 3
0
  @Override
  public List<Document> customRanking(List<Query> queryList) throws Exception {

    List<Document> result = new ArrayList<Document>();
    Map<String, ResultRep> docMap = new LinkedHashMap<String, ResultRep>();
    BooleanQuery bq = new BooleanQuery();

    bq.setMinimumNumberShouldMatch(1);
    Set<String> tgtset = tstSet == null ? new HashSet<String>() : tstSet;
    for (String clsStr : tgtset)
      bq.add(
          new TermQuery(new Term(Configuration.IDX_FIELD_PKG_NAME, QueryParser.escape(clsStr))),
          Occur.SHOULD);
    Filter idFilter = new QueryWrapperFilter(bq);

    try {
      Float avg = new Float(queryList.size());
      Float rank;
      // BooleanQuery mainQuery;
      for (Query query : queryList) {
        // mainQuery = new BooleanQuery();
        // mainQuery.add(query, Occur.SHOULD);
        // mainQuery.add(idFilter, Occur.MUST);
        result = searcher(query, idFilter);
        for (int i = 0; i < result.size(); i++) {
          Document doc = result.get(i);
          String key = doc.get(Configuration.IDX_FIELD_METHOD_DESCRIPTION_CATCHALL);
          rank = Float.parseFloat(doc.get("SCORE"));
          rank = rank / avg;
          if (!docMap.containsKey(key)) docMap.put(key, new ResultRep(doc, rank));
          else {
            ResultRep rep = docMap.get(key);
            rep.setRank(rep.getRank() + rank);
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    TreeSet<ResultRep> set = new TreeSet<ResultRep>(new CustomComparator());

    for (String key : docMap.keySet()) set.add(docMap.get(key));

    result = new ArrayList<Document>();
    Iterator<ResultRep> iter = set.descendingIterator();
    ResultRep rep;
    while (iter.hasNext()) {
      rep = iter.next();
      if (tgtset.contains(rep.getDoc().get(Configuration.IDX_FIELD_CLASS_NAME).trim()))
        result.add(rep.getDoc());
    }

    if (result.size() > 10) result = result.subList(0, 10);
    return result;
  }
Exemplo n.º 4
0
 @Override
 public Query apply(Function input, Context context) {
   assert input != null;
   BooleanQuery query = new BooleanQuery();
   query.setMinimumNumberShouldMatch(1);
   for (Symbol symbol : input.arguments()) {
     query.add(process(symbol, context), BooleanClause.Occur.SHOULD);
   }
   return query;
 }
Exemplo n.º 5
0
 @Override
 protected Query applyArrayLiteral(Reference reference, Literal arrayLiteral, Context context)
     throws IOException {
   // col like ANY (['a', 'b']) --> or(like(col, 'a'), like(col, 'b'))
   BooleanQuery booleanQuery = new BooleanQuery();
   booleanQuery.setMinimumNumberShouldMatch(1);
   for (Object value : toIterable(arrayLiteral.value())) {
     booleanQuery.add(
         likeQuery.toQuery(reference, value, context), BooleanClause.Occur.SHOULD);
   }
   return booleanQuery;
 }
 @Override
 public void readFields(DataInput in) throws IOException {
   query = new BooleanQuery(in.readBoolean());
   query.setBoost(in.readFloat());
   query.setMinimumNumberShouldMatch(in.readInt());
   int length = in.readInt();
   for (int i = 0; i < length; i++) {
     BooleanClauseWritable booleanClauseWritable = new BooleanClauseWritable();
     booleanClauseWritable.readFields(in);
     query.add(booleanClauseWritable.getBooleanClause());
   }
 }
Exemplo n.º 7
0
 @Override
 protected Query applyArrayLiteral(Reference reference, Literal arrayLiteral, Context context)
     throws IOException {
   // col < ANY ([1,2,3]) --> or(col<1, col<2, col<3)
   BooleanQuery booleanQuery = new BooleanQuery();
   booleanQuery.setMinimumNumberShouldMatch(1);
   for (Object value : toIterable(arrayLiteral.value())) {
     booleanQuery.add(
         inverseRangeQuery.toQuery(reference, reference.valueType(), value),
         BooleanClause.Occur.SHOULD);
   }
   return booleanQuery;
 }
Exemplo n.º 8
0
      @Override
      protected Query applyArrayReference(
          Reference arrayReference, Literal literal, Context context) throws IOException {
        // 1 != any ( col ) -->  gt 1 or lt 1
        String columnName = arrayReference.info().ident().columnIdent().fqn();
        Object value = literal.value();

        QueryBuilderHelper builder = QueryBuilderHelper.forType(arrayReference.valueType());
        BooleanQuery query = new BooleanQuery();
        query.setMinimumNumberShouldMatch(1);
        query.add(
            builder.rangeQuery(columnName, value, null, false, false), BooleanClause.Occur.SHOULD);
        query.add(
            builder.rangeQuery(columnName, null, value, false, false), BooleanClause.Occur.SHOULD);
        return query;
      }
Exemplo n.º 9
0
  public CObjList searchSubscribable(
      String squery, String memid, boolean prv, boolean pub, Sort s) {
    if (!prv && !pub) {
      prv = true;
      pub = true;
    }

    BooleanQuery combined = new BooleanQuery();
    combined.setMinimumNumberShouldMatch(1);

    Term comterm = new Term(CObj.PARAM_TYPE, CObj.COMMUNITY);
    combined.add(new TermQuery(comterm), BooleanClause.Occur.MUST);

    if (pub) {

      Term pubterm = new Term(CObj.docString(CObj.SCOPE), CObj.SCOPE_PUBLIC);
      combined.add(new TermQuery(pubterm), BooleanClause.Occur.SHOULD);
    }

    if (prv && memid != null) {

      Term pubterm2 = new Term(CObj.docPrivate(memid), "true");
      combined.add(new TermQuery(pubterm2), BooleanClause.Occur.SHOULD);
    }

    if (squery != null) {
      Matcher m = Pattern.compile("\\S+").matcher(squery);

      if (m.find()) {
        StringBuilder sb = new StringBuilder();
        sb.append(CObj.docPrivateText(CObj.NAME));
        sb.append(":\"");
        sb.append(squery);
        sb.append("\" OR ");
        sb.append(CObj.docPrivateText(CObj.DESCRIPTION));
        sb.append(":\"");
        sb.append(squery);
        sb.append("\"");
        return search(combined, sb.toString(), Integer.MAX_VALUE, s);
      }
    }

    return search(combined, Integer.MAX_VALUE, s);
  }
Exemplo n.º 10
0
  @Override
  public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    XContentParser parser = parseContext.parser();

    boolean disableCoord = false;
    float boost = 1.0f;
    int minimumNumberShouldMatch = -1;

    List<BooleanClause> clauses = newArrayList();

    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
      if (token == XContentParser.Token.FIELD_NAME) {
        currentFieldName = parser.currentName();
      } else if (token == XContentParser.Token.START_OBJECT) {
        if ("must".equals(currentFieldName)) {
          clauses.add(new BooleanClause(parseContext.parseInnerQuery(), BooleanClause.Occur.MUST));
        } else if ("must_not".equals(currentFieldName) || "mustNot".equals(currentFieldName)) {
          clauses.add(
              new BooleanClause(parseContext.parseInnerQuery(), BooleanClause.Occur.MUST_NOT));
        } else if ("should".equals(currentFieldName)) {
          clauses.add(
              new BooleanClause(parseContext.parseInnerQuery(), BooleanClause.Occur.SHOULD));
        }
      } else if (token == XContentParser.Token.START_ARRAY) {
        if ("must".equals(currentFieldName)) {
          while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            clauses.add(
                new BooleanClause(parseContext.parseInnerQuery(), BooleanClause.Occur.MUST));
          }
        } else if ("must_not".equals(currentFieldName) || "mustNot".equals(currentFieldName)) {
          while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            clauses.add(
                new BooleanClause(parseContext.parseInnerQuery(), BooleanClause.Occur.MUST_NOT));
          }
        } else if ("should".equals(currentFieldName)) {
          while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            clauses.add(
                new BooleanClause(parseContext.parseInnerQuery(), BooleanClause.Occur.SHOULD));
          }
        }
      } else if (token.isValue()) {
        if ("disable_coord".equals(currentFieldName) || "disableCoord".equals(currentFieldName)) {
          disableCoord = parser.booleanValue();
        } else if ("minimum_number_should_match".equals(currentFieldName)
            || "minimumNumberShouldMatch".equals(currentFieldName)) {
          minimumNumberShouldMatch = parser.intValue();
        } else if ("boost".equals(currentFieldName)) {
          boost = parser.floatValue();
        }
      }
    }

    BooleanQuery query = new BooleanQuery(disableCoord);
    for (BooleanClause clause : clauses) {
      query.add(clause);
    }
    query.setBoost(boost);
    if (minimumNumberShouldMatch != -1) {
      query.setMinimumNumberShouldMatch(minimumNumberShouldMatch);
    }
    return optimizeQuery(fixNegativeQueryIfNeeded(query));
  }