Я использую Lucene 3.3.0 с Java. Я столкнулся со следующей проблемой и не знаю, найдется ли решение для нее.
Я индексирую следующий текст: «Мальчик играл так усердно, чтобы выиграть игру», используя StandardAnalyzer, а затем я делаю поиск по запросу, используя «play» ... Lucene находит хиты ТОЛЬКО при использовании WildcardQuery Builder.
Проблема в том, что, когда я пытаюсь найти "boy game", он не находит хитов вообще.
Можно ли заставить Lucene сделать что-то вроде контекстного поиска, чтобы решить эту проблему?
Спасибо,
Samer
private static void addDoc(IndexWriter w, String value) throws IOException {
Document doc = new Document();
doc.add(new Field("title", value, Field.Store.YES, Field.Index.ANALYZED));
w.addDocument(doc);
}
@SuppressWarnings("deprecation")
public static void lucene(String args, String query) throws IOException, ParseException {
// 0. Specify the analyzer for tokenizing text.
// The same analyzer should be used for indexing and searching
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
// 1. create the index
Directory index = new RAMDirectory();
// the boolean arg in the IndexWriter ctor means to
// create a new index, overwriting any existing index
IndexWriter w = new IndexWriter(index, analyzer, true,
IndexWriter.MaxFieldLength.UNLIMITED);
String[] splitOnLinefeed = args.split("\n");
for (int i = 0; i < splitOnLinefeed.length; i++) {
addDoc(w, splitOnLinefeed[i]);
}
w.close();
// 2. query
String querystr = query+"*";
// the "title" arg specifies the default field to use
// when no field is explicitly specified in the query.
Query q = new QueryParser(Version.LUCENE_CURRENT, "title", analyzer)
.parse(querystr);
// 3. search
IndexSearcher searcher = new IndexSearcher(index, true);
ScoreDoc[] hits = searcher.search(q, 100).scoreDocs;
// 4. display results
System.out.println("Found " + hits.length + " hit(s).");
for (int i = 0; i < hits.length; ++i) {
int docId = hits[i].doc;
Document d = searcher.doc(docId);
System.out.println((i + 1) + ". " + d.get("title"));
}
// searcher can only be closed when there
// is no need to access the documents any more.
searcher.close();
}
public static void main(String[] args) throws Exception {
lucene(parse("Test.pdf"), "boy game");
}