2

We store a path in our schema, slash-delimited, and it also starts with a slash. According to this post solr interprets slashes in the beginning of queries as regex expressions after version 4.0, which means that we need to escape the slash.

But SolrTemplate.queryForPage(Query, Class) does not escape the slash, so the natural solution is to use QueryParser.escape(searchTerm) suggested in the link above.

However, this will add a backslashe to escape the slash, and that backslash will be escaped by SolrTemplate, resulting in an escaped backslash in the query - which gives no results


I'll add a couple of examples for clarity:

Query without any escaping:

q=paths:/myrootpath&start=0&rows=10

Query with manual escaping of the slash(QueryParser.escape(String)):

q=paths:\\/myrootpath&start=0&rows=10

The query I need:

q=paths:\/myrootpath&start=0&rows=10


I'm not sure if this is a bug or intended, since as far as I know, pre-4.0-solr didn't need to escape slashes

So is there way in spring-data-solr to either disable character escaping for a query, or modify which characters that are escaped?

Community
  • 1
  • 1
Einar Bjerve
  • 524
  • 2
  • 12
  • @tlavarea I'm sorry, but I did not find a solution, but it might be possible in later versions, it's been almost a year since I worked on that proejct. Anyway, I figured it must be a bug. As a workaround I transformed the slash to/from a semi-colon – Einar Bjerve Feb 09 '15 at 20:58
  • That's a good solution. I base64 encoded my paths then wrote a custom solr converter to decode/encode as appropriate. – tlavarea Feb 12 '15 at 14:20

1 Answers1

1

I like idea with converter. We can add new class ContentPath:

@AllArgsConstructor
@ToString
@Getter
public class ContentPath {
    private String path;
}

Then new converter needs to be registered while SolrTemplate creation:

SolrTemplate solrTemplate = ...
DefaultQueryParser defaultQueryParser = new DefaultQueryParser();
defaultQueryParser.registerConverter(new Converter<ContentPath, String>() {

    @Override
    public String convert(ContentPath o) {
        return escape(o.getPath());
    }

});
solrTemplate.registerQueryParser(Query.class, defaultQueryParser);

Last change needs to be done while query definition:

new SimpleQuery((new Criteria(IDENTIFIER)).is(new ContentPath(id)));