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.bewsia.script.LuceneHandler.java

License:Open Source License

public void load(String id, SEntity src) {
    try {/*from   w  ww . j a  v a2 s.co m*/
        IndexReader reader = IndexReader.open(FSDirectory.open(new File(dirIndex)));
        IndexSearcher searcher = new IndexSearcher(reader);
        TopDocs td = searcher.search(new TermQuery(new Term(SEntity.ID, id)), 1);
        if (td.totalHits > 0) {
            Document doc = searcher.doc(td.scoreDocs[0].doc);
            if (allowLoad(id, doc.get(SEntity.KIND))) {
                src.setSchema(doc.get(SEntity.SCHEMA));
                read(src, doc);
            }
        }
        searcher.close();
        reader.close();
    } catch (Exception e) {
    }
}

From source file:com.bewsia.script.LuceneHandler.java

License:Open Source License

public int count(String kind, Query query, Filter filter, Sort sort, int max) {
    int tag = 0;/*ww  w.j a v  a 2 s . c o  m*/
    try {
        IndexReader reader = IndexReader.open(FSDirectory.open(new File(dirIndex)));
        IndexSearcher searcher = new IndexSearcher(reader);
        BooleanQuery boolQuery = new BooleanQuery();
        boolQuery.add(new BooleanClause(new TermQuery(new Term(SEntity.KIND, kind)), Occur.MUST));
        if (query != null) {
            boolQuery.add(new BooleanClause(query, Occur.MUST));
        }
        TopDocs td = null;
        if (filter != null && sort != null) {
            td = searcher.search(boolQuery, filter, max, sort);
        } else if (filter != null) {
            td = searcher.search(boolQuery, filter, max);
        } else if (sort != null) {
            td = searcher.search(boolQuery, max, sort);
        } else {
            td = searcher.search(boolQuery, max);
        }
        tag = td.totalHits;
        searcher.close();
        reader.close();
    } catch (Exception e) {
    }
    return tag;
}

From source file:com.bewsia.script.LuceneHandler.java

License:Open Source License

public List<SEntity> search(String kind, Query query, Filter filter, Sort sort, int max) {
    List<SEntity> tag = new ArrayList<SEntity>();
    try {/*from w w w.j  ava 2s  . co  m*/
        IndexReader reader = IndexReader.open(FSDirectory.open(new File(dirIndex)));
        IndexSearcher searcher = new IndexSearcher(reader);
        BooleanQuery boolQuery = new BooleanQuery();
        boolQuery.add(new BooleanClause(new TermQuery(new Term(SEntity.KIND, kind)), Occur.MUST));
        if (query != null) {
            boolQuery.add(new BooleanClause(query, Occur.MUST));
        }
        TopDocs td = null;
        if (filter != null && sort != null) {
            td = searcher.search(boolQuery, filter, max, sort);
        } else if (filter != null) {
            td = searcher.search(boolQuery, filter, max);
        } else if (sort != null) {
            td = searcher.search(boolQuery, max, sort);
        } else {
            td = searcher.search(boolQuery, max);
        }
        for (int i = 0; i < td.totalHits; i++) {
            SEntity item = new SEntity(this);
            Document doc = searcher.doc(td.scoreDocs[i].doc);
            item.setSchema(doc.get(SEntity.SCHEMA));
            read(item, doc);
            tag.add(item);
        }
        searcher.close();
        reader.close();
    } catch (Exception e) {
    }
    return tag;
}

From source file:com.bewsia.script.LuceneHandler.java

License:Open Source License

public List<SEntity> search(String kind, Query query, Filter filter, Sort sort, int pagesize, int pageno) {
    List<SEntity> tag = new ArrayList<SEntity>();
    try {/*w  w  w .j  av a 2 s .co m*/
        IndexReader reader = IndexReader.open(FSDirectory.open(new File(dirIndex)));
        IndexSearcher searcher = new IndexSearcher(reader);
        BooleanQuery boolQuery = new BooleanQuery();
        boolQuery.add(new BooleanClause(new TermQuery(new Term(SEntity.KIND, kind)), Occur.MUST));
        if (query != null) {
            boolQuery.add(new BooleanClause(query, Occur.MUST));
        }
        if (pagesize <= 0)
            pagesize = 10;
        if (pageno <= 0)
            pageno = 1;
        int max = pageno * pagesize;
        TopDocs td = null;
        if (filter != null && sort != null) {
            td = searcher.search(boolQuery, filter, max, sort);
        } else if (filter != null) {
            td = searcher.search(boolQuery, filter, max);
        } else if (sort != null) {
            td = searcher.search(boolQuery, max, sort);
        } else {
            td = searcher.search(boolQuery, max);
        }
        for (int i = (pageno - 1) * pagesize; i < td.totalHits && i < max; i++) {
            SEntity item = new SEntity(this);
            Document doc = searcher.doc(td.scoreDocs[i].doc);
            item.setSchema(doc.get(SEntity.SCHEMA));
            read(item, doc);
            tag.add(item);
        }
        searcher.close();
        reader.close();
    } catch (Exception e) {
    }
    return tag;
}

From source file:com.bewsia.script.LuceneHandler.java

License:Open Source License

protected void deleteEntity(String id) {
    if (id.length() == 0)
        return;/*from  w w  w.j  a  v a2s  .  c  o  m*/
    String kind = "";

    try {
        IndexReader reader = IndexReader.open(FSDirectory.open(new File(dirIndex)));
        IndexSearcher searcher = new IndexSearcher(reader);
        TopDocs td = searcher.search(new TermQuery(new Term(SEntity.ID, id)), 1);
        if (td.totalHits > 0) {
            Document doc = searcher.doc(td.scoreDocs[0].doc);
            kind = doc.get(SEntity.KIND);
        }
        searcher.close();
        reader.close();
    } catch (Exception e) {
    }
    if (kind.length() == 0)
        return;
    if (!allowDelete(id, kind))
        return;

    try {
        if (!kind.equals(KIND_QUOTA)) {
            if (!quotaDelete(id, kind))
                return;
        }
        removeBackup(id, kind);
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36, analyzer);
        iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        IndexWriter writer = new IndexWriter(FSDirectory.open(new File(dirIndex)), iwc);
        writer.deleteDocuments(new Term(SEntity.ID, id));
        writer.close();
    } catch (Exception e) {
    }
}

From source file:com.bitplan.pdfindex.Pdfindexer.java

License:Apache License

/**
 * http://lucene.472066.n3.nabble.com/search-trough-single-pdf-document-
 * return -page-number-td598698.html//from w  w  w.  ja v  a2s.  c om
 * http://tarikguelzim.wordpress.com/2008/09/13/creating
 * -a-search-engine-to-parse-index-and-search-pdf-file-content/ search the
 * 
 * @return hits for the query
 * @param qString - the query string
 * @param idxField - the index field
 * @param limit - the maximum number of results
 * @return - the TopDocs retrieved from the index
 */
protected TopDocs searchIndex(String qString, String idxField, int limit) {
    TopDocs topDocs = null;
    Directory fsDir = null;
    Query query = null;
    QueryParser qParser = null;

    try {
        // query index
        if (debug) {
            System.out.println("Searching for " + qString + " in " + indexFile + "  field: " + idxField);
        }

        // create index searcher
        fsDir = FSDirectory.getDirectory(indexFile);
        searcher = new IndexSearcher(fsDir);
        // parse query
        qParser = new QueryParser(idxField, new StandardAnalyzer());
        if (this.autoescape)
            qString = qString.replace(" ", "\\ ");
        query = qParser.parse(qString);
        // query = QueryParser.parse(qString, idxField, new
        // StandardAnalyzer());
        if (debug)
            System.out.println("query: '" + query.toString() + "'");
        // search
        topDocs = searcher.search(query, limit);

    } catch (IOException ex) {
        System.err.println("Index file" + indexFile + " doesnt exist");
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        System.exit(1);
    } catch (ParseException ex) {
        System.err.println("Error while parsing the index.");
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
    }
    return topDocs;
}

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

License:Open Source License

public List<PsCustomer> 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<PsCustomer> customer = new ArrayList<PsCustomer>();

    Query query = null;//from w  w  w . jav  a  2s  .com
    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);
        PsCustomer mycustomer = new PsCustomer();
        mycustomer.setNickname(resultDocument.get((indexField[0])));
        mycustomer.setEmail(resultDocument.get((indexField[1])));
        mycustomer.setPassword(resultDocument.get((indexField[2])));
        mycustomer.setGender(Short.valueOf(resultDocument.get((indexField[3]))));
        mycustomer.setName(resultDocument.get((indexField[4])));
        customer.add(mycustomer);

    }
    return customer;
}

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

License:Open Source License

public List<PsItem> 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<PsItem> item = new ArrayList<PsItem>();

    Query query = null;/*w  w w.ja  va2 s .  c om*/
    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);
        PsItem myitem = new PsItem();
        myitem.setName(resultDocument.get((indexField[0])));
        myitem.setIntroduction(resultDocument.get((indexField[1])));
        myitem.setPrice(Double.valueOf(resultDocument.get((indexField[2]))));
        myitem.setUrlPicture(resultDocument.get((indexField[3])));
        myitem.setExtra1(resultDocument.get((indexField[4])));
        myitem.setExtra2(resultDocument.get((indexField[5])));
        myitem.setCountPurchase(Integer.valueOf(resultDocument.get((indexField[6]))));
        myitem.setCountFavourite(Integer.valueOf(resultDocument.get((indexField[7]))));
        myitem.setCountClick(Integer.valueOf(resultDocument.get((indexField[8]))));
        myitem.setVote(Double.valueOf(resultDocument.get((indexField[9]))));
        item.add(myitem);

    }
    return item;
}

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

License:Open Source License

public List<PsOrder> 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<PsOrder> order = new ArrayList<PsOrder>();

    Query query = null;/*from  w w  w .  j a  v a2  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);
        PsOrder myorder = new PsOrder();
        myorder.setId(Integer.valueOf(resultDocument.get((indexField[0]))));
        myorder.setStatus(Short.valueOf(resultDocument.get((indexField[3]))));
        myorder.setTrackingNumber(resultDocument.get((indexField[4])));
        myorder.setPriceTotal(Double.valueOf(resultDocument.get((indexField[9]))));
        order.add(myorder);

    }
    return order;
}

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

License:Open Source License

public List<PsSeller> 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<PsSeller> seller = new ArrayList<PsSeller>();

    Query query = null;/*from ww  w  . j av a  2s. co 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);
        PsSeller myseller = new PsSeller();
        myseller.setId(Integer.valueOf(resultDocument.get((indexField[0]))));
        myseller.setNickname(resultDocument.get((indexField[1])));
        myseller.setPersonIdNum(resultDocument.get((indexField[2])));
        myseller.setEmail(resultDocument.get((indexField[3])));
        myseller.setPassword(resultDocument.get((indexField[4])));
        myseller.setStatus(Short.valueOf(resultDocument.get((indexField[5]))));
        seller.add(myseller);

    }
    return seller;
}