0
votes

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.

1
Please i want to know if queryParser parse= queryParser("fie ld", query); can do all the type of lucene queries, or do i have to identify the type of query from the query string to call the appropraite query type method. I dont want to over design of enhance bugs in my code. If theres any neat way to combine all the query types it can help me so much. Am still a beginner with lucene searchChukwu Remijius

1 Answers

1
votes

If I am not wrong, it seems like you want to read query strings from a file and then execute them. And you want to be able to write all sorts of queries in the file. Correct?

It is possible. While coding the queries gives you flexibility, all queries can be written in text format with trial and error.

I would suggest looking at this. It is Lucene's documentation for Query Parser Syntax.

Also I would suggest to write all queries using code, and print the String representation of the final Query. This would give you insight as to how to write queries in String format.