分析器spring
public static void main(String[] args) throws IOException { //1.建立一個Analyzer對象 Analyzer analyzer=new StandardAnalyzer(); //2.調用Analyzer對象的tokenStream方法獲取TokenStream對象,此對象包含了全部的分詞結果 TokenStream tokenStream = analyzer.tokenStream("", "The spring Framework provides a comprehensive programming and configuration model."); //3.給tokenStream對象設置一個指針,指針在哪當前就在哪個分詞上 CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class); //4.調用tokenStream對象的reset方法,重置指針,不調用會報錯 tokenStream.reset(); //5.利用while循環,拿到分詞列表的結果 incrementToken方法返回值若是爲false表明讀取完畢 true表明沒有讀取完畢 while (tokenStream.incrementToken()){ System.out.println(charTermAttribute.toString()); } //6.關閉 tokenStream.close(); }
中文分析器apache
<!-- https://mvnrepository.com/artifact/com.jianggujin/IKAnalyzer-lucene --> <dependency> <groupId>com.jianggujin</groupId> <artifactId>IKAnalyzer-lucene</artifactId> <version>8.0.0</version> </dependency>public static void main(String[] args) throws IOException { //1.建立一個Analyzer對象 Analyzer analyzer=new IKAnalyzer(); //2.調用Analyzer對象的tokenStream方法獲取TokenStream對象,此對象包含了全部的分詞結果 TokenStream tokenStream = analyzer.tokenStream("", "五道口課工場梅川酷子梅川酷子梅川酷子呵呵"); //3.給tokenStream對象設置一個指針,指針在哪當前就在哪個分詞上 CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class); //4.調用tokenStream對象的reset方法,重置指針,不調用會報錯 tokenStream.reset(); //5.利用while循環,拿到分詞列表的結果 incrementToken方法返回值若是爲false表明讀取完畢 true表明沒有讀取完畢 while (tokenStream.incrementToken()){ System.out.println(charTermAttribute.toString()); } //6.關閉 tokenStream.close(); } IndexWriter indexWriter=new IndexWriter(directory,new IndexWriterConfig(new IKAnalyzer()));
索引添加ide
@Test public void createDocument() throws IOException { //建立IndexWriter對象 參數一:索引庫位置 參數二:指定配置 IndexWriter indexWriter=new IndexWriter(FSDirectory.open(new File("D:\\Y3\\0225\\LuncenIndex").toPath()), new IndexWriterConfig(new IKAnalyzer())); //建立一個文檔對象 Document document=new Document(); document.add(new TextField("fieldName","hehe.txt", Field.Store.YES)); document.add(new StoredField("fieldPath","c://hehe.txt")); document.add(new LongPoint("fieldSize",123)); document.add(new StoredField("fieldSize",123)); document.add(new TextField("fieldContent","愛上風神股份代號你哦按名單來看積分不到的解決辦法梅川酷子是一個開源基於JAVA語言的 .", Field.Store.YES)); //建立索引,將文檔添加到索引庫當中 indexWriter.addDocument(document); //關閉 indexWriter.close(); }
索引修改:原理-先刪除在添加spa
@Test public void updateDocument() throws IOException { //建立IndexWriter對象 參數一:索引庫位置 參數二:指定配置 IndexWriter indexWriter=new IndexWriter(FSDirectory.open(new File("D:\\Y3\\0225\\LuncenIndex").toPath()), new IndexWriterConfig(new IKAnalyzer())); //建立文檔 Document document=new Document(); document.add(new TextField("fieldName","new.txt", Field.Store.YES)); document.add(new StoredField("fieldPath","c://new.txt")); document.add(new LongPoint("fieldSize",456)); document.add(new StoredField("fieldSize",456)); document.add(new TextField("fieldContent","修改fieldName爲全文檢索的文檔,進行文檔替換,先刪除掉fieldName爲全文檢索的兩個文檔,再添加一個fileName爲new的新文檔", Field.Store.YES)); //修改 參數一爲條件 參數二爲修改的文檔值 indexWriter.updateDocument(new Term("fieldName","全文檢索"),document); //關閉 indexWriter.close(); }
索引刪除指針
@Test public void deleteAllDocument() throws IOException { //建立IndexWriter對象 參數一:索引庫位置 參數二:指定配置 IndexWriter indexWriter=new IndexWriter(FSDirectory.open(new File("D:\\Y3\\0225\\LuncenIndex").toPath()), new IndexWriterConfig(new IKAnalyzer())); //刪除索引 indexWriter.deleteAll(); //關閉 indexWriter.close(); }
根據域和關鍵詞刪除code
@Test public void termQuery() throws IOException { //建立查詢條件 Query query=new TermQuery(new Term("fieldName","new")); //執行查詢 TopDocs topDocs = indexSearcher.search(query, 10); System.out.println("返回的文檔個數:"+topDocs.totalHits); //獲取到文檔集合 ScoreDoc [] scoreDocs=topDocs.scoreDocs; for (ScoreDoc doc:scoreDocs) { //獲取到文檔 Document document = indexSearcher.doc(doc.doc); //獲取到文檔域中數據 System.out.println("fieldName:"+document.get("fieldName")); System.out.println("fieldPath:"+document.get("fieldPath")); System.out.println("fieldSize:"+document.get("fieldSize")); System.out.println("fieldContent:"+document.get("fieldContent")); } //關閉 indexReader.close(); }
範圍查找對象
@Test public void RangeQuery() throws IOException { //設置範圍搜索的條件 參數一範圍所在的域 Query query=LongPoint.newRangeQuery("fieldSize",0,10); //查詢 TopDocs topDocs = indexSearcher.search(query, 10); System.out.println("返回的文檔個數:"+topDocs.totalHits); //獲取到文檔集合 ScoreDoc [] scoreDocs=topDocs.scoreDocs; for (ScoreDoc doc:scoreDocs) { //獲取到文檔 Document document = indexSearcher.doc(doc.doc); //獲取到文檔域中數據 System.out.println("fieldName:"+document.get("fieldName")); System.out.println("fieldPath:"+document.get("fieldPath")); System.out.println("fieldSize:"+document.get("fieldSize")); System.out.println("fieldContent:"+document.get("fieldContent")); } //關閉 indexReader.close(); }
TermQuery:根據域和關鍵詞進行搜索blog
@Test public void termQuery() throws IOException { //建立查詢條件 Query query=new TermQuery(new Term("fieldName","new")); //執行查詢 TopDocs topDocs = indexSearcher.search(query, 10); System.out.println("返回的文檔個數:"+topDocs.totalHits); //獲取到文檔集合 ScoreDoc [] scoreDocs=topDocs.scoreDocs; for (ScoreDoc doc:scoreDocs) { //獲取到文檔 Document document = indexSearcher.doc(doc.doc); //獲取到文檔域中數據 System.out.println("fieldName:"+document.get("fieldName")); System.out.println("fieldPath:"+document.get("fieldPath")); System.out.println("fieldSize:"+document.get("fieldSize")); System.out.println("fieldContent:"+document.get("fieldContent")); } //關閉 indexReader.close(); }
RangeQuery:範圍搜索索引
前提:建立文檔是保存範圍token
@Test public void RangeQuery() throws IOException { //設置範圍搜索的條件 參數一範圍所在的域 Query query=LongPoint.newRangeQuery("fieldSize",0,50); //查詢 TopDocs topDocs = indexSearcher.search(query, 10); System.out.println("返回的文檔個數:"+topDocs.totalHits); //獲取到文檔集合 ScoreDoc [] scoreDocs=topDocs.scoreDocs; for (ScoreDoc doc:scoreDocs) { //獲取到文檔 Document document = indexSearcher.doc(doc.doc); //獲取到文檔域中數據 System.out.println("fieldName:"+document.get("fieldName")); System.out.println("fieldPath:"+document.get("fieldPath")); System.out.println("fieldSize:"+document.get("fieldSize")); System.out.println("fieldContent:"+document.get("fieldContent")); } //關閉 indexReader.close(); }
QueryParser:匹配一行數據
<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-queryparser --> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-queryparser</artifactId> <version>7.4.0</version> </dependency> @Test public void queryparser() throws IOException, ParseException { //建立一個QueryParser對象 參數一:查詢的域 參數二:使用哪一種分析器 QueryParser parser=new QueryParser("fieldContent",new IKAnalyzer()); //設置匹配的數據條件 Query query = parser.parse("Lucene是一個開源的基於Java的搜索庫"); //查詢 TopDocs topDocs = indexSearcher.search(query, 10); System.out.println("返回的文檔個數:"+topDocs.totalHits); //獲取到文檔集合 ScoreDoc [] scoreDocs=topDocs.scoreDocs; for (ScoreDoc doc:scoreDocs) { //獲取到文檔 Document document = indexSearcher.doc(doc.doc); //獲取到文檔域中數據 System.out.println("fieldName:"+document.get("fieldName")); System.out.println("fieldPath:"+document.get("fieldPath")); System.out.println("fieldSize:"+document.get("fieldSize")); System.out.println("fieldContent:"+document.get("fieldContent")); } //關閉 indexReader.close(); }