欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

为开发人员而准备的搜索工具,开发人员搜索工具,public stati

来源: javaer 分享于  点击 49957 次 点评:206

为开发人员而准备的搜索工具,开发人员搜索工具,public stati


public static void main(String[] args) throws CorruptIndexException, LockObtainFailedException, IOException, ParseException {        Analyzer analyzer = new SimpleAnalyzer();        // Store the index in memory:        Directory directory = new RAMDirectory();        // To store an index on disk, use this instead:        //Directory directory = FSDirectory.open("/tmp/testindex");        IndexWriter iwriter = new IndexWriter(directory, analyzer, true, new IndexWriter.MaxFieldLength(25000));        Document doc = new Document();        String text = "This is the text to is be indexed.";        doc.add(new Field("fieldname", text, Field.Store.YES, Field.Index.ANALYZED));        iwriter.addDocument(doc);        iwriter.close();        // Now search the index:        IndexSearcher isearcher = new IndexSearcher(directory, true); // read-only=true        // Parse a simple query that searches for "text":        QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, "fieldname", analyzer);        Query query = parser.parse("is");        ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;        System.out.println(hits.length);        // Iterate through the results:        for (int i = 0; i < hits.length; i++) {          Document hitDoc = isearcher.doc(hits[i].doc);          System.out.println(hitDoc.get("fieldname"));        }        isearcher.close();        directory.close();    }//该片段来自于http://byrx.net
相关栏目:

用户点评