package de.uni_tuebingen.ub.ixTheo.bibleRangeSearch;

import org.apache.lucene.search.FilteredQuery;
import org.apache.lucene.search.Query;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.QParser;
import org.apache.solr.search.SyntaxError;

public class BibleRangeParser extends QParser {
    private final static String QUERY_SEPARATOR = " ";
    private final static String DB_FIELD_SEPARATOR = ",";

    private Query innerQuery;

    public BibleRangeParser(final String searchString, final SolrParams localParams, final SolrParams params, final SolrQueryRequest request) {
        super(searchString, localParams, params, request);
        try {
            final String queryString = "+bible_ranges:" + getBookPrefixQueryString(searchString);
            final QParser parser = getParser(queryString, "lucene", getReq());
            final Range[] ranges = getRangesFromQuery();
            this.innerQuery = new FilteredQuery(new BibleRangeQuery(parser.parse(), ranges), new BibleRangeFilter(ranges));
        } catch (SyntaxError ex) {
            throw new RuntimeException("error parsing query", ex);
        }
    }

    /**
     * Tries to extract the book index of a search query.
     * Then creates a query string by concatenating the book index with '*'.
     * If no book index is found, only '*' will be returned.
     *
     * The first to digits of range number are the book index. 
     * See /var/lib/tuelib/books_of_the_bible_to_code.map
     *
     * @param queryString The search string from user
     * @return e.g. "12*" or "*"
     */
    private String getBookPrefixQueryString(final String queryString) {
        if (queryString == null || queryString.length() < 2) {
            return "*";
        }
        return queryString.substring(0, 2) + "*";
    }

    @Override
    public Query parse() throws SyntaxError {
        return this.innerQuery;
    }

    private Range[] getRangesFromQuery() {
        return BibleRange.getRanges(getFieldsFromQuery());
    }

    private String[] getFieldsFromQuery() {
        return qstr.split(QUERY_SEPARATOR);
    }

    public static Range[] getRangesFromDatabaseField(final String db_field) {
        return BibleRange.getRanges(db_field, DB_FIELD_SEPARATOR);
    }
}

@@ -1210,17 +1210,17 @@
    /
-*
+/
 *%0A    
- *
+//
  Tri
@@ -1267,26 +1267,26 @@
  query.%0A    
- *
+//
  Then create
@@ -1306,24 +1306,52 @@
 ing 
-by concatenating
+only matching bible references starting with
  the
@@ -1365,25 +1365,16 @@
 ndex
- with '*'
 .%0A    
- *
+//
  If 
@@ -1428,25 +1428,25 @@
 ed.%0A    
- *
+//
 %0A    
- *
+//
  The fir
@@ -1449,16 +1449,17 @@
  first t
+w
 o digits
@@ -1466,21 +1466,16 @@
  of 
+a 
 range 
-number 
 are 
@@ -1495,18 +1495,18 @@
 x. %0A    
- *
+//
  See /va
@@ -1557,17 +1557,17 @@
     
- *
+//
 %0A    
- *
+//
  @pa
@@ -1614,18 +1614,18 @@
 ser%0A    
- *
+//
  @return
@@ -1635,12 +1635,128 @@
 g. %22
-12*%22
+/.*12%5B0-9%5D%7B5%7D.*/%22 (NB. the SOLR query parser anchors regular expressions at the%0A    //         beginning and at the end)
  or 
@@ -1767,10 +1767,9 @@
     
- *
+/
 /%0A  
@@ -1935,30 +1935,31 @@
       %7D%0A
-        return
+%09return %22/.*%22 +
  querySt
@@ -1982,17 +1982,27 @@
 , 2) + %22
-*
+%5B0-9%5D%7B5%7D.*/
 %22;%0A    %7D

Fix for evaluating several bible ranges in one record, solrconfig now repects new package scheme