public void close() throws IOException {
   if (luceneSearcher != null) {
     luceneSearcher.close();
   }
   if (reader != null) {
     reader.close();
   }
 }
  /** @see org.wyona.yarep.impl.repo.vfs.DateIndexerSearcher#getRevisionYoungerThan(Date) */
  public Revision getRevisionYoungerThan(Date date) throws Exception {
    // log.debug("Get revision younger than: " + DateIndexerSearcherImplV1.format(date));
    Date youngerThanDate = new Date(date.getTime() + 1);

    // log.debug("Get revision for date: " + DateIndexerSearcherImplV1.format(youngerThanDate));
    try {
      org.apache.lucene.search.Searcher searcher = new IndexSearcher(indexDir.getAbsolutePath());
      if (searcher != null) {
        org.apache.lucene.search.Query query =
            org.apache.lucene.search.NumericRangeQuery.newLongRange(
                CREATION_DATE_FIELD_NAME,
                new Long(youngerThanDate.getTime()),
                new Long(new Date().getTime()),
                true,
                true);

        org.apache.lucene.search.Hits hits =
            searcher.search(
                query, new Sort(new SortField(CREATION_DATE_FIELD_NAME, SortField.LONG)));
        // log.debug("Query \"" + query + "\" on field '" + CREATION_DATE_FIELD_NAME + "' returned "
        // + hits.length() + " hits");
        String revisionName = null;
        if (hits != null && hits.length() > 0) {
          /* DEBUG
                              for (int i = 0; i < hits.length();i++) {
                                  revisionName = hits.doc(i).getField(REVISION_NAME_FIELD_NAME).stringValue();
                                  log.warn("DEBUG: Found revision name '" + revisionName + "' (Creation date: " + new Date(new Long(revisionName).longValue()) + ")");
                              }
          */
          revisionName = hits.doc(0).getField(REVISION_NAME_FIELD_NAME).stringValue();
        }
        searcher.close();
        if (revisionName != null) {
          return node.getRevision(revisionName);
        } else {
          return null;
        }
      } else {
        log.error("Searcher could not be initialized for index directory '" + indexDir + "'!");
        return null;
      }
    } catch (Exception e) {
      log.error(e, e);
      return null;
    }
  }
Beispiel #3
0
  /**
   * Performs a search using Lucene
   *
   * @param index with the documents to search
   * @param query to search
   * @param fieldName field where search inside the documents
   * @return the search result
   */
  public static LuceneSearchResult search(LuceneIndex index, String query, String fieldName) {
    try {

      Searcher searcher = new IndexSearcher(index.getDirectory());
      Analyzer analyzer = new StandardAnalyzer();
      QueryParser parser = new QueryParser(fieldName, analyzer);
      Query q = parser.parse(query);
      Hits hits = searcher.search(q);
      LuceneSearchResult lsr = new LuceneSearchResult(hits, index);
      searcher.close();
      return lsr;

    } catch (Exception e) {
      org.apache.commons.logging.LogFactory.getLog(LuceneSearcher.class).error(e);
    }
    return null;
  }
Beispiel #4
0
  public void run() {

    try {

      String searchQuery =
          (new BufferedReader(new InputStreamReader(searchSocket.getInputStream())))
              .readLine()
              .trim();

      IndexReader reader = writer.getReader();
      Searcher searcher = new IndexSearcher(reader);

      QueryParser indexParser = new QueryParser(Version.LUCENE_30, "data", analyzer);

      SortField hitSortField = new SortField("date", SortField.LONG);
      Sort hitSort = new Sort(hitSortField);

      TopFieldDocs hits = searcher.search(indexParser.parse(searchQuery), null, 1000, hitSort);

      PrintWriter searchReply = new PrintWriter(searchSocket.getOutputStream(), true);

      searchReply.println(hits.totalHits + " Hits for " + searchQuery);

      for (int i = 0; i < hits.totalHits; i++) {
        Document document = searcher.doc(hits.scoreDocs[i].doc);

        String host = document.get("hostname");
        String date = document.get("date");
        String data = document.get("data");

        searchReply.print("host: " + host + ", date: " + date + ", data: " + data + "\n\n");
      }

      searchReply.close();
      searcher.close();
      reader.close();
      searchSocket.close();
    } catch (Exception ex) {
      System.out.print("Exception: " + ex + "\n");
    }
  }
 /** @see org.wyona.yarep.impl.repo.vfs.DateIndexerSearcher#getMostRecentRevision() */
 public Revision getMostRecentRevision() {
   // log.debug("Get most recent revision...");
   try {
     org.apache.lucene.search.Searcher searcher = new IndexSearcher(indexDir.getAbsolutePath());
     if (searcher != null) {
       /*
                       String q = "*:*";
                       org.apache.lucene.search.Query query = new org.apache.lucene.queryParser.QueryParser(CREATION_DATE_FIELD_NAME, getAnalyzer()).parse(q);
       */
       org.apache.lucene.search.Query query = new org.apache.lucene.search.MatchAllDocsQuery();
       org.apache.lucene.search.Hits hits =
           searcher.search(
               query, new Sort(new SortField(CREATION_DATE_FIELD_NAME, SortField.LONG)));
       // log.debug("Query \"" + query + "\" on field '" + CREATION_DATE_FIELD_NAME + "' returned "
       // + hits.length() + " hits");
       String revisionName = null;
       if (hits != null && hits.length() > 0) {
         /* DEBUG
                             for (int i = 0; i < hits.length();i++) {
                                 revisionName = hits.doc(i).getField(REVISION_NAME_FIELD_NAME).stringValue();
                                 log.warn("DEBUG: Found revision name '" + revisionName + "' (Creation date: " + new Date(new Long(revisionName).longValue()) + ")");
                             }
         */
         revisionName =
             hits.doc(hits.length() - 1).getField(REVISION_NAME_FIELD_NAME).stringValue();
       }
       searcher.close();
       if (revisionName != null) {
         return node.getRevision(revisionName);
       } else {
         return null;
       }
     } else {
       log.error("Searcher could not be initialized for index directory '" + indexDir + "'!");
       return null;
     }
   } catch (Exception e) {
     log.error(e, e);
     return null;
   }
 }
  /**
   * Index the fileset.
   *
   * @exception IOException if Lucene I/O exception TODO: refactor!!!!!
   */
  private void indexDocs() throws IOException {
    Date start = new Date();

    boolean create = overwrite;
    // If the index directory doesn't exist,
    // create it and force create mode
    if (indexDir.mkdirs() && !overwrite) {
      create = true;
    }

    FSDirectory dir = FSDirectory.open(indexDir);
    try {
      Searcher searcher = null;
      boolean checkLastModified = false;
      if (!create) {
        try {
          searcher = new IndexSearcher(dir, true);
          checkLastModified = true;
        } catch (IOException ioe) {
          log("IOException: " + ioe.getMessage());
          // Empty - ignore, which indicates to index all
          // documents
        }
      }

      log("checkLastModified = " + checkLastModified, Project.MSG_VERBOSE);

      IndexWriterConfig conf =
          new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer)
              .setOpenMode(create ? OpenMode.CREATE : OpenMode.APPEND);
      LogMergePolicy lmp = (LogMergePolicy) conf.getMergePolicy();
      lmp.setUseCompoundFile(useCompoundIndex);
      lmp.setMergeFactor(mergeFactor);
      IndexWriter writer = new IndexWriter(dir, conf);
      int totalFiles = 0;
      int totalIndexed = 0;
      int totalIgnored = 0;
      try {

        for (int i = 0; i < rcs.size(); i++) {
          ResourceCollection rc = rcs.elementAt(i);
          if (rc.isFilesystemOnly()) {
            Iterator resources = rc.iterator();
            while (resources.hasNext()) {
              Resource r = (Resource) resources.next();
              if (!r.isExists() || !(r instanceof FileResource)) {
                continue;
              }

              totalFiles++;

              File file = ((FileResource) r).getFile();

              if (!file.exists() || !file.canRead()) {
                throw new BuildException(
                    "File \"" + file.getAbsolutePath() + "\" does not exist or is not readable.");
              }

              boolean indexIt = true;

              if (checkLastModified) {
                Term pathTerm = new Term("path", file.getPath());
                TermQuery query = new TermQuery(pathTerm);
                ScoreDoc[] hits = searcher.search(query, null, 1).scoreDocs;

                // if document is found, compare the
                // indexed last modified time with the
                // current file
                // - don't index if up to date
                if (hits.length > 0) {
                  Document doc = searcher.doc(hits[0].doc);
                  String indexModified = doc.get("modified").trim();
                  if (indexModified != null) {
                    long lastModified = 0;
                    try {
                      lastModified = DateTools.stringToTime(indexModified);
                    } catch (ParseException e) {
                      // if modified time is not parsable, skip
                    }
                    if (lastModified == file.lastModified()) {
                      // TODO: remove existing document
                      indexIt = false;
                    }
                  }
                }
              }

              if (indexIt) {
                try {
                  log("Indexing " + file.getPath(), Project.MSG_VERBOSE);
                  Document doc = handler.getDocument(file);

                  if (doc == null) {
                    totalIgnored++;
                  } else {
                    // Add the path of the file as a field named "path".  Use a Keyword field, so
                    // that the index stores the path, and so that the path is searchable
                    doc.add(
                        new Field(
                            "path", file.getPath(), Field.Store.YES, Field.Index.NOT_ANALYZED));

                    // Add the last modified date of the file a field named "modified".  Use a
                    // Keyword field, so that it's searchable, but so that no attempt is made
                    // to tokenize the field into words.
                    doc.add(
                        new Field(
                            "modified",
                            DateTools.timeToString(
                                file.lastModified(), DateTools.Resolution.MILLISECOND),
                            Field.Store.YES,
                            Field.Index.NOT_ANALYZED));

                    writer.addDocument(doc);
                    totalIndexed++;
                  }
                } catch (DocumentHandlerException e) {
                  throw new BuildException(e);
                }
              }
            }
            // for j
          }
          // if (fs != null)
        }
        // for i

        writer.optimize();
      }
      // try
      finally {
        // always make sure everything gets closed,
        // no matter how we exit.
        writer.close();
        if (searcher != null) {
          searcher.close();
        }
      }

      Date end = new Date();

      log(
          totalIndexed
              + " out of "
              + totalFiles
              + " indexed ("
              + totalIgnored
              + " ignored) in "
              + (end.getTime() - start.getTime())
              + " milliseconds");
    } finally {
      dir.close();
    }
  }