I am using Apache Solr, Say I search for “B” I want Solr to return “AB”,”BA”,”ABA”
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
query.addFilterQuery("color:*B*");
However, it is throwing an exception, what should I do?
Caused by: org.apache.lucene.queryParser.ParseException: Cannot parse 'color:*B*': '*' or '?' not allowed as first character in WildcardQuery
at org.apache.lucene.queryParser.QueryParser.parse(QueryParser.java:211)
at org.apache.solr.search.LuceneQParser.parse(LuceneQParserPlugin.java:80)
at org.apache.solr.search.QParser.getQuery(QParser.java:142)
at org.apache.solr.handler.component.QueryComponent.prepare(QueryComponent.java:114)
... 17 more
Caused by: org.apache.lucene.queryParser.ParseException: '*' or '?' not allowed as first character in WildcardQuery
When you say AB or ABA or BA, these are one single units or one word each. When you index them they are stored in the inverted index as is i.e ABA or BA etc. When you search for B in the inverted index it is not found. What you need to do is mark your fields as n-gram indexed (instead of type=”text” or type=”string” in your schema.xml mark them as type=”NGram”) which would index partial words too on top of full words. Once you are done with your N-Gram Indexing and then when you search for B you would get all of AB or BA or ABA. But remember that N-Gram indexing is space/time intensive.
For an example, say your field name is color, then in your schema.xml :
Also check whether this portion of XML is present in your schema.xml (if not, the copy paste this):