public int count(String query) {

    Query q;
    try {
      q = new QueryParser(Version.LUCENE_30, defaultFieldName, analyzer).parse(query);

      final IndexSearcher searcher = new IndexSearcher(index, true);

      CountCollector cc = new CountCollector();
      searcher.search(q, cc);

      return cc.getCount();
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    // ScoreDoc[] hits = collector.topDocs().scoreDocs;

    return -1;
  }
示例#2
0
  @Override
  public int count(String query) {
    if (searcher == null) return 0;

    Query q;
    try {
      q = queryParser.parse(query);

      CountCollector cc = new CountCollector();
      searcher.search(q, cc);

      return cc.getCount();
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    // ScoreDoc[] hits = collector.topDocs().scoreDocs;

    return -1;
  }
示例#3
0
  @Override
  public Selection select(
      String query, final int offs, final int limit, final Collection<String> aggs) {
    final Selection selection = new Selection();

    if (searcher == null) {
      selection.setObjects(Collections.<AgeObject>emptyList());

      return selection;
    }

    final List<AgeObject> res = new ArrayList<AgeObject>();

    Query q;

    try {
      q = queryParser.parse(query);

      CountCollector coll =
          new CountCollector() {
            int base;
            int count = -1;
            IndexReader reader;

            @Override
            int getCount() {
              return count + 1;
            }

            @Override
            public void setScorer(Scorer arg0) throws IOException {}

            @Override
            public void setNextReader(IndexReader arg0, int arg1) throws IOException {
              reader = arg0;
              base = arg1;
            }

            @Override
            public void collect(int docId) throws IOException {
              count++;
              //     System.out.println("Found doc: "+ind+". Object:
              // "+objectList.get(ind).getId()+". Class:
              // "+objectList.get(ind).getAgeElClass().getName() );
              if (count >= offs && (limit <= 0 || count < (offs + limit)))
                res.add(objectList.get(docId + base));

              if (aggs != null) {
                Document doc = reader.document(docId);

                for (String fld : aggs) {
                  String val = doc.get(fld);

                  int ival = 0;

                  try {
                    ival = Integer.parseInt(val);
                  } catch (Throwable e) {
                  }

                  selection.aggregate(fld, ival);
                }
              }
            }

            @Override
            public boolean acceptsDocsOutOfOrder() {
              return false;
            }
          };

      searcher.search(q, coll);

      selection.setObjects(res);
      selection.setTotalCount(coll.getCount());
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    // ScoreDoc[] hits = collector.topDocs().scoreDocs;

    return selection;
  }