Example usage for org.apache.lucene.search IndexSearcher IndexSearcher

List of usage examples for org.apache.lucene.search IndexSearcher IndexSearcher

Introduction

In this page you can find the example usage for org.apache.lucene.search IndexSearcher IndexSearcher.

Prototype

public IndexSearcher(IndexReaderContext context, Executor executor) 

Source Link

Document

Creates a searcher searching the provided top-level IndexReaderContext .

Usage

From source file:analysis.SynonymAnalyzerTest.java

License:Apache License

public void setUp() throws Exception {
    RAMDirectory directory = new RAMDirectory();

    IndexWriter writer = new IndexWriter(directory, synonymAnalyzer, //#1  
            IndexWriter.MaxFieldLength.UNLIMITED);
    Document doc = new Document();
    doc.add(new Field("content", "The quick brown fox jumps over the lazy dog", Field.Store.YES,
            Field.Index.ANALYZED)); //#2
    writer.addDocument(doc);//from  ww  w.java2 s  . c o  m

    writer.close();

    searcher = new IndexSearcher(directory, true);
}

From source file:aos.lucene.analysis.positional.PositionalPorterStopAnalyzerTest.java

License:Apache License

public void setUp() throws Exception {

    RAMDirectory directory = new RAMDirectory();

    IndexWriter writer = new IndexWriter(directory, porterAnalyzer, IndexWriter.MaxFieldLength.UNLIMITED);

    Document doc = new Document();
    doc.add(new Field("contents", "The quick brown fox jumps over the lazy dog", Field.Store.YES,
            Field.Index.ANALYZED));
    writer.addDocument(doc);/* w w w .  j a  v a  2 s.co m*/
    writer.close();
    searcher = new IndexSearcher(directory, true);
    parser = new QueryParser(Version.LUCENE_46, "contents", porterAnalyzer);
}

From source file:aos.lucene.search.advanced.FunctionQueryTest.java

License:Apache License

public void setUp() throws Exception {
    Directory dir = new RAMDirectory();
    w = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_46), IndexWriter.MaxFieldLength.UNLIMITED);
    addDoc(7, "this hat is green");
    addDoc(42, "this hat is blue");
    w.close();//from  w w  w.  ja  va 2  s  . c  o  m

    s = new IndexSearcher(dir, true);
}

From source file:aos.lucene.search.advanced.MultiFieldQueryParserTest.java

License:Apache License

public void testDefaultOperator() throws Exception {
    Query query = new MultiFieldQueryParser(Version.LUCENE_46, new String[] { "title", "subject" },
            new SimpleAnalyzer()).parse("development");

    Directory dir = TestUtil.getBookIndexDirectory();
    IndexSearcher searcher = new IndexSearcher(dir, true);
    TopDocs hits = searcher.search(query, 10);

    assertTrue(TestUtil.hitsIncludeTitle(searcher, hits, "Ant in Action"));

    assertTrue(TestUtil.hitsIncludeTitle( //A
            searcher, //A
            hits, //A
            "Extreme Programming Explained")); //A
    searcher.close();/*from  w  ww .  j a va  2  s  . co  m*/
    dir.close();
}

From source file:aos.lucene.search.advanced.MultiFieldQueryParserTest.java

License:Apache License

public void testSpecifiedOperator() throws Exception {
    Query query = MultiFieldQueryParser.parse(Version.LUCENE_46, "lucene", new String[] { "title", "subject" },
            new BooleanClause.Occur[] { BooleanClause.Occur.MUST, BooleanClause.Occur.MUST },
            new SimpleAnalyzer());

    Directory dir = TestUtil.getBookIndexDirectory();
    IndexSearcher searcher = new IndexSearcher(dir, true);
    TopDocs hits = searcher.search(query, 10);

    assertTrue(TestUtil.hitsIncludeTitle(searcher, hits, "Lucene in Action, Second Edition"));
    assertEquals("one and only one", 1, hits.scoreDocs.length);
    searcher.close();//  w  w  w  .  j a v a 2s. c  o m
    dir.close();
}

From source file:aos.lucene.search.ext.filters.SpecialsFilterTest.java

License:Apache License

@Override
protected void setUp() throws Exception {
    allBooks = new MatchAllDocsQuery();
    searcher = new IndexSearcher(TestUtil.getBookIndexDirectory(), true);
}

From source file:aos.lucene.search.ext.queryparser.NumericQueryParserTest.java

License:Apache License

protected void setUp() throws Exception {
    analyzer = new WhitespaceAnalyzer(Version.LUCENE_46);
    dir = TestUtil.getBookIndexDirectory();
    searcher = new IndexSearcher(dir, true);
}

From source file:aos.lucene.tools.BerkeleyDbJESearcher.java

License:Apache License

public static void main(String[] args) throws IOException, DatabaseException {

    if (args.length != 1) {
        System.err.println("Usage: BerkeleyDbSearcher <index dir>");
        System.exit(-1);// ww w  . ja  v a 2s. c o  m
    }
    File indexFile = new File(args[0]);

    EnvironmentConfig envConfig = new EnvironmentConfig();
    DatabaseConfig dbConfig = new DatabaseConfig();

    envConfig.setTransactional(true);
    envConfig.setAllowCreate(true);
    dbConfig.setTransactional(true);
    dbConfig.setAllowCreate(true);

    Environment env = new Environment(indexFile, envConfig);

    Database index = env.openDatabase(null, "__index__", dbConfig);
    Database blocks = env.openDatabase(null, "__blocks__", dbConfig);

    JEDirectory directory = new JEDirectory(null, index, blocks);

    IndexSearcher searcher = new IndexSearcher(directory, true);
    TopDocs hits = searcher.search(new TermQuery(new Term("contents", "fox")), 10);
    LOGGER.info(hits.totalHits + " documents found");
    searcher.close();

    index.close();
    blocks.close();
    env.close();
}

From source file:aos.lucene.tools.BerkeleyDbSearcher.java

License:Apache License

public static void main(String[] args) throws IOException, DatabaseException {
    if (args.length != 1) {
        System.err.println("Usage: BerkeleyDbSearcher <index dir>");
        System.exit(-1);//from   www  .j  ava  2s .  co m
    }
    File indexFile = new File(args[0]);

    EnvironmentConfig envConfig = new EnvironmentConfig();
    DatabaseConfig dbConfig = new DatabaseConfig();

    envConfig.setTransactional(true);
    envConfig.setInitializeCache(true);
    envConfig.setInitializeLocking(true);
    envConfig.setInitializeLogging(true);
    envConfig.setAllowCreate(true);
    envConfig.setThreaded(true);
    dbConfig.setAllowCreate(true);
    dbConfig.setType(DatabaseType.BTREE);

    Environment env = new Environment(indexFile, envConfig);

    Database index = env.openDatabase(null, "__index__", null, dbConfig);
    Database blocks = env.openDatabase(null, "__blocks__", null, dbConfig);

    DbDirectory directory = new DbDirectory(null, index, blocks, 0);

    IndexSearcher searcher = new IndexSearcher(directory, true);
    TopDocs hits = searcher.search(new TermQuery(new Term("contents", "fox")), 10);
    LOGGER.info(hits.totalHits + " documents found");
    searcher.close();

    index.close();
    blocks.close();
    env.close();
}

From source file:axiom.scripting.rhino.LuceneQueryDispatcher.java

License:Open Source License

public ArrayList getVersionFields(Object obj, Object fields, ArrayList prototypes, IFilter filter,
        Object options) throws Exception {
    String id = null;/*  w  w w.ja va  2  s . c  o m*/
    if (obj instanceof String) {
        id = (String) obj;
    } else if (obj instanceof INode) {
        id = ((INode) obj).getID();
    } else if (obj instanceof Scriptable) {
        id = ScriptRuntime.toString(obj);
    } else {
        id = obj.toString();
    }

    Scriptable idfilter = Context.getCurrentContext().newObject(this.core.getScope());
    idfilter.put(LuceneManager.ID, idfilter, id);
    IFilter newFilter = AndFilterObject.filterObjCtor(null, new Object[] { filter, idfilter }, null, false);

    SortObject sort = getSortObject((Scriptable) options);
    ArrayList opaths = getPaths((Scriptable) options);
    int _layer = getLayer((Scriptable) options);
    RequestEvaluator reqeval = this.app.getCurrentRequestEvaluator();
    _layer = (_layer == -1) ? (reqeval != null) ? reqeval.getLayer() : DbKey.LIVE_LAYER : _layer;
    int maxResults = getMaxResults((Scriptable) options);

    IndexSearcher searcher = new IndexSearcher(this.lmgr.getDirectory(), true);

    Object hits = this.luceneHits(prototypes, newFilter, sort, maxResults, opaths, searcher, null, _layer);

    ArrayList<Scriptable> versions = new ArrayList<Scriptable>();
    if (hits != null) {
        int hitslength = hits instanceof Hits ? ((Hits) hits).length() : ((TopDocs) hits).scoreDocs.length;
        if (hitslength > 0) {
            if (maxResults < 0) {
                maxResults = hitslength;
            }
        }
        for (int i = 0, count = 0; i < hitslength && count < maxResults; i++) {
            Document doc = hits instanceof Hits ? ((Hits) hits).doc(i)
                    : searcher.doc(((TopDocs) hits).scoreDocs[i].doc);
            if (doc != null) {
                ArrayList<String> _fields = new ArrayList<String>();
                if (fields instanceof String) {
                    _fields.add((String) fields);
                } else if (fields instanceof Scriptable) {
                    String className = ((Scriptable) fields).getClassName();
                    if (className.equals("String")) {
                        _fields.add(fields.toString());
                    } else if (className.equals("Array")) {
                        Scriptable arr = (Scriptable) fields;
                        final int arrlen = arr.getIds().length;
                        for (int j = 0; j < arrlen; j++) {
                            _fields.add(arr.get(j, arr).toString());
                        }
                    } else {
                        _fields.add(fields.toString());
                    }
                }

                Scriptable version = Context.getCurrentContext().newObject(this.core.getScope());
                for (int j = 0; j < _fields.size(); j++) {
                    String field = _fields.get(j);
                    Object value = doc.get(field) != null ? doc.get(field) : Undefined.instance;
                    version.put(field, version, value);
                }
                count++;
                versions.add(version);
            }
        }
    }

    return versions;
}