Search in sources :

Example 6 with DocumentStoredFieldVisitor

use of org.apache.lucene.document.DocumentStoredFieldVisitor in project cxf by apache.

the class Catalog method getBooks.

// CHECKSTYLE:ON: ReturnCount
@GET
@Produces(MediaType.APPLICATION_JSON)
public JsonArray getBooks() throws IOException {
    final IndexReader reader = DirectoryReader.open(directory);
    final IndexSearcher searcher = new IndexSearcher(reader);
    final JsonArrayBuilder builder = Json.createArrayBuilder();
    try {
        final Query query = new MatchAllDocsQuery();
        for (final ScoreDoc scoreDoc : searcher.search(query, 1000).scoreDocs) {
            final DocumentStoredFieldVisitor fieldVisitor = new DocumentStoredFieldVisitor(LuceneDocumentMetadata.SOURCE_FIELD);
            reader.document(scoreDoc.doc, fieldVisitor);
            builder.add(fieldVisitor.getDocument().getField(LuceneDocumentMetadata.SOURCE_FIELD).stringValue());
        }
        return builder.build();
    } finally {
        reader.close();
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) IndexReader(org.apache.lucene.index.IndexReader) DocumentStoredFieldVisitor(org.apache.lucene.document.DocumentStoredFieldVisitor) JsonArrayBuilder(javax.json.JsonArrayBuilder) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ScoreDoc(org.apache.lucene.search.ScoreDoc) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

DocumentStoredFieldVisitor (org.apache.lucene.document.DocumentStoredFieldVisitor)6 Document (org.apache.lucene.document.Document)2 JsonArrayBuilder (javax.json.JsonArrayBuilder)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 StoredFieldsReader (org.apache.lucene.codecs.StoredFieldsReader)1 Field (org.apache.lucene.document.Field)1 DocValuesStatus (org.apache.lucene.index.CheckIndex.Status.DocValuesStatus)1 IndexReader (org.apache.lucene.index.IndexReader)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 Query (org.apache.lucene.search.Query)1 ScoreDoc (org.apache.lucene.search.ScoreDoc)1 TermQuery (org.apache.lucene.search.TermQuery)1 Bits (org.apache.lucene.util.Bits)1