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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

lucene学习的小结

發布時間:2023/12/18 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 lucene学习的小结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

pom.xml設置

<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-core</artifactId><version>5.3.1</version></dependency><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-queryparser</artifactId><version>5.3.1</version></dependency><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-analyzers-common</artifactId><version>5.3.1</version></dependency><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-analyzers-smartcn</artifactId><version>5.3.1</version></dependency><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-highlighter</artifactId><version>5.3.1</version></dependency>

?

生成索引IndexingTest.java

package com.chabansheng.lucene;import java.nio.file.Paths;import ...;public class IndexingTest {private String ids[]={"1","2","3","4"};private String authors[]={"Jack","Marry","John","Json"};private String positions[]={"accounting","technician","salesperson","boss"};private String titles[]={"Java is a good language.","Java is a cross platform language","Java powerful","You should learn java"};private String contents[]={"If possible, use the same JRE major version at both index and search time.","When upgrading to a different JRE major version, consider re-indexing. ","Different JRE major versions may implement different versions of Unicode,","For example: with Java 1.4, `LetterTokenizer` will split around the character U+02C6,"};private Directory dir;/*** 生成索引* @throws Exception*/@Testpublic void index()throws Exception{dir=FSDirectory.open(Paths.get("D:\\lucene3"));//獲取IndexWriter實例Analyzer analyzer=new StandardAnalyzer(); // 標準分詞器IndexWriterConfig iwc=new IndexWriterConfig(analyzer);IndexWriter writer=new IndexWriter(dir, iwc);for(int i=0;i<ids.length;i++){Document doc=new Document();doc.add(new StringField("id", ids[i], Field.Store.YES));doc.add(new StringField("author",authors[i],Field.Store.YES));doc.add(new StringField("position",positions[i],Field.Store.YES));// 加權操作TextField field=new TextField("title", titles[i], Field.Store.YES);if("boss".equals(positions[i])){field.setBoost(1.5f);}doc.add(field);doc.add(new TextField("content", contents[i], Field.Store.NO));writer.addDocument(doc); // 添加文檔 }writer.close();}/*** 查詢索引方式一* @throws Exception*/@Testpublic void search()throws Exception{dir=FSDirectory.open(Paths.get("D:\\lucene"));IndexReader reader=DirectoryReader.open(dir);IndexSearcher is=new IndexSearcher(reader);String searchField="title";String q="java";//Term方式查詢Term t=new Term(searchField,q);Query query=new TermQuery(t);TopDocs hits=is.search(query, 10);System.out.println("匹配 '"+q+"',總共查詢到"+hits.totalHits+"個文檔");for(ScoreDoc scoreDoc:hits.scoreDocs){Document doc=is.doc(scoreDoc.doc);System.out.println(doc.get("author"));}reader.close();}}

查詢索引方式二Searcher.java

package com.chabansheng.lucene;import java.io.StringReader; import java.nio.file.Paths;import ...;public class Searcher {public static void search(String indexDir,String q)throws Exception{Directory dir=FSDirectory.open(Paths.get(indexDir));IndexReader reader=DirectoryReader.open(dir);IndexSearcher is=new IndexSearcher(reader);//QueryParser查詢方式// Analyzer analyzer=new StandardAnalyzer(); // 標準分詞器SmartChineseAnalyzer analyzer=new SmartChineseAnalyzer();QueryParser parser=new QueryParser("desc", analyzer);Query query=parser.parse(q);TopDocs hits=is.search(query, 10); //高亮行號QueryScorer scorer=new QueryScorer(query);Fragmenter fragmenter=new SimpleSpanFragmenter(scorer);SimpleHTMLFormatter simpleHTMLFormatter=new SimpleHTMLFormatter("<b><font color='red'>","</font></b>");Highlighter highlighter=new Highlighter(simpleHTMLFormatter, scorer);highlighter.setTextFragmenter(fragmenter);for(ScoreDoc scoreDoc:hits.scoreDocs){Document doc=is.doc(scoreDoc.doc);System.out.println(doc.get("city"));System.out.println(doc.get("desc"));String desc=doc.get("desc");if(desc!=null){TokenStream tokenStream=analyzer.tokenStream("desc", new StringReader(desc));System.out.println(highlighter.getBestFragment(tokenStream, desc));}}reader.close();}public static void main(String[] args) {String indexDir="D:\\lucene2";String q="南京文明";try {search(indexDir,q);} catch (Exception e) {// TODO Auto-generated catch block e.printStackTrace();}} }

?

package com.chabansheng.lucene;
import java.nio.file.Paths;
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.StringField;import org.apache.lucene.document.TextField;import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.index.Term;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Query;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TermQuery;import org.apache.lucene.search.TopDocs;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.junit.Test;
public class IndexingTest {
private String ids[]={"1","2","3","4"};private String authors[]={"Jack","Marry","John","Json"};private String positions[]={"accounting","technician","salesperson","boss"};private String titles[]={"Java is a good language.","Java is a cross platform language","Java powerful","You should learn java"};private String contents[]={"If possible, use the same JRE major version at both index and search time.","When upgrading to a different JRE major version, consider re-indexing. ","Different JRE major versions may implement different versions of Unicode,","For example: with Java 1.4, `LetterTokenizer` will split around the character U+02C6,"};private Directory dir;/** * 生成索引 * @throws Exception */@Testpublic void index()throws Exception{dir=FSDirectory.open(Paths.get("D:\\lucene3"));//獲取IndexWriter實例Analyzer analyzer=new StandardAnalyzer(); // 標準分詞器IndexWriterConfig iwc=new IndexWriterConfig(analyzer);IndexWriter writer=new IndexWriter(dir, iwc);for(int i=0;i<ids.length;i++){Document doc=new Document();doc.add(new StringField("id", ids[i], Field.Store.YES));doc.add(new StringField("author",authors[i],Field.Store.YES));doc.add(new StringField("position",positions[i],Field.Store.YES));// 加權操作TextField field=new TextField("title", titles[i], Field.Store.YES);if("boss".equals(positions[i])){field.setBoost(1.5f);}doc.add(field);doc.add(new TextField("content", contents[i], Field.Store.NO));writer.addDocument(doc); // 添加文檔}writer.close();}
/** * 查詢 * @throws Exception */@Testpublic void search()throws Exception{dir=FSDirectory.open(Paths.get("D:\\lucene3"));IndexReader reader=DirectoryReader.open(dir);IndexSearcher is=new IndexSearcher(reader);String searchField="title";String q="java";//Term方式查詢Term t=new Term(searchField,q);Query query=new TermQuery(t);TopDocs hits=is.search(query, 10);System.out.println("匹配 '"+q+"',總共查詢到"+hits.totalHits+"個文檔");for(ScoreDoc scoreDoc:hits.scoreDocs){Document doc=is.doc(scoreDoc.doc);System.out.println(doc.get("author"));}reader.close();}}

?

轉載于:https://www.cnblogs.com/375163374lsb/p/10542985.html

總結

以上是生活随笔為你收集整理的lucene学习的小结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。