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

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

Introduction

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

Prototype

public <C extends Collector, T> T search(Query query, CollectorManager<C, T> collectorManager)
        throws IOException 

Source Link

Document

Lower-level search API.

Usage

From source file:com.bdaum.zoom.lal.internal.lucene.Lucene.java

License:Open Source License

private static Document getDocumentById(IndexReader indexReader, String searchString) throws IOException {
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    Analyzer analyzer = new KeywordAnalyzer();
    QueryParser queryParser = new QueryParser(DocumentBuilder.FIELD_NAME_IDENTIFIER, analyzer);
    try {//from  ww w .  jav a2 s  .c om
        Query query = queryParser.parse(searchString);
        TopDocs topdocs = indexSearcher.search(query, 1);
        if (topdocs.totalHits > 0)
            return indexReader.document(topdocs.scoreDocs[0].doc);
    } catch (org.apache.lucene.queryparser.classic.ParseException e) {
        // should never happen
    }
    return null;
}

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

License:Open Source License

public boolean exists(String id) {
    boolean tag = false;
    if (id.length() == 0)
        return tag;
    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) {
            tag = true;
        }
        searcher.close();
        reader.close();
    } catch (Exception e) {
    }

    return tag;
}

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

License:Open Source License

public void load(String id, SEntity src) {
    try {/*from w w w  . j av a 2  s.c o  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

protected void deleteEntity(String id) {
    if (id.length() == 0)
        return;// w  ww.  jav  a2  s.co 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.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   www. j  av a  2s .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);
        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;//from w w w. ja v 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);
        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;// w  w  w.ja v  a2s  . 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);
        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;
}

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;/*from w  w w .  j  a  v  a  2 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);
        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.bluedragon.search.search.QueryRun.java

License:Open Source License

public void run() throws CorruptIndexException, Exception {
    int potentialRows = 0;

    Iterator<Collection> it = queryAttributes.getCollectionIterator();

    while (it.hasNext() && queryResultData.getSize() <= queryAttributes.getMaxRows()) {
        Collection collection = it.next();

        IndexSearcher searcher = collection.getIndexSearcher();
        TopDocs hits = searcher.search(queryAttributes.getQuery(), collection.getTotalDocs());

        ScoreDoc[] scorehits = hits.scoreDocs;
        for (int x = 0; x < scorehits.length; x++) {
            if (scorehits[x].score > queryAttributes.getMinScore()) {
                potentialRows++;//from w w  w  . j  ava2  s . c  o m

                if (potentialRows >= queryAttributes.getStartRow()) {
                    addRow(searcher, scorehits[x].doc, scorehits[x].score, x, scorehits.length,
                            collection.getTotalDocs());
                    if (queryResultData.getSize() == queryAttributes.getMaxRows())
                        break;
                }

            }
        }
    }

    queryResultData.reset();

    if (uniqueSet != null)
        uniqueSet.clear();
}