Wildcards in SolrSharp?

May 13, 2008 at 2:01 PM
Am I missing something obvious?

 I have been trying to get wildcard searches through but have been unable. I finally gave up and just took the "+" and the "*" out of the _specialchars string array in the QueryParameter class.

I think that this API should also have a simple way to create a q=<query> string without depending on the QueryBuilder class. Or at least some method of dictating how the query request string should look explicitly.
Dec 4, 2008 at 2:09 PM
You can set the QueryBuilder's Query property, and the use AddQueryParameter method. That seems pretty explicit to me.
Sep 21, 2009 at 1:48 PM

I believe the real question here with with the QueryParameter class itself. You can add a wildcard symbol to the "Value" property of a QueryParameter object, but this gets automatically escaped when it is converted into a string for use as a query to Solr. In effect, as far as I can tell, you cannot add wildcards using this approach. This leaves you with two options:

  1. Edit the SolrSharp source code, and remove the * and + from the _specialChars string array in QueryParameter, as suggested by TCataldo
  2. Override the QueryParameter class, and override ToString() so that it behaves as before, but without the escaping of the * and the +.

Neither of these strike me as particularly elegant solutions, so if there is a better solution that I have missed then I would be interested to see it!

Jun 6, 2010 at 10:34 PM
Edited Jun 6, 2010 at 10:35 PM

Won't the following code handle this situation:

 

 

	/// <summary>
	/// Represents a wildcard parameter for queries. The value is always assumed to be "*"
	/// </summary>
	class WildcardParameter : QueryParameter
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="WildcardParameter"/> class.
		/// </summary>
		/// <param name="field">The field.</param>
		public WildcardParameter(string field)
			: base(field, "*")
		{
		}

		/// <summary>
		/// Returns a <see cref="System.String"/> that represents this instance.
		/// </summary>
		/// <returns>
		/// A <see cref="System.String"/> that represents this instance.
		/// </returns>
		public override string ToString()
		{
			return string.Format("{0}:*", Field);
		}
	}

 

It can be made more generic, so that "*" part is customizable.

 

Update: Maybe another class such as "VerbatimQueryParameter" might be helpful instead.