Пример #1
0
 public DirectoryReader getReader(boolean applyDeletions) throws IOException {
   getReaderCalled = true;
   if (r.nextInt(20) == 2) {
     doRandomForceMerge();
   }
   if (!applyDeletions || r.nextBoolean()) {
     if (LuceneTestCase.VERBOSE) {
       System.out.println("RIW.getReader: use NRT reader");
     }
     if (r.nextInt(5) == 1) {
       w.commit();
     }
     return w.getReader(applyDeletions);
   } else {
     if (LuceneTestCase.VERBOSE) {
       System.out.println("RIW.getReader: open new reader");
     }
     w.commit();
     if (r.nextBoolean()) {
       return DirectoryReader.open(w.getDirectory());
     } else {
       return w.getReader(applyDeletions);
     }
   }
 }
  @Test
  public void testVectorHighlighter() throws Exception {
    Directory dir = new RAMDirectory();
    IndexWriter indexWriter =
        new IndexWriter(dir, Lucene.STANDARD_ANALYZER, true, IndexWriter.MaxFieldLength.UNLIMITED);

    indexWriter.addDocument(
        doc()
            .add(field("_id", "1"))
            .add(
                field(
                    "content",
                    "the big bad dog",
                    Field.Store.YES,
                    Field.Index.ANALYZED,
                    Field.TermVector.WITH_POSITIONS_OFFSETS))
            .build());

    IndexReader reader = indexWriter.getReader();
    IndexSearcher searcher = new IndexSearcher(reader);
    TopDocs topDocs = searcher.search(new TermQuery(new Term("_id", "1")), 1);

    assertThat(topDocs.totalHits, equalTo(1));

    FastVectorHighlighter highlighter = new FastVectorHighlighter();
    String fragment =
        highlighter.getBestFragment(
            highlighter.getFieldQuery(new TermQuery(new Term("content", "bad"))),
            reader,
            topDocs.scoreDocs[0].doc,
            "content",
            30);
    assertThat(fragment, notNullValue());
    System.out.println(fragment);
  }
Пример #3
0
 private AcquirableResource<ReaderSearcherHolder> buildNrtResource(IndexWriter indexWriter)
     throws IOException {
   IndexReader indexReader = indexWriter.getReader();
   IndexSearcher indexSearcher = new IndexSearcher(indexReader);
   indexSearcher.setSimilarity(similarityService.defaultSearchSimilarity());
   return newAcquirableResource(new ReaderSearcherHolder(indexSearcher));
 }
 public IndexReader getDiskIndexReader() throws IOException {
   if (_writer != null) {
     return _writer.getReader();
   } else {
     return null;
   }
 }
 /**
  * Get the index writer/searcher wrapper for the given connection.
  *
  * @param conn the connection
  * @return the index access wrapper
  */
 protected static IndexAccess getIndexAccess(Connection conn) throws SQLException {
   String path = getIndexPath(conn);
   synchronized (INDEX_ACCESS) {
     IndexAccess access = INDEX_ACCESS.get(path);
     if (access == null) {
       try {
         /*## LUCENE2 ##
         boolean recreate = !IndexReader.indexExists(path);
         Analyzer analyzer = new StandardAnalyzer();
         access = new IndexAccess();
         access.modifier = new IndexModifier(path, analyzer, recreate);
         //*/
         // ## LUCENE3 ##
         File f = new File(path);
         Directory indexDir = FSDirectory.open(f);
         boolean recreate = !IndexReader.indexExists(indexDir);
         Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
         IndexWriter writer =
             new IndexWriter(indexDir, analyzer, recreate, IndexWriter.MaxFieldLength.UNLIMITED);
         // see http://wiki.apache.org/lucene-java/NearRealtimeSearch
         IndexReader reader = writer.getReader();
         access = new IndexAccess();
         access.writer = writer;
         access.reader = reader;
         access.searcher = new IndexSearcher(reader);
         // */
       } catch (IOException e) {
         throw convertException(e);
       }
       INDEX_ACCESS.put(path, access);
     }
     return access;
   }
 }
Пример #6
0
  public void testLatLongFilterOnDeletedDocs() throws Exception {
    writer.deleteDocuments(new Term("name", "Potomac"));
    IndexReader r = writer.getReader();
    LatLongDistanceFilter f =
        new LatLongDistanceFilter(
            new QueryWrapperFilter(new MatchAllDocsQuery()), lat, lng, 1.0, latField, lngField);

    IndexReader[] readers = r.getSequentialSubReaders();
    for (int i = 0; i < readers.length; i++) {
      f.getDocIdSet(readers[i]);
    }
  }
Пример #7
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");
    }
  }
  @Test
  public void testVectorHighlighterPrefixQuery() throws Exception {
    Directory dir = new RAMDirectory();
    IndexWriter indexWriter =
        new IndexWriter(dir, Lucene.STANDARD_ANALYZER, true, IndexWriter.MaxFieldLength.UNLIMITED);

    indexWriter.addDocument(
        doc()
            .add(field("_id", "1"))
            .add(
                field(
                    "content",
                    "the big bad dog",
                    Field.Store.YES,
                    Field.Index.ANALYZED,
                    Field.TermVector.WITH_POSITIONS_OFFSETS))
            .build());

    IndexReader reader = indexWriter.getReader();
    IndexSearcher searcher = new IndexSearcher(reader);
    TopDocs topDocs = searcher.search(new TermQuery(new Term("_id", "1")), 1);

    assertThat(topDocs.totalHits, equalTo(1));

    FastVectorHighlighter highlighter = new FastVectorHighlighter();

    PrefixQuery prefixQuery = new PrefixQuery(new Term("content", "ba"));
    assertThat(
        prefixQuery.getRewriteMethod().getClass().getName(),
        equalTo(PrefixQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT.getClass().getName()));
    String fragment =
        highlighter.getBestFragment(
            highlighter.getFieldQuery(prefixQuery),
            reader,
            topDocs.scoreDocs[0].doc,
            "content",
            30);
    assertThat(fragment, nullValue());

    prefixQuery.setRewriteMethod(PrefixQuery.SCORING_BOOLEAN_QUERY_REWRITE);
    Query rewriteQuery = prefixQuery.rewrite(reader);
    fragment =
        highlighter.getBestFragment(
            highlighter.getFieldQuery(rewriteQuery),
            reader,
            topDocs.scoreDocs[0].doc,
            "content",
            30);
    assertThat(fragment, notNullValue());

    System.out.println(fragment);

    // now check with the custom field query
    prefixQuery = new PrefixQuery(new Term("content", "ba"));
    assertThat(
        prefixQuery.getRewriteMethod().getClass().getName(),
        equalTo(PrefixQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT.getClass().getName()));
    CustomFieldQuery.reader.set(reader);
    fragment =
        highlighter.getBestFragment(
            new CustomFieldQuery(prefixQuery, highlighter),
            reader,
            topDocs.scoreDocs[0].doc,
            "content",
            30);
    assertThat(fragment, notNullValue());

    System.out.println(fragment);
  }
Пример #9
0
 public IndexInfo(String indexName, Directory directory, Analyzer analyzer)
     throws CorruptIndexException, IOException {
   IndexWriter iwriter = null;
   IndexReader indexReader = null;
   try {
     iwriter = new IndexWriter(directory, analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
     indexReader = iwriter.getReader();
     this.index_name = indexName;
     this.index_version = indexReader.getVersion();
     this.optimized = indexReader.isOptimized();
     this.numDocs = indexReader.numDocs();
     Date date = new Date(IndexReader.lastModified(directory));
     DateFormat formatter = new SimpleDateFormat("EEEE, dd MMMM yyyy, hh:mm:ss a");
     this.lastModifyDate = formatter.format(date);
     this.files = new HashMap<String, String>();
     String[] files = directory.listAll();
     for (int i = 0; i < files.length; i++) {
       if (files[i].startsWith("_"))
         this.files.put(
             files[i],
             Long.toString(
                     Math.round(
                         FileSizeConverter.convertSize(
                             directory.fileLength(files[i]), FileSizeConverter.KB)))
                 + " KB");
     }
     indexReader.close();
     iwriter.close();
     iwriter = null;
     directory.clearLock(directory.getLockID());
   } catch (CorruptIndexException e) {
     if (iwriter != null) {
       iwriter.close();
       iwriter = null;
     }
     if (indexReader != null) {
       indexReader.close();
       indexReader = null;
     }
     throw e;
   } catch (LockObtainFailedException e) {
     if (iwriter != null) {
       iwriter.close();
       iwriter = null;
     }
     if (indexReader != null) {
       indexReader.close();
       indexReader = null;
     }
     throw e;
   } catch (IOException e) {
     if (iwriter != null) {
       iwriter.close();
       iwriter = null;
     }
     if (indexReader != null) {
       indexReader.close();
       indexReader = null;
     }
     throw e;
   }
 }