锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.FilterIndexReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.HitCollector;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.TopDocCollector;
/** Simple command-line based search demo. */
public class SearchFiles {
/** Use the norms from one field for all fields. Norms are read into memory,
* using a byte of memory per document per searched field. This can cause
* search of large collections with a large number of fields to run out of
* memory. If all of the fields contain only a single token, then the norms
* are all identical, then single norm vector may be shared. */
private static class OneNormsReader extends FilterIndexReader {
private String field;
public OneNormsReader(IndexReader in, String field) {
super(in);
this.field = field;
}
public byte[] norms(String field) throws IOException {
return in.norms(this.field);
}
}
private SearchFiles() {}
/** Simple command-line based search demo. */
public static void main(String[] args) throws Exception {
String index = "index";
String field = "content";
boolean multipleFields = true;
IndexReader reader = IndexReader.open(index);//IndexReader 鏍規嵁 index 鎸囧畾鐨勮礬寰?璁塊棶绱㈠紩錛屾壂鎻忕儲寮曘?br />
Searcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer();
BufferedReader in =new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
while (true) {
System.out.println("Enter query: ");
String line = in.readLine();
if (line == null || line.length() == -1)
break;
line = line.trim();
if (line.length() == 0)
break;
if (!multipleFields) {
QueryParser parser = new QueryParser(field, analyzer);
Query query = parser.parse(field);// 鏍規嵁鎸囧畾鐨勫崟涓猣ield鏌ヨ
parser.setDefaultOperator(parser.OR_OPERATOR.OR);
//澶氫釜瀛楃涓蹭互絀烘牸浠芥牸鏃訛紝OR : a b 鍚湁a鎴朾鍧囧彲銆?br />
//AND a b 蹇呴』鍚湁 a鍜宐銆?br />
doPagingSearch(searcher, query);
} else {
String[] fields = new String[2];
fields[0] = "contents";
fields[1] = "name";
BooleanClause.Occur[] flags = new BooleanClause.Occur[] {
BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD };
//鏍規嵁澶氫釜field鏌ヨ鏃躲俿hould,should鏌ヨ瀛楁鍦?name鎴栨槸contents浠諱綍涓涓腑錛屽潎鍋氫負涓鏉¤褰曡繑鍥炪?br />
//must,must .蹇呴』 鍗沖湪 name 涓紝鍙堝湪contents 涓?br />
Query query = MultiFieldQueryParser.parse(line, fields, flags,
analyzer);
doPagingSearch(searcher, query);
}
}
reader.close();
}
/**
* This method uses a custom HitCollector implementation which simply prints out
* the docId and score of every matching document.
*
* This simulates the streaming search use case, where all hits are supposed to
* be processed, regardless of their relevance.
*/
public static void doPagingSearch( Searcher searcher, Query query) throws IOException {
// Collect enough docs to show 5 pages
TopDocCollector collector = new TopDocCollector(20);//鏈澶氱粨鏋滈泦涓暟銆?br />
searcher.search(query, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
int numTotalHits = collector.getTotalHits();//鎼滅儲鍒扮殑絎﹀悎鏉′歡鐨勮褰曟繪潯鏁般?br />
System.out.println(numTotalHits + " total matching documents");
for(int i=0;i<numTotalHits;i++){
Document doc = searcher.doc(hits[i].doc);
System.out.println("path.."+doc.get("path"));
System.out.println("modified.."+doc.get("modified"));
System.out.println("name.."+doc.get("name"));
System.out.println("parent"+doc.get("parent"));
System.out.println("content..."+doc.get("content"));
}
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
/** Index all text files under a directory. */
public class IndexFiles {
private IndexFiles() {}
static final File INDEX_DIR = new File("index");//绱㈠紩姝㈠綍銆傚緩鍦ㄥ綋鍓嶇洰褰曠殑/index涓?br />
/** Index all text files under a directory. */
public static void main(String[] args) {//args[0] 鏂囦歡璺緞. main 鏂規硶錛氬args[0]鎸囧畾鐨勬枃浠惰礬寰勪笅鐨勬墍鏈夋枃浠跺緩绔嬬儲寮曘?br />
final File docDir = new File(args[0]);
if (!docDir.exists() || !docDir.canRead()) {
System.out .println("Document directory '" + docDir.getAbsolutePath() + "' does not exist or is not readable, please check the path");
System.exit(1);
}
Date start = new Date();
try {
IndexWriter writer = new IndexWriter(INDEX_DIR, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
//IndexWriter璐熻矗鍒涘緩鍜岀淮鎶ょ儲寮?br />
//IndexWriter(String path, Analyzer a, boolean create, IndexWriter.MaxFieldLength mfl)
//path:璺緞 Analyzer:鏂囨湰鍒嗘瀽鍣?nbsp; create:鏄惁鍒涘緩鏂扮儲寮?nbsp; mfl 鏈澶ield鏁伴噺
System.out.println("Indexing to directory '" + INDEX_DIR + "'...");
indexDocs(writer, docDir);
System.out.println("Optimizing...");
writer.optimize();//浼樺寲绱㈠紩
writer.close();//鍏抽棴
Date end = new Date();
System.out.println(end.getTime() - start.getTime() + " total milliseconds");
} catch (IOException e) {
System.out.println(" caught a " + e.getClass()
+ "\n with message: " + e.getMessage());
}
}
static void indexDocs(IndexWriter writer, File file) throws IOException {
// do not try to index files that cannot be read
if (file.canRead()) {
if (file.isDirectory()) {
String[] files = file.list();
// an IO error could occur
if (files != null) {
for (int i = 0; i < files.length; i++) {
indexDocs(writer, new File(file, files[i]));
}
}
} else {
System.out.println("adding " + file);
try {
writer.addDocument(FileDocument.Document(file));
}
// at least on windows, some temporary files raise this
// exception with an "access denied" message
// checking if the file can be read doesn't help
catch (FileNotFoundException fnfe) {
;
}
}
}
}
}
package org.apache.lucene.demo;
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.File;
import java.io.FileReader;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
/** A utility for making Lucene Documents from a File. */
public class FileDocument {
public static Document Document(File f)
throws java.io.FileNotFoundException {
// make a new, empty document
Document doc = new Document();
doc.add(new Field("contents", new FileReader(f)));
doc.add(new Field("path", f.getPath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("modified", DateTools.timeToString(f.lastModified(), DateTools.Resolution.MINUTE),Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("name",f.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED));
//Field
// return the document
return doc;
}
private FileDocument() {}
}