你好我是lucene的新手我想编写一个演示程序来索引一组具有唯一字段的字段,然后能够搜索任何字段。我试过,但每当我打印任何未用于搜索查询的字段时,它返回一个空值。对于下面的示例,name字段返回null。
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.logging.Level;
import java.util.logging.Logger;
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.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
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.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;
/**
*
* @author user
*/
public class Indexer {
Analyzer analyzer;
Directory directory;
IndexWriterConfig config;
IndexWriter iwriter;
static Document doc;
final File file = new File("datasetindex");
static public void addDoc(IndexWriter w, String field,String value) throws IOException{
doc = new Document();
if("id".equals(field)){
doc.add(new Field(field,value, Field.Store.YES,Field.Index.NOT_ANALYZED));
}else{
doc.add(new Field(field,value, TextField.TYPE_STORED));
}
// w.addDocument(doc);
}
public static void main(String args[]){
try {
Analyzer analyzer = new StandardAnalyzer();
Directory directory = new RAMDirectory();
IndexWriterConfig config = new IndexWriterConfig(analyzer);
config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
IndexWriter iwriter = new IndexWriter(directory, config);
String id1 = "1";
String name1 = "WAN (Wireless Area network)";
String des1 = "large network, used for country or continent,make use of satelite and gave rise to broadband network";
String id2 = "2";
String name2 = "MAN (Metropolitan Area network)";
String des2 = "large networks usually used for cities and intranet networks";
String id3 = "3";
String name3 = "LAN (local area network)";
String des3 = "connection between computers over small land mass";
addDoc(iwriter, "id", id1);
addDoc(iwriter,"name",name1);
addDoc(iwriter,"description",des1);
iwriter.addDocument(doc);
addDoc(iwriter,"id",id2);
addDoc(iwriter,"name",name2);
addDoc(iwriter,"description",des2);
iwriter.addDocument(doc);
addDoc(iwriter, "id", id3);
addDoc(iwriter, "name", name3);
addDoc(iwriter,"description",des3);
iwriter.addDocument(doc);
/*
addDoc(iwriter,"name",name3);
addDoc(iwriter,"description",des1);
addDoc(iwriter,"description",des2);
addDoc(iwriter,"description",des3);
*/
iwriter.close();
//searching started
DirectoryReader ireader = DirectoryReader.open(directory);
IndexSearcher isearcher = new IndexSearcher(ireader);
//parse a search query
TermQuery tq = new TermQuery(new Term("description", "networks"));
//QueryParser parser = new QueryParser("description", analyzer);
//Query query = parser.parse("country");
ScoreDoc hits[] = isearcher.search(tq,1000).scoreDocs;
System.out.println("Records found: "+hits.length);
for(int a=0; a < hits.length; a++){
Document HitDoc = isearcher.doc(hits[a].doc);
System.out.println("A DOC: "+HitDoc.get("name"));
System.out.println("DOC ID: "+hits[a].doc);
}
ireader.close();
directory.close();
} catch (IOException ex) {
Logger.getLogger(Indexer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
答案 0 :(得分:1)
基本上在您编写新文档的每个字段的代码中,在之前的代码中,只有描述字段才会添加到doc。我已更新您的代码以修复它: -
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
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.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
/**
*
* @author user
*/
public class Indexer {
Analyzer analyzer;
Directory directory;
IndexWriterConfig config;
IndexWriter iwriter;
final File file = new File("datasetindex");
static public void addDoc( String field,String value, Document doc) throws IOException{
if("id".equals(field)){
doc.add(new Field(field,value, Field.Store.YES,Field.Index.NOT_ANALYZED));
}else{
doc.add(new Field(field, value, TextField.TYPE_STORED));
}
// w.addDocument(doc);
}
public static void main(String args[]){
try {
Analyzer analyzer = new StandardAnalyzer();
Directory directory = new RAMDirectory();
IndexWriterConfig config = new IndexWriterConfig(analyzer);
config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
IndexWriter iwriter = new IndexWriter(directory, config);
String id1 = "1";
String name1 = "WAN (Wireless Area network)";
String des1 = "large network, used for country or continent,make use of satelite and gave rise to broadband network";
String id2 = "2";
String name2 = "MAN (Metropolitan Area network)";
String des2 = "large networks usually used for cities and intranet networks";
String id3 = "3";
String name3 = "LAN (local area network)";
String des3 = "connection between computers over small land mass";
Document doc1=new Document();
addDoc( "id", id1,doc1);
addDoc("name",name1,doc1);
addDoc("description",des1,doc1);
iwriter.addDocument(doc1);
Document doc2=new Document();
addDoc("id",id2,doc2);
addDoc("name",name2,doc2);
addDoc("description",des2,doc2);
iwriter.addDocument(doc2);
Document doc3=new Document();
addDoc( "id", id3,doc3);
addDoc( "name", name3,doc3);
addDoc("description",des3,doc3);
iwriter.addDocument(doc3);
/*
addDoc(iwriter,"name",name3);
addDoc(iwriter,"description",des1);
addDoc(iwriter,"description",des2);
addDoc(iwriter,"description",des3);
*/
iwriter.close();
//searching started
DirectoryReader ireader = DirectoryReader.open(directory);
IndexSearcher isearcher = new IndexSearcher(ireader);
//parse a search query
TermQuery tq = new TermQuery(new Term("description", "networks"));
//QueryParser parser = new QueryParser("description", analyzer);
//Query query = parser.parse("country");
ScoreDoc hits[] = isearcher.search(tq,1000).scoreDocs;
System.out.println("Records found: "+hits.length);
for(int a=0; a < hits.length; a++){
Document HitDoc = isearcher.doc(hits[a].doc);
System.out.println("A DOC: "+HitDoc.get("name"));
System.out.println("DOC ID: "+hits[a].doc);
}
ireader.close();
directory.close();
} catch (IOException ex) {
Logger.getLogger(Indexer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}