apacheelasticsearchsolrlucenequery-parser

Syntax Error, cannot parse from lucene StandardQueryparser when using fq(filtered query)


I am trying to pass a java String to Apache StandardQueryparser to get the Querynode.

Input - "fq=section:1"

All I need is section:1 in FILTER clause in QueryNode. This looks pretty straightforward but it throws

INVALID_SYNTAX_CANNOT_PARSE: Syntax Error, cannot parse fq=section:1:


Solution

  • Use a ConstantScoreQuery. It won't affect it score, and is the same was as the fq parameter is implemented in Solr:

      public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
        // SolrRequestInfo reqInfo = SolrRequestInfo.getRequestInfo();
    
        if (!(searcher instanceof SolrIndexSearcher)) {
          // delete-by-query won't have SolrIndexSearcher
          return new BoostQuery(new ConstantScoreQuery(q), 0).createWeight(searcher, scoreMode, 1f);
        }
    
        SolrIndexSearcher solrSearcher = (SolrIndexSearcher)searcher;
        DocSet docs = solrSearcher.getDocSet(q);
        // reqInfo.addCloseHook(docs);  // needed for off-heap refcounting
    
        return new BoostQuery(new SolrConstantScoreQuery(docs.getTopFilter()), 0).createWeight(searcher, scoreMode, 1f);
      }