Web サイトのデータベースのクエリに Lucene を使用していますが、いくつかの問題が発生しています。問題が索引付けによるものなのか、検索によるものなのか (より正確にはクエリの作成によるもの) は実際にはわかりません。私の知る限り、複数の SQL データベース テーブルを検索する場合は、テーブルごとに複数のドキュメントを使用することをお勧めします (次のチュートリアルに従いました。
http://kalanir.blogspot.pt/2008/06/indexing-database-using-apache-lucene.html
http://www.lucenetutorial.com/techniques/indexing-databases.html
)私がやりたいことに近いです。実際、私の場合、それぞれが上記のレベルを指定しているため、すべて関連する 3 つのテーブルを検索する必要があります (例: 製品 -> タイプ -> 色)。したがって、私の索引付けは次のようなものでした:
String sql = "select c.idConteudo as ID, c.designacao as DESIGNACAO, cd.texto as DESCRICAO, ctf.webTag as TAG from Conteudo c, ConteudoDetalhe cd, ConteudoTipoFormato ctf where c.idConteudo = cd.idConteudo AND cd.idConteudoTipoFormato = ctf.idConteudoTipoFormato;";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql);
Document document;
while (rs.next())
{
String S = new String();
S += IndexerCounter;
document = new Document();
document.add(new Field("ID_ID",S, Field.Store.YES, Field.Index.NO));
document.add(new Field("ID CONTEUDO", rs.getString("ID"), Field.Store.YES, Field.Index.NO));
document.add(new Field("DESIGNACAO", rs.getString("DESIGNACAO"), Field.Store.NO, Field.Index.TOKENIZED));
document.add(new Field("DESCRICAO", rs.getString("DESCRICAO"), Field.Store.NO, Field.Index.TOKENIZED));
document.add(new Field("TAG", rs.getString("TAG"), Field.Store.NO, Field.Index.TOKENIZED));
try{
writer.addDocument(document);
}catch(CorruptIndexException e){
}catch(IOException e){
}catch(Exception e){ } //just for knowing if something is wrong
IndexerCounter++;
}
結果を出力すると、次のようになります。
ID: idConteudo: designacao: texto: webTag
1:1:Xor:xor 1 Descricao:x or
2:1:Xor:xor 2 Descricao:xis Or
3:1:Xor:xor 3 Descricao:exor
4:2:And:and 1 Descricao:and
5:2:And:and 2 Descricao:&
6:2:And:and 3 Descricao:ande
7:2:And:and 4 Descricao:a n d
8:2:And:and 5 Descricao:and,
9:3:Nor:nor 1 Descricao:nor
10:3:Nor:nor 2 Descricao:not or
私が本当に欲しいのは、(Xor などの) クエリを作成し、作成されたドキュメントでそれを検索することです。したがって、私の検索方法は次のようなものです。
コンストラクタ:
public Spider(String Query, String Pathh) {
String[] Q;
QueryFromUser = new String();
QueryFromUser = Query;
QueryToSearch1 = new String();
QueryToSearch2 = new String();
Path = Pathh;
try {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
return;
}
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "");
} catch (SQLException e) {
e.printStackTrace();
return;
}
Q = Query.split(" ");
//NOTE: the AND word enables the search engine to search by the various words in a query
for (int i = 0; i < Q.length; i++) {
if ((Q.length - i) > 1) //prevents the last one to take a AND
{
QueryToSearch1 += Q[i] + " AND ";
} else {
QueryToSearch1 += Q[i];
}
}
for (int i = 0; i < Q.length; i++) {
QueryToSearch2 += "+" + Q[i];
}
try {
SEARCHING_CONTENT();
} catch (ClassNotFoundException ex) {
Logger.getLogger(Spider.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(Spider.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Spider.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Spider.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParseException ex) {
Logger.getLogger(Spider.class.getName()).log(Level.SEVERE, null, ex);
}
SEARCHING_WEB(); //not for using now
} catch (CorruptIndexException ex) {
Logger.getLogger(Spider.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Spider.class.getName()).log(Level.SEVERE, null, ex);
}
アイデアは、QueryToSearch1 と QueryToSearch2 にコマンド (オンライン チュートリアルで見た、場所をよく覚えていない) AND と + があるということです。したがって、ユーザからの「not or」というクエリに対しては、2 つの単語を同時に検索する「not AND or」と、2 つの単語を別々に検索する「+not+or」が検索対象となります。これは私の疑問の 1 つです。lucene クエリの構築がこのようなものであるかどうかはよくわかりません。実際のところ、メソッド Querying では次のことが行われます。
private void SEARCHING_CONTENT() throws CorruptIndexException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, ParseException {
Querying(QueryToSearch1); // search for the whole phrase
Querying(QueryToSearch2); //search by individual words
//Querying(QueryFromUser); //search by individual words
}
private void Querying(String QueryS) throws CorruptIndexException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, ParseException {
searcher = new IndexSearcher(IndexReader.open(Path + "/INDEX_CONTENTS"));
query = new QueryParser("TAG", new StopWords()).parse(QueryS);
query.toString();
hits = searcher.search(query);
pstmt = connection.prepareStatement(sql);
for (int i = 0; i < hits.length(); i++) {
id = hits.doc(i).get("TAG");
pstmt.setString(1, id);
displayResults(pstmt);
}
}
クエリのドキュメントにヒットはありません。次の行で次のように言うことが重要です。
query = new QueryParser("TAG", new StopWords()).parse(QueryS);
これStopWords
は、StandardAnalyser を拡張するように作成したクラスですが、指定した単語を含む新しいクラスです (または、およびなどの検索単語で重要なものを削除しないためです。この場合、これらの単語は重要である可能性があります)。
問題は、私が言ったようにです。検索してもヒットしません。これが索引付けによるものなのか、検索するクエリの構成によるものなのかはわかりません (クエリの構成が不適切な場合、ヒットはありません)。
私は誰からの助けにも感謝します。必要に応じて、より多くの情報を喜んで提供します。
どうもありがとう。