Search in sources :

Example 1 with CrossOriginResourceSharing

use of org.apache.cxf.rs.security.cors.CrossOriginResourceSharing in project cxf by apache.

the class Catalog method findBook.

@GET
@Produces(MediaType.APPLICATION_JSON)
@CrossOriginResourceSharing(allowAllOrigins = true)
@Path("/search")
public Response findBook(@Context SearchContext searchContext, @Context final UriInfo uri) throws IOException {
    final IndexReader reader = DirectoryReader.open(directory);
    final IndexSearcher searcher = new IndexSearcher(reader);
    final JsonArrayBuilder builder = Json.createArrayBuilder();
    try {
        visitor.reset();
        visitor.visit(searchContext.getCondition(SearchBean.class));
        final Query query = visitor.getQuery();
        if (query != null) {
            final TopDocs topDocs = searcher.search(query, 1000);
            for (final ScoreDoc scoreDoc : topDocs.scoreDocs) {
                final Document document = reader.document(scoreDoc.doc);
                final String source = document.getField(LuceneDocumentMetadata.SOURCE_FIELD).stringValue();
                builder.add(Json.createObjectBuilder().add("source", source).add("score", scoreDoc.score).add("url", uri.getBaseUriBuilder().path(Catalog.class).path(source).build().toString()));
            }
        }
        return Response.ok(builder.build()).build();
    } finally {
        reader.close();
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) IndexReader(org.apache.lucene.index.IndexReader) SearchBean(org.apache.cxf.jaxrs.ext.search.SearchBean) JsonArrayBuilder(javax.json.JsonArrayBuilder) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) Path(javax.ws.rs.Path) CrossOriginResourceSharing(org.apache.cxf.rs.security.cors.CrossOriginResourceSharing) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

JsonArrayBuilder (javax.json.JsonArrayBuilder)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 SearchBean (org.apache.cxf.jaxrs.ext.search.SearchBean)1 CrossOriginResourceSharing (org.apache.cxf.rs.security.cors.CrossOriginResourceSharing)1 Document (org.apache.lucene.document.Document)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 TopDocs (org.apache.lucene.search.TopDocs)1