我已经开始在过去几天使用Java中的Lucene 6.2.0并尝试解析包含标记的.trectext文件。我相信我使用正确的解析器来解析标签,但是我在创建一个新的Lucene文档时遇到了这个奇怪的错误,其中Cannot instantiate the type Document
。我恳请有人帮我这个,我没有看到任何关于这个的建议在互联网上也没有eclipse暗示任何更正。不知道我哪里错了。我已粘贴下面的代码。
`
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.StringField;
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.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.MultiFields;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.Terms;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.document.Field;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Paths;
import java.io.Reader;
import java.util.Iterator;
public class Indexing {
public static void main(String argv[]) {
String[] tags={"DOCNO","HEAD","BYLINE","DATELINE","TEXT"};
try {
String indexPath="C:\\Users\\sujit\\Desktop\\lucene_indexed"; //Path to create the Lucene Document Index.
File fXmlFile = new File("C:\\Users\\sujit\\Desktop\\sample.txt"); //Path to find the document to be indexed.
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
Analyzer analyzer=new StandardAnalyzer();
Directory dir = FSDirectory.open(Paths.get(indexPath));
IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
iwc.setOpenMode(OpenMode.CREATE);
IndexWriter writer=new IndexWriter(dir,iwc);
String DocNo="" ,Head="",ByLine="",DateLine="",Text="";
//System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("DOC");
//System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
//**Place where I see the error **
Document luceneDoc=new Document();
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
for(int tagNo=0;tagNo<tags.length;tagNo++){
for(int j=0;j<eElement.getElementsByTagName(tags[tagNo]).getLength();j++){
if(tags[tagNo]==tags[0])
DocNo+=eElement.getElementsByTagName(tags[tagNo]).item(j).getTextContent();
else if(tags[tagNo]==tags[1])
Head+=eElement.getElementsByTagName(tags[tagNo]).item(j).getTextContent();
else if(tags[tagNo]==tags[2])
ByLine+=eElement.getElementsByTagName(tags[tagNo]).item(j).getTextContent();
else if(tags[tagNo]==tags[3])
DateLine+=eElement.getElementsByTagName(tags[tagNo]).item(j).getTextContent();
else if(tags[tagNo]==tags[4])
Text+=eElement.getElementsByTagName(tags[tagNo]).item(j).getTextContent();
}
System.out.println(DocNo+Head+ByLine+DateLine+Text+"\n");
luceneDoc.add(new StringField("DOCNO",DocNo,Field.Store.YES));
luceneDoc.add(new StringField("HEAD",Head,Field.Store.YES));
luceneDoc.add(new StringField("BYLINE",ByLine,Field.Store.YES));
luceneDoc.add(new StringField("DATELINE",DateLine,Field.Store.YES));
luceneDoc.add(new StringField("TEXT",Text,Field.Store.YES));
writer.addDocument(luceneDoc);
DocNo="";Head="";ByLine="";DateLine="";Text="";
}
writer.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
您要导入org.w3c.dom.Document
,而不是org.apache.lucene.document.Document
。由于您似乎需要两者,您只需使用它的完整路径引用其中一条,而不是导入它。