Example usage for org.apache.lucene.index.memory MemoryIndex addField

List of usage examples for org.apache.lucene.index.memory MemoryIndex addField

Introduction

In this page you can find the example usage for org.apache.lucene.index.memory MemoryIndex addField.

Prototype

public void addField(String fieldName, TokenStream stream, int positionIncrementGap) 

Source Link

Document

Iterates over the given token stream and adds the resulting terms to the index; Equivalent to adding a tokenized, indexed, termVectorStored, unstored, Lucene org.apache.lucene.document.Field .

Usage

From source file:org.jppf.example.webcrawler.CrawlerTask.java

License:Apache License

/**
 * Search for the user-specified query expression in the current page.
 * @throws Exception if an error occurs.
 *///from  w  ww  . ja  v a 2s  . c o m
private void search() throws Exception {
    QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
    Query q = parser.parse(query);

    MemoryIndex index = new MemoryIndex();
    Link link = new Link(url);
    PageData pageData = new SimpleHttpClientParser().load(link);
    index.addField("contents", pageData.getData().toString(), new StandardAnalyzer());
    IndexSearcher searcher = index.createSearcher();
    Hits hits = searcher.search(q);
    Iterator it = hits.iterator();
    float relevance = 0f;
    if (it.hasNext()) {
        while (it.hasNext()) {
            Hit hit = (Hit) it.next();
            relevance += ((float) Math.round(hit.getScore() * 1000)) / 10;
        }
        matchedLinks.add(new LinkMatch(url, relevance));
    }
}

From source file:org.openspaces.textsearch.LuceneTextSearchQueryExtensionManager.java

License:Open Source License

@Override
public boolean accept(String typeName, String path, String operation, Object gridValue, Object luceneQuery) {
    Assert.notNull(gridValue, "Provided value from grid is null");
    Assert.notNull(luceneQuery, "Provided lucene query is null");
    validateOperationName(operation);// www  .  j  a va 2  s .  co  m

    if (_logger.isLoggable(Level.FINE))
        _logger.log(Level.FINE, "filter [operation=" + operation + ", leftOperand(value from grid)=" + gridValue
                + ", rightOperand(lucene query)=" + luceneQuery + "]");

    try {
        Analyzer analyzer = getAnalyzer(typeName, path);
        MemoryIndex index = new MemoryIndex();
        index.addField("content", String.valueOf(gridValue), analyzer);
        Query query = new QueryParser("content", analyzer).parse(String.valueOf(luceneQuery));
        float score = index.search(query);
        return score > 0.0f;
    } catch (ParseException e) {
        throw new SpaceRuntimeException("Could not parse full text query [ " + luceneQuery + " ]", e);
    }
}