Example usage for org.apache.lucene.queryparser.classic MultiFieldQueryParser MultiFieldQueryParser

List of usage examples for org.apache.lucene.queryparser.classic MultiFieldQueryParser MultiFieldQueryParser

Introduction

In this page you can find the example usage for org.apache.lucene.queryparser.classic MultiFieldQueryParser MultiFieldQueryParser.

Prototype

public MultiFieldQueryParser(String[] fields, Analyzer analyzer, Map<String, Float> boosts) 

Source Link

Document

Creates a MultiFieldQueryParser.

Usage

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;// ww  w  .  ja  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);
        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  ww. 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);
        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  av  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);
        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   w ww  .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);
        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  va 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.cohesionforce.search.EMFIndex.java

License:Open Source License

/**
 * Deletes a document matching the EObject
 * //  w w w  . j av a  2 s  .  c o  m
 * @param obj
 * @throws IllegalArgumentException
 *             if EObject is null or the EObject is not contained in a
 *             resource
 * @throws IOException
 *             if there are issues saving the index
 */
public void deleteDocument(EObject obj) throws IllegalArgumentException, IOException {
    if (obj == null) {
        throw new IllegalArgumentException("EObject cannot be null");
    }
    if (obj.eResource() == null) {
        throw new IllegalArgumentException("EObject must be contained in a Resource");
    }

    Query query = findDocument(obj);
    if (query != null) {
        logger.debug("Deleting existing index for {}:{}", obj.eResource().getURI(),
                obj.eResource().getURIFragment(obj));
        writer.deleteDocuments(query);
        if (!holdCommits) {
            writer.commit();
        }
    }

    DirectoryReader reader = DirectoryReader.open(fsDir);

    ArrayList<String> names = new ArrayList<String>();
    for (AtomicReaderContext context : reader.leaves()) {
        for (FieldInfo fi : context.reader().getFieldInfos()) {
            if (!names.contains(fi.name)) {
                names.add(fi.name);
            }

        }
    }
    if (names.size() > 0) {
        MultiFieldQueryParser parser = new MultiFieldQueryParser(version, names.toArray(new String[] {}),
                analyzer);
        try {
            query = parser.parse(obj.eResource().getURIFragment(obj));
            IndexSearcher searcher = new IndexSearcher(reader);

            ScoreDoc[] hits = searcher.search(query, null, MAX_SEARCH_RESULT).scoreDocs;
            for (ScoreDoc hit : hits) {
                Document hitDoc = searcher.doc(hit.doc);
                logger.debug("Hanging reference in: {}",
                        hitDoc.getField(EMFIndexUtil.DOCUMENT_URI_KEY).stringValue());
            }
        } catch (ParseException e) {
            logger.error(e.getMessage());
        }

    }
}

From source file:com.doculibre.constellio.lucene.LuceneSearchResultsProvider.java

License:Open Source License

private synchronized void initIfNecessary() {
    if (topDocs == null) {
        try {/*from w  ww  . j a va2 s .c om*/
            Directory directory = FSDirectory.open(indexDir);
            Analyzer analyzer = analyzerProvider.getAnalyzer(Locale.FRENCH);
            MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_44, searchFields, analyzer);
            parser.setAllowLeadingWildcard(true);
            try {
                luceneQuery = parser.parse(luceneTextQuery);
            } catch (ParseException e) {
                try {
                    luceneQuery = parser.parse(QueryParser.escape(luceneTextQuery));
                } catch (ParseException e1) {
                    throw new RuntimeException(e1);
                }
            }
            indexReader = DirectoryReader.open(directory);
            indexSearcher = new IndexSearcher(indexReader);
            //              Sort sort;
            //              if (sortField != null) {
            //                  sort = new Sort(new SortField(sortField, Locale.CANADA_FRENCH, Boolean.FALSE.equals(sortAscending)));
            //              } else {
            //                  sort = null;
            //              }
            topDocs = indexSearcher.search(luceneQuery, indexReader.maxDoc());
            directory.close();
        } catch (CorruptIndexException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.lucene.index.test.IKAnalyzerdemo.java

License:Apache License

/**
 * /*from   w w  w .j  a  v  a 2 s .  c  om*/
 * ???
 * @param args
 */
public static void main(String[] args) {
    //Lucene Document??
    String fieldName = "text";
    //
    String text1 = "oracle,?";
    String text2 = "?";
    String text3 = "?";

    //IKAnalyzer?
    Analyzer analyzer = new IKAnalyzer();

    Directory directory1 = null;
    Directory directory2 = null;
    IndexWriter iwriter1 = null;
    IndexWriter iwriter2 = null;
    IndexReader ireader1 = null;
    IndexReader ireader2 = null;
    IndexSearcher isearcher = null;
    try {
        //
        directory1 = new RAMDirectory();
        directory2 = new RAMDirectory();

        //?IndexWriterConfig

        IndexWriterConfig iwConfig1 = new IndexWriterConfig(analyzer);
        iwConfig1.setOpenMode(OpenMode.CREATE);

        IndexWriterConfig iwConfig2 = new IndexWriterConfig(analyzer);
        iwConfig2.setOpenMode(OpenMode.CREATE);
        iwriter1 = new IndexWriter(directory1, iwConfig1);
        iwriter2 = new IndexWriter(directory2, iwConfig2);

        //
        Document doc1 = new Document();
        doc1.add(new StringField("ID", "10000", Field.Store.YES));
        doc1.add(new TextField("text1", text1, Field.Store.YES));
        iwriter1.addDocument(doc1);

        Document doc2 = new Document();
        doc2.add(new StringField("ID", "10001", Field.Store.YES));
        doc2.add(new TextField("text2", text2, Field.Store.YES));
        iwriter2.addDocument(doc2);

        iwriter1.close();
        iwriter2.close();

        //?**********************************
        //?   
        ireader1 = DirectoryReader.open(directory1);
        ireader2 = DirectoryReader.open(directory2);

        IndexReader[] mreader = { ireader1, ireader2 };

        MultiReader multiReader = new MultiReader(mreader);

        isearcher = new IndexSearcher(multiReader);

        String keyword = "?";
        //QueryParser?Query
        String[] fields = { "text1", "text2" };

        Map<String, Float> boosts = new HashMap<String, Float>();
        boosts.put("text1", 5.0f);
        boosts.put("text2", 2.0f);
        /**MultiFieldQueryParser??? 
         * */
        MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, analyzer, boosts);
        Query query = parser.parse(keyword);

        System.out.println("Query = " + query);

        //?5?
        TopDocs topDocs = isearcher.search(query, 5);
        System.out.println("" + topDocs.totalHits);
        //
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (int i = 0; i < topDocs.totalHits; i++) {
            Document targetDoc = isearcher.doc(scoreDocs[i].doc);
            System.out.println("" + targetDoc.toString());
        }

    } catch (CorruptIndexException e) {
        e.printStackTrace();
    } catch (LockObtainFailedException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    } finally {
        if (ireader1 != null) {
            try {
                ireader1.close();
                ireader2.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (directory1 != null) {
            try {
                directory1.close();
                directory2.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.mathworks.xzheng.advsearching.MultiFieldQueryParserTest.java

License:Apache License

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

    Directory dir = TestUtil.getBookIndexDirectory();
    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(dir));
    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

    dir.close();/*  w  ww.j ava 2  s . co m*/
}

From source file:com.meltmedia.cadmium.search.SearchService.java

License:Apache License

QueryParser createParser(Analyzer analyzer) {
    return new MultiFieldQueryParser(Version.LUCENE_43, new String[] { "title", "content" }, analyzer);
}