Lucene 및 Tika Apache 라이브러리 패키지를 구현하여 원하는대로 잘 작동하도록 관리했습니다. 하지만 나는 말로 악센트에 문제가 있으며 악센트가있는 단어에 대해서는 결과를 반환 할 수 없습니다.Lucene + Tika에 액센트가있는 단어에 대한 결과를 반환 할 수 없습니다.
Indexador.java
package br.com.ir;
import org.apache.log4j.Logger;
import org.apache.lucene.analysis.br.BrazilianAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import org.apache.tika.Tika;
import javax.swing.*;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.text.SimpleDateFormat;
class Indexador {
private static final Logger logger = Logger.getLogger(Indexador.class);
private IndexWriter writer;
private Tika tika;
private int qntArq = 0;
public void iniciaIndexacao() {
try {
File diretorio = new File(ArquivoDeConfiguracao.retornaValorIndice());
apagaIndices(diretorio);
Directory d = new SimpleFSDirectory(diretorio);
BrazilianAnalyzer analyzer = new BrazilianAnalyzer(Version.LUCENE_4_9);
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_9,
analyzer);
writer = new IndexWriter(d, config);
long inicio = System.currentTimeMillis();
indexaArquivosDoDiretorio(new File(ArquivoDeConfiguracao.retornaValorFonte()));
// Fecha o IndexWriter e comita as mudanças
writer.commit();
long fim = System.currentTimeMillis();
JOptionPane.showMessageDialog(
null,
"Quantidade de arquivos indexados: " + qntArq + "\n"
+ "Tempo para indexar: "
+ String.valueOf((fim - inicio)/1000) + "s"
);
} catch (IOException e) {
logger.error(e);
} finally {
if (writer != null) try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void apagaIndices(File diretorio) {
if (diretorio.exists()) {
File arquivos[] = diretorio.listFiles();
if (arquivos != null) {
for (File arquivo : arquivos) {
arquivo.delete();
}
}
}
}
void indexaArquivosDoDiretorio(File raiz) {
FilenameFilter filtro = new FilenameFilter() {
public boolean accept(File arquivo, String nome) {
return nome.toLowerCase().endsWith(".pdf")
|| nome.toLowerCase().endsWith(".odt")
|| nome.toLowerCase().endsWith(".doc")
|| nome.toLowerCase().endsWith(".docx")
|| nome.toLowerCase().endsWith(".ppt")
|| nome.toLowerCase().endsWith(".pptx")
|| nome.toLowerCase().endsWith(".xls")
|| nome.toLowerCase().endsWith(".xlsx")
|| nome.toLowerCase().endsWith(".txt")
|| nome.toLowerCase().endsWith(".rtf")
|| nome.toLowerCase().endsWith("");
}
};
for (File arquivo : raiz.listFiles(filtro)) {
if (arquivo.isFile()) {
try {
String textoExtraido = getTika().parseToString(arquivo);
indexaArquivo(arquivo, textoExtraido);
qntArq++;
} catch (Exception e) {
logger.error(e);
}
} else {
indexaArquivosDoDiretorio(arquivo);
}
}
}
private void indexaArquivo(File arquivo, String textoExtraido) {
SimpleDateFormat formatador = new SimpleDateFormat("ddMMyyyy");
String ultimaModificacao = formatador.format(arquivo.lastModified());
Document documento = new Document();
documento.add(new TextField("UltimaModificacao", ultimaModificacao,
Field.Store.YES));
documento.add(new TextField("Caminho", arquivo.getAbsolutePath(),
Field.Store.YES));
documento.add(new TextField("Texto", textoExtraido, Field.Store.YES));
try {
getWriter().addDocument(documento);
} catch (IOException e) {
logger.error(e);
}
}
Tika getTika() {
if (tika == null) {
tika = new Tika();
}
return tika;
}
IndexWriter getWriter() {
return writer;
}
}
Buscador.java
package br.com.ir;
import org.apache.log4j.Logger;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
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.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import javax.swing.*;
import java.io.File;
class Buscador {
private final static Logger logger = Logger.getLogger(Buscador.class);
public int totalDeOcorrencias;
public String quantBusca;
@SuppressWarnings({"unchecked", "rawtypes"})
public Buscador(String parametro, DefaultListModel listModel) {
try {
Directory diretorio = new SimpleFSDirectory(new File(
ArquivoDeConfiguracao.retornaValorIndice()));
// IndexReader: classe abstrata responsável por acessar
// o índice;
IndexReader leitor = DirectoryReader.open(diretorio);
// IndexSearcher: implementa os métodos necessários para
// realizar buscas em um índice;
IndexSearcher buscador = new IndexSearcher(leitor);
Analyzer analisador = new StandardAnalyzer(Version.LUCENE_4_9);
// QueryParser/Query: representa a consulta do usuário. Outros
// exemplos de query podem ser vistos no Javadoc;
QueryParser parser = new QueryParser(Version.LUCENE_4_9, "Texto",
analisador);
Query consulta = parser.parse(parametro);
long inicio = System.currentTimeMillis();
// Realiza a busca e armazena o resultado em um TopDocs;
TopDocs resultado = buscador.search(consulta, 100);
long fim = System.currentTimeMillis();
totalDeOcorrencias = resultado.totalHits;
quantBusca = String.valueOf((fim - inicio)/1000);
// ScoreDoc: representa cada um dos documentos retornados na busca.
for (ScoreDoc sd : resultado.scoreDocs) {
Document documento = buscador.doc(sd.doc);
// Preenche o modelo
listModel.addElement(documento.get("Caminho"));
}
leitor.close();
} catch (Exception e) {
logger.error(e);
}
}
}
Indexador.java은 파일 내의 파일과 인덱스를위한 디렉토리 내에서 검색하는 클래스입니다. 그런 다음 Buscador.java를 사용하여 검색을 수행하지만 "plástico"ou "coração"와 같은 단어를 검색하려고하면 (이 단어가있는 파일이 있다고 가정하면) 아무 것도 반환하지 않습니다.
제공된 코드를 어떻게 검색하는지 알 수 없습니다. 예상 결과를 얻지 못하는 쿼리의 예뿐만 아니라이를 추가하십시오. – femtoRgon
질문 업데이트 – Strokes