I am building a project that can generate custom queries for searching a lucene index. I did some research and found out many ways to query a lucene index like:
- boolean Search: field: white cloths AND field shoes
- term Query Search: field: i am a boy
- proximity Query search: 5/5/2016-10/6/2017
What am confused about is: Can QueryParser do all this querying or do you have to write a method to do individual queries like in Tutorialspoint query programming, where they gave examples of each kind of query and how to implement it.
Here is my code so far:
/** Setting the searcher method to search the index passed **/
private void indexSearch(String indexDir, String queryX, int repeat, int hitsPerPage, String field, boolean raw) throws Exception{
/*** starting the method process ***/
// Here we process the searcher data
reader = DirectoryReader.open(FSDirectory.open(Paths.get(indexDir)));
searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer();
// BufferedReader
BufferedReader in = null;
boolean checkQ=false;
// Lets check if query is a file
File cfile=new File(queryX);
// Now lets check
if(cfile.isFile()){
// We process queryX as a file
in = Files.newBufferedReader(Paths.get(queryX), StandardCharsets.UTF_8);
checkQ=true;
}
else{
checkQ=false;
}
/** We pass query in different query types **/
parser = new QueryParser(field, analyzer);
// Here we are going to select the data we use for line
String line = checkQ != true ? queryX : in.readLine();
// Now lets trim the line
line = line.trim();
/******* NOW LETS CALL FUNCTION TO PASS QUERY ******/
search(line);
}
/** Making complex query priviledge to get data **/
public TopDocs search( String searchQuery) throws Exception{
// Lets pass query
query = parser.parse(searchQuery);
// Now lets return
return searcher.search(query, 100);
}
public TopDocs search(Query query) throws IOException{
return searcher.search(query, 100);
}
/**** Making the getDocument for the search ****/
public Document getDocument(ScoreDoc scoreDoc) throws CorruptIndexException, IOException{
return searcher.doc(scoreDoc.doc);
}
/** Simple command-line based search demo. */
public void close() throws Exception {
// Lets close reader
reader.close();
}
Here is the Tutorials point example in using a different query type:
private void searchUsingTermQuery(String searchQuery)throws IOException, ParseException{
searcher = new Searcher(indexDir); long startTime = System.currentTimeMillis(); //create a term to search file name
Term term = new Term(LuceneConstants.FILE_NAME, searchQuery);
//create the term query object
Query query = new TermQuery(term);
//do the search
TopDocs hits = searcher.search(query);
long endTime = System.currentTimeMillis();
System.out.println(hits.totalHits + " documents found. Time :" + (endTime - startTime) + "ms");
for(ScoreDoc scoreDoc : hits.scoreDocs) {
Document doc = searcher.getDocument(scoreDoc);
System.out.println("File: "+ doc.get(LuceneConstants.FILE_PATH));
}
searcher.close();
I want to be able to combine all the lucene Query Types so I can query my indexes like described in this tutorial: http://www.javaranch.com/journal/2004/04/Lucene.html
My project creates dynamic fields from xml files and uses it to store indexes, so I know the fields, and I also want to be able to get the fields that got hits.