Search in sources :

Example 36 with StandardAnalyzer

use of org.apache.lucene.analysis.standard.StandardAnalyzer in project gitblit by gitblit.

the class TicketIndexer method searchFor.

/**
	 * Search for tickets matching the query.  The returned tickets are
	 * shadows of the real ticket, but suitable for a results list.
	 *
	 * @param repository
	 * @param text
	 * @param page
	 * @param pageSize
	 * @return search results
	 */
public List<QueryResult> searchFor(RepositoryModel repository, String text, int page, int pageSize) {
    if (StringUtils.isEmpty(text)) {
        return Collections.emptyList();
    }
    Set<QueryResult> results = new LinkedHashSet<QueryResult>();
    StandardAnalyzer analyzer = new StandardAnalyzer();
    try {
        // search the title, description and content
        BooleanQuery.Builder bldr = new BooleanQuery.Builder();
        QueryParser qp;
        qp = new QueryParser(Lucene.title.name(), analyzer);
        qp.setAllowLeadingWildcard(true);
        bldr.add(qp.parse(text), Occur.SHOULD);
        qp = new QueryParser(Lucene.body.name(), analyzer);
        qp.setAllowLeadingWildcard(true);
        bldr.add(qp.parse(text), Occur.SHOULD);
        qp = new QueryParser(Lucene.content.name(), analyzer);
        qp.setAllowLeadingWildcard(true);
        bldr.add(qp.parse(text), Occur.SHOULD);
        IndexSearcher searcher = getSearcher();
        Query rewrittenQuery = searcher.rewrite(bldr.build());
        log.debug(rewrittenQuery.toString());
        TopScoreDocCollector collector = TopScoreDocCollector.create(5000);
        searcher.search(rewrittenQuery, collector);
        int offset = Math.max(0, (page - 1) * pageSize);
        ScoreDoc[] hits = collector.topDocs(offset, pageSize).scoreDocs;
        for (int i = 0; i < hits.length; i++) {
            int docId = hits[i].doc;
            Document doc = searcher.doc(docId);
            QueryResult result = docToQueryResult(doc);
            if (repository != null) {
                if (!result.repository.equalsIgnoreCase(repository.name)) {
                    continue;
                }
            }
            results.add(result);
        }
    } catch (Exception e) {
        log.error(MessageFormat.format("Exception while searching for {0}", text), e);
    }
    return new ArrayList<QueryResult>(results);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) TopScoreDocCollector(org.apache.lucene.search.TopScoreDocCollector) ArrayList(java.util.ArrayList) Document(org.apache.lucene.document.Document) ParseException(java.text.ParseException) IOException(java.io.IOException) ScoreDoc(org.apache.lucene.search.ScoreDoc) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer)

Example 37 with StandardAnalyzer

use of org.apache.lucene.analysis.standard.StandardAnalyzer in project gitblit by gitblit.

the class TicketIndexer method getWriter.

private IndexWriter getWriter() throws IOException {
    if (writer == null) {
        indexStore.create();
        Directory directory = FSDirectory.open(indexStore.getPath());
        StandardAnalyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        config.setOpenMode(OpenMode.CREATE_OR_APPEND);
        writer = new IndexWriter(directory, config);
    }
    return writer;
}
Also used : IndexWriter(org.apache.lucene.index.IndexWriter) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 38 with StandardAnalyzer

use of org.apache.lucene.analysis.standard.StandardAnalyzer in project textdb by TextDB.

the class RelationManagerTest method test1.

/*
     * Test the information about "table catalog" itself is stored properly.
     * 
     */
@Test
public void test1() throws Exception {
    String tableCatalogDirectory = relationManager.getTableDirectory(CatalogConstants.TABLE_CATALOG);
    Analyzer tableCatalogLuceneAnalyzer = relationManager.getTableAnalyzer(CatalogConstants.TABLE_CATALOG);
    Schema tableCatalogSchema = relationManager.getTableSchema(CatalogConstants.TABLE_CATALOG);
    Assert.assertEquals(tableCatalogDirectory, new File(CatalogConstants.TABLE_CATALOG_DIRECTORY).getCanonicalPath());
    Assert.assertTrue(tableCatalogLuceneAnalyzer instanceof StandardAnalyzer);
    Assert.assertEquals(tableCatalogSchema, Utils.getSchemaWithID(CatalogConstants.TABLE_CATALOG_SCHEMA));
}
Also used : Schema(edu.uci.ics.textdb.api.schema.Schema) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) File(java.io.File) Test(org.junit.Test)

Example 39 with StandardAnalyzer

use of org.apache.lucene.analysis.standard.StandardAnalyzer in project geode by apache.

the class LuceneServiceImpl method createIndex.

public void createIndex(String indexName, String regionPath, Map<String, Analyzer> fieldAnalyzers) {
    if (fieldAnalyzers == null || fieldAnalyzers.isEmpty()) {
        throw new IllegalArgumentException("At least one field must be indexed");
    }
    Analyzer analyzer = new PerFieldAnalyzerWrapper(new StandardAnalyzer(), fieldAnalyzers);
    Set<String> fieldsSet = fieldAnalyzers.keySet();
    String[] fields = (String[]) fieldsSet.toArray(new String[fieldsSet.size()]);
    createIndex(indexName, regionPath, analyzer, fieldAnalyzers, fields);
}
Also used : StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) PerFieldAnalyzerWrapper(org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper)

Example 40 with StandardAnalyzer

use of org.apache.lucene.analysis.standard.StandardAnalyzer in project geode by apache.

the class LuceneIndexCreation method beforeCreate.

@Override
public void beforeCreate(Extensible<Region<?, ?>> source, Cache cache) {
    LuceneServiceImpl service = (LuceneServiceImpl) LuceneServiceProvider.get(cache);
    Analyzer analyzer = this.fieldAnalyzers == null ? new StandardAnalyzer() : new PerFieldAnalyzerWrapper(new StandardAnalyzer(), this.fieldAnalyzers);
    try {
        service.createIndex(getName(), getRegionPath(), analyzer, this.fieldAnalyzers, getFieldNames());
    } catch (LuceneIndexExistsException e) {
        logger.info(LocalizedStrings.LuceneIndexCreation_IGNORING_DUPLICATE_INDEX_CREATION_0_ON_REGION_1.toLocalizedString(e.getIndexName(), e.getRegionPath()));
    }
}
Also used : LuceneIndexExistsException(org.apache.geode.cache.lucene.LuceneIndexExistsException) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) LuceneServiceImpl(org.apache.geode.cache.lucene.internal.LuceneServiceImpl) Analyzer(org.apache.lucene.analysis.Analyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) PerFieldAnalyzerWrapper(org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper)

Aggregations

StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)112 Analyzer (org.apache.lucene.analysis.Analyzer)37 IndexWriter (org.apache.lucene.index.IndexWriter)36 Document (org.apache.lucene.document.Document)29 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)29 IndexSearcher (org.apache.lucene.search.IndexSearcher)24 Term (org.apache.lucene.index.Term)22 RAMDirectory (org.apache.lucene.store.RAMDirectory)21 Test (org.junit.Test)21 Query (org.apache.lucene.search.Query)20 BooleanQuery (org.apache.lucene.search.BooleanQuery)19 TermQuery (org.apache.lucene.search.TermQuery)19 IOException (java.io.IOException)16 Before (org.junit.Before)15 IndexReader (org.apache.lucene.index.IndexReader)14 HashMap (java.util.HashMap)13 Field (org.apache.lucene.document.Field)13 ArrayList (java.util.ArrayList)12 QueryParser (org.apache.lucene.queryparser.classic.QueryParser)12 Directory (org.apache.lucene.store.Directory)12