I am using Lucene 3.6.2 on Android. The code used and the observations made are as below.
Indexing Code:
public void indexBookContent(Book book, File externalFilesDir) throws Exception {
IndexWriter indexWriter = null;
NIOFSDirectory directory = null;
directory = new NIOFSDirectory(new File(externalFilesDir.getPath() + "/IndexFile", book.getBookId()));
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(LUCENE_36, new StandardAnalyzer(LUCENE_36));
indexWriter = new IndexWriter(directory, indexWriterConfig);
Document document = createFieldsForContent();
String pageContent = Html.fromHtml(decryptedPage).toString();
((Field) document.getFieldable("content")).setValue(pageContent);
((Field) document.getFieldable("content")).setValue(pageContent);
((Field) document.getFieldable("content")).setValue(pageContent.toLowerCase());
}
private Document createFieldsForContent() {
Document document = new Document();
Field contentFieldLower = new Field("content", "", YES, NOT_ANALYZED);
document.add(contentFieldLower);
Field contentField = new Field("content", "", YES, ANALYZED);
document.add(contentField);
Field contentFieldNotAnalysed = new Field("content", "", YES, NOT_ANALYZED);
document.add(contentFieldNotAnalysed);
Field recordIdField = new Field("recordId", "", YES, ANALYZED);
document.add(recordIdField);
return document;
}
public JSONArray searchBook(String bookId, String searchText, File externalFieldsDir, String filter) throws Exception {
List<SearchResultData> searchResults = null;
NIOFSDirectory directory = null;
IndexReader indexReader = null;
IndexSearcher indexSearcher = null;
directory = new NIOFSDirectory(new File(externalFieldsDir.getPath() + "/IndexFile", bookId));
indexReader = IndexReader.open(directory);
indexSearcher = new IndexSearcher(indexReader);
Query finalQuery = constructSearchQuery(searchText, filter);
TopScoreDocCollector collector = TopScoreDocCollector.create(100, false);
indexSearcher.search(finalQuery, collector);
ScoreDoc[] scoreDocs = collector.topDocs().scoreDocs;
}
private Query constructSearchQuery(String searchText, String filter) throws ParseException {
QueryParser contentQueryParser = new QueryParser(LUCENE_36, "content", new StandardAnalyzer(LUCENE_36));
contentQueryParser.setAllowLeadingWildcard(true);
contentQueryParser.setLowercaseExpandedTerms(false);
String wildCardSearchText = "*" + QueryParser.escape(searchText) + "*";
// Query Parser used.
Query contentQuery = contentQueryParser.parse(wildCardSearchText);
return contentQueryParser.parse(wildCardSearchText);
}
I have gone through this: "Lucene: Multi-word phrases as search terms", and my logic didn't seem to different.
My doubt is that the fields are getting overwritten. Also, I need Chinese language support which works with this code except the problem of two or more word support.