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) 

Source Link

Document

Creates a searcher searching the provided top-level IndexReaderContext .

Usage

From source file:com.bitranger.parknshop.common.fulltext.SearchShop.java

License:Open Source License

public List<PsShop> search(String value) throws IOException {
    // get the index
    IndexReader reader = DirectoryReader.open(FSDirectory.open(BuildIndexForItem.getIndexFile()));
    // use this to search
    IndexSearcher indexSearcher = new IndexSearcher(reader);
    // use the queryParser to wrap your request
    QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_45, queryString, analyzer);

    List<PsShop> shop = new ArrayList<PsShop>();

    Query query = null;//w w w . ja va 2 s .  c  o  m
    try {
        query = queryParser.parse("title:" + value + "~");
    } catch (ParseException e) {
        e.printStackTrace();
    }
    // we get what we want in the topdocs
    TopDocs topDocs = indexSearcher.search(query, 25);
    System.out.println(":" + topDocs.totalHits + "");
    // ScoreDoc[] scoreDoc = topDocs.scoreDocs;
    for (int i = 0; i < topDocs.scoreDocs.length; i++) {
        Document resultDocument = indexSearcher.doc(topDocs.scoreDocs[i].doc);
        PsShop myshop = new PsShop();
        myshop.setId(Integer.valueOf(resultDocument.get((indexField[0]))));
        myshop.setName(resultDocument.get((indexField[2])));
        myshop.setStatus(Short.valueOf(resultDocument.get((indexField[3]))));
        myshop.setIntroduction(resultDocument.get((indexField[4])));
        myshop.setVote(Double.valueOf(resultDocument.get((indexField[6]))));
        shop.add(myshop);

    }

    return shop;
}

From source file:com.bizosys.hsearch.dictionary.DictionaryValues.java

License:Apache License

public int load(Set<String> wordDescription, Writer outputWriter, Analyzer analyzer) throws Exception {
    if (null != reader) {
        try {/*w  w w.  ja  v  a 2  s .  c  o m*/
            reader.close();
            if (null != searcher)
                searcher.close();
        } catch (Exception ex) {
        }
    }

    this.idx = new RAMDirectory();
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_35, analyzer);
    IndexWriter writer = new IndexWriter(this.idx, indexWriterConfig);

    int linesAdded = 0;
    for (String line : wordDescription) {
        writer.addDocument(createDocument(line));
        linesAdded++;
    }
    if (null != outputWriter)
        outputWriter.append("----------\n</BR>Total  Lines Loaded :  ")
                .append(new Integer(linesAdded).toString());

    writer.close();
    reader = IndexReader.open(this.idx);
    searcher = new IndexSearcher(reader);
    return linesAdded;
}

From source file:com.bizosys.hsearch.dictionary.DictionaryValues.java

License:Apache License

public void loadAndOpen(String file, Writer outputWriter) throws Exception {
    if (null != reader) {
        try {/*from w  w w  . ja  v a 2  s.  c  o  m*/
            reader.close();
            if (null != searcher)
                searcher.close();
        } catch (Exception ex) {
        }

    }

    this.idx = loadFile(file, outputWriter);
    reader = IndexReader.open(idx);
    searcher = new IndexSearcher(reader);
}

From source file:com.bluecubs.xinco.index.XincoIndexer.java

License:Apache License

public static synchronized Vector findXincoCoreData(String s, int l, XincoDBManager dbm) {

    int i = 0;/*  w ww .j  a va2 s  .co m*/
    Vector v = new Vector();
    Searcher searcher = null;

    try {

        searcher = new IndexSearcher(dbm.config.FileIndexPath);
        Analyzer analyzer = new StandardAnalyzer();

        //add language to query
        if (l != 0) {
            s = s + " AND language:" + l;
        }
        Query query = QueryParser.parse(s, "designation", analyzer);

        Hits hits = searcher.search(query);

        for (i = 0; i < hits.length(); i++) {
            try {
                v.addElement(new XincoCoreDataServer(Integer.parseInt(hits.doc(i).get("id")), dbm));
            } catch (Exception xcde) {
                // don't add non-existing data
            }
            if (i >= dbm.config.MaxSearchResult) {
                break;
            }
        }

        searcher.close();

    } catch (Exception e) {
        if (searcher != null) {
            try {
                searcher.close();
            } catch (Exception se) {
            }
        }
        return null;
    }

    return v;
}

From source file:com.bluedragon.search.collection.Collection.java

License:Open Source License

/**
 * Retrieves the IndexSearcher object.  This object is cached against this collection and is intended
 * to be used amongst several threads.   This is the recommended approach.
 * /*from ww  w  .  ja  v  a  2s  .c  o m*/
 * If new content is added to this collection then it won't be available for searching until a new
 * indexsearcher is created
 * 
 * @return
 * @throws CorruptIndexException
 * @throws IOException
 */
public synchronized IndexSearcher getIndexSearcher() throws CorruptIndexException, IOException {
    lastUsed = System.currentTimeMillis();

    if (indexsearcher != null)
        return indexsearcher;

    setDirectory();
    indexsearcher = new IndexSearcher(DirectoryReader.open(directory));

    totalDocs = indexsearcher.getIndexReader().numDocs();

    return indexsearcher;
}

From source file:com.browseengine.bobo.test.section.TestSectionSearch.java

License:Apache License

protected void setUp() throws Exception {
    directory = new RAMDirectory();
    analyzer = new WhitespaceAnalyzer();
    writer = new IndexWriter(directory, analyzer, true, MaxFieldLength.UNLIMITED);
    addDoc("1", new String[] { "aa", "bb" }, new String[] { "aaa", "aaa" }, new int[] { 100, 200 });
    addDoc("2", new String[] { "aa", "bb" }, new String[] { "aaa", "bbb" }, new int[] { 200, 200 });
    addDoc("3", new String[] { "aa", "bb" }, new String[] { "bbb", "aaa" }, new int[] { 300, 300 });
    addDoc("3", new String[] { "bb", "aa" }, new String[] { "bbb", "bbb" }, new int[] { 300, 400 });
    addDoc("3", new String[] { "bb", "aa" }, new String[] { "aaa", "ccc" }, new int[] { 300, 500 });
    writer.commit();//from  w  w w  .ja v  a 2s  . c om
    IndexReader reader = IndexReader.open(directory, true);
    searcher = new IndexSearcher(reader);
    IndexReader readerWithCache = new IndexReaderWithMetaDataCache(reader);
    searcherWithCache = new IndexSearcher(readerWithCache);
}

From source file:com.browseengine.local.service.geocode.NameSearcher.java

License:Open Source License

public NameSearcher(File f) throws IOException {
    try {/* ww w  .  j av  a2 s  . c  o m*/
        _reader = IndexReader.open(f);
        _searcher = new IndexSearcher(_reader);
        _maxDoc = _reader.maxDoc();
    } catch (IOException ioe) {
        try {
            close();
        } catch (IOException ioe2) {
            LOGGER.warn("trouble closing NameSearcher, after trouble opening it", ioe2);
        }
        throw ioe;
    }
}

From source file:com.browseengine.local.service.geocode.RangeSearcher.java

License:Open Source License

public RangeSearcher(File f) throws IOException {
    try {/*from   w w w.j  a  v  a2  s . co m*/
        _reader = IndexReader.open(f);
        _searcher = new IndexSearcher(_reader);
        _maxDoc = _reader.maxDoc();
        _isLeft = loadBitField(RangesFields.LEFT.getField());
        _isNumeric = loadBitField(RangesFields.IS_NUMERIC.getField());
    } catch (IOException ioe) {
        try {
            close();
        } catch (IOException ioe2) {
            LOGGER.warn("trouble closing NameSearcher, after trouble opening it", ioe2);
        }
        throw ioe;
    }
}

From source file:com.browseengine.local.service.geocode.SegmentSearcher.java

License:Open Source License

public SegmentSearcher(File f) throws IOException {
    try {/* w w w  . ja v  a  2  s  .co m*/
        _reader = IndexReader.open(f);
        _searcher = new IndexSearcher(_reader);
        _maxDoc = _reader.maxDoc();
    } catch (IOException ioe) {
        try {
            close();
        } catch (IOException ioe2) {
            LOGGER.warn("trouble closing SegmentsSearcher, after trouble opening it", ioe2);
        }
        throw ioe;
    }
}

From source file:com.browseengine.local.service.geosearch.GeoSearchImpl.java

License:Open Source License

public GeoSearchImpl(GeoCode geocoder, File f) throws IOException, GeoSearchingException {
    try {/* w  ww . j  a  va 2  s .  c  o  m*/
        _geocoder = geocoder;
        _reader = IndexReader.open(f);
        _searcher = new IndexSearcher(_reader);
        _lons = loadDegreeFieldIntoInt(_reader, GeoSearchFields.LON.getField());
        checkSorted(_lons);
        _lats = loadDegreeFieldIntoInt(_reader, GeoSearchFields.LAT.getField());
        _maxDoc = _reader.maxDoc();
    } catch (IOException ioe) {
        close();
        throw ioe;
    }
}