How to Search for Literal Asterisks in a String Field with Solr

27 views Asked by At

I'm working with Apache Solr and facing an issue where I need to perform a search query to find documents containing specific strings with literal asterisks. My goal is to match documents where the field numardosar exactly contains the string 3153/115/2018***, including the three asterisks at the end. For example I am searching for numardosar:"3153/115/2018***" and I get numardosar:"3153/115/2018*" both in my Spring app and Solr admin UI. I've tried escaping the asterisks using backslashes like so:

numardosar:"3153/115/2018\*\*\*"

or a regex filter query in Solr's admin UI

numardosar:/3153\/115\/2018\*\*\*/

I am testing my queries from Solr admin UI but also implemented them as java requests with

org.apache.solr.client.solrj.SolrQuery

I thought about sending my requests directly to Solr as HttpRequests but I am pretty sure that the issue won't be fixed. This is the method that does the query more exactly:

 public JSONObject qrry(String querystr,String start,String offset,String order){
        SolrQuery query;
        query = new SolrQuery(querystr);
        query.setStart(Integer.valueOf(start));
        query.setRows(Integer.valueOf(offset));
//        query.setParam("group", true); // I have tried grouping but that didn't work
//        query.setParam("group.field", "numardosar"); // Group results based on the numardosar field
//        query.setParam("group.limit", "1");
        try {
            System.out.println(querystr);
            QueryResponse list= solrClient.query(query);
            if (list.getResults().getNumFound() != 0) {
                return listToJson(list.getResults(), list.getResults().getNumFound());
            }
            return null;
        } catch (SolrServerException | IOException e) {
            System.out.println(e.getMessage());
            throw new RuntimeException("Eroare401 Eroare solr");
        }
    }

And in the listToJSON I am removing duplicates from the response

Attempts to Solve:

Escaping Asterisks: I've tried escaping the asterisks in my query as follows, but it didn't yield the correct results:

numardosar:"3153/115/2018\*\*\*"

or

numardosar:"3153/115/2018\\*\\*\\*"

Regex Filter Query: Attempting a regex filter query in the Solr Admin UI also didn't work as expected:

numardosar:/3153\/115\/2018\*\*\*/

SolrJ Implementation: I'm implementing the queries in Java using:

org.apache.solr.client.solrj.SolrQuery

Below is the method I use for querying:

public JSONObject query(String querystr, String start, String offset, String order) {
    SolrQuery query = new SolrQuery(querystr);
    query.setStart(Integer.parseInt(start));
    query.setRows(Integer.parseInt(offset));
    try {
        System.out.println(querystr);
        QueryResponse list = solrClient.query(query);
        if (list.getResults().getNumFound() != 0) {
            return listToJson(list.getResults(), list.getResults().getNumFound());
        }
        return null;
    } catch (SolrServerException | IOException e) {
        System.out.println(e.getMessage());
        throw new RuntimeException("Error 401: Solr error");
    }
}

I don't have admin access to solr schema and I prefer to not modify it to change the

<tokenizer class="solr.WhiteSpaceTokenizerFactory"/> 
0

There are 0 answers