Java 用于组合所有lucene查询类型的自定义代码

Java 用于组合所有lucene查询类型的自定义代码,java,lucene,Java,Lucene,我正在构建一个可以生成自定义查询以搜索lucene索引的项目。我做了一些研究,发现了许多查询lucene索引的方法,如: 布尔搜索:字段:白布和字段鞋 术语查询搜索:字段:我是一个男孩 近距离查询搜索:5/5/2016-10/6/2017 我感到困惑的是:QueryParser可以执行所有这些查询吗?还是必须编写一个方法来执行单独的查询,就像在Tutorialspoint查询编程中一样,他们给出了每种查询的示例以及如何实现它 以下是我目前的代码: /** Setting the searcher

我正在构建一个可以生成自定义查询以搜索lucene索引的项目。我做了一些研究,发现了许多查询lucene索引的方法,如:

布尔搜索:字段:白布和字段鞋 术语查询搜索:字段:我是一个男孩 近距离查询搜索:5/5/2016-10/6/2017 我感到困惑的是:QueryParser可以执行所有这些查询吗?还是必须编写一个方法来执行单独的查询,就像在Tutorialspoint查询编程中一样,他们给出了每种查询的示例以及如何实现它

以下是我目前的代码:

/** 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();
  }
以下是使用不同查询类型的教程要点示例:

    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();
我希望能够组合所有lucene查询类型,以便可以像本教程中所述那样查询索引:


我的项目从xml文件创建动态字段,并使用它来存储索引,因此我知道这些字段,并且我还希望能够获得命中的字段。

如果我没有错,您似乎希望从文件中读取查询字符串,然后执行它们。您希望能够在文件中写入各种查询。对吗

这是可能的。虽然对查询进行编码给您带来了灵活性,但所有查询都可以用文本格式编写,只需反复尝试即可

我建议你看看。这是Lucene关于查询解析器语法的文档


我还建议使用代码编写所有查询,并打印最终查询的字符串表示形式。这将使您了解如何以字符串格式编写查询。

请告诉我queryParser parse=queryParserfie ld,query;可以执行所有类型的lucene查询,或者我必须从查询字符串中标识查询类型才能调用appropraite查询类型方法。我不想过度设计代码中的bug。如果有任何巧妙的方法来组合所有的查询类型,它可以帮助我这么多。我还是lucene Search的初学者,我读过那个文档。我知道查询文本术语。我甚至用那部纪录片来学习如何像专业人士一样搜索谷歌。我想知道哪种查询类型可以执行文档中的所有查询。他们没有编写关于如何实现所编写的查询语法类型的代码。我想知道查询解析器是否可以实现所有这些语法,或者我是否必须为每个查询类型编写单独的方法,并使用查询语法来知道如果我的应用程序生成一个查询语法,比如:google:were to eat in negeria ^4和ebay:buy a spoon,我应该调用哪一个。这是一个布尔查询类型,对吗?我的查询类型向导现在可以调用一个实现布尔查询类型的方法来查询索引:booleanQueryquerysyntax;类似的事情我想知道是否有一种方法可以简化应用程序代码,以lucene语法生成查询。然后将其传递给查询解析器。查询解析器将接受所有字符串。您添加的代码完全正确。您不需要使用特定于查询的方法。