日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Lucene6.0 创建索引及查询text简单实例

發(fā)布時間:2024/1/17 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Lucene6.0 创建索引及查询text简单实例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

1.pom.xml

<project?xmlns="http://maven.apache.org/POM/4.0.0"?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0?http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>hui</groupId><artifactId>Lucene</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>Lucene</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-core</artifactId><version>6.0.0</version></dependency><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-analyzers-common</artifactId><version>6.0.0</version></dependency><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-queryparser</artifactId><version>6.0.0</version></dependency><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-highlighter</artifactId><version>6.0.0</version></dependency></dependencies> </project>

2.LuceneTest.java:

package?luceneTest;import?java.io.IOException;import?org.apache.lucene.analysis.Analyzer; import?org.apache.lucene.analysis.standard.StandardAnalyzer; import?org.apache.lucene.document.Document; import?org.apache.lucene.document.Field; import?org.apache.lucene.document.TextField; import?org.apache.lucene.index.DirectoryReader; import?org.apache.lucene.index.IndexWriter; import?org.apache.lucene.index.IndexWriterConfig; import?org.apache.lucene.queryparser.classic.ParseException; import?org.apache.lucene.queryparser.classic.QueryParser; import?org.apache.lucene.search.IndexSearcher; import?org.apache.lucene.search.Query; import?org.apache.lucene.search.ScoreDoc; import?org.apache.lucene.store.Directory; import?org.apache.lucene.store.RAMDirectory;public?class?TextTest?{public?static?void?main(String[]?args)?throws?IOException,?ParseException?{Analyzer?analyzer?=?new?StandardAnalyzer();//?Store?the?index?in?memory:Directory?directory?=?new?RAMDirectory();//?To?store?an?index?on?disk,?use?this?instead://?Directory?directory?=?FSDirectory.open("/tmp/testindex");IndexWriterConfig?config?=?new?IndexWriterConfig(analyzer);IndexWriter?writer?=?new?IndexWriter(directory,?config);String[]?texts?=?new?String[]?{?"This?is?the?text?to?be?indexed","That?is?an?cat",?"he?is?shit",?"stupid?women,so?bitch"?};for?(String?text?:?texts)?{Document?doc?=?new?Document();doc.add(new?Field("fieldname",?text,?TextField.TYPE_STORED));writer.addDocument(doc);}writer.close();//?Now?search?the?index:DirectoryReader?reader?=?DirectoryReader.open(directory);IndexSearcher?searcher?=?new?IndexSearcher(reader);QueryParser?parser?=?new?QueryParser("fieldname",?analyzer);Query?query?=?parser.parse("stupid");//?if?you?parse("the"),then?hits.length=0,because?"the"?is//?stopWord,others?like?"to"?"be",also?stopWordScoreDoc[]?hits?=?searcher.search(query,?5).scoreDocs;System.out.println(hits.length);//?Iterate?through?the?results:for?(int?i?=?0;?i?<?hits.length;?i++)?{Document?hitDoc?=?searcher.doc(hits[i].doc);System.out.println(hitDoc.get("fieldname"));}reader.close();directory.close();}}


轉(zhuǎn)載于:https://my.oschina.net/u/2430057/blog/667656

總結(jié)

以上是生活随笔為你收集整理的Lucene6.0 创建索引及查询text简单实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。