Of Code and Me

Somewhere to write down all the stuff I'm going to forget and then need

Using Solr and Lucene with Asp.Net and Windows May 28, 2010

Filed under: Solr,Web — Rupert Bates @ 5:10 pm

Lucene and the Solr server layer which builds on it are probably the most widely used search technology on the internet today. And guardian.co.uk has recently adopted it as our main search technology and the platform on which our OpenPlatform is based. Because it is by the Apache foundation though I think there is a perception amongst .Net developers that it will be very difficult to use from .Net and will need to run on Linux; this is completely wrong. I have recently been working on incorporating it into an Asp.Net site which we have and it has been very easy indeed.

First step is to download Solr from here or you can also get “A comprehensive, tested version of Apache Solr based on the most recent stable Solr version with additional utilities, bug fixes and performance enhancements.” from Lucid Imagination. I just went with the standard Apache version.

You also need the Java runtime installed, preferably version 6.

Extract the zip file into a directory on your hard drive, go to that directory in a command prompt, then to the \examples directory and type java -jar start.jar. You should see the server start up and spit out a load of information, and that’s it! Up and running on Windows. Just to make sure, go to http://localhost:8983/solr/admin/ and you should see the Solr admin site.

The next thing to do is to work through the excellent tutorial here to get an idea of how Solr works. In my next post I’ll talk about defining schemas and connecting to Solr from .Net.

Advertisements
 

Convert any IEnumerable to a comma separated string of values May 25, 2010

Filed under: Uncategorized — Rupert Bates @ 1:12 pm

This extension method allows you to create a comma separated string from any IEnumerable by passing in a function which takes T and returns the string value which you are interested in:

public static string AsCommaSeparatedString<T>(this IEnumerable<T> enumerable, Func<T, string> value)
        {
            StringBuilder sb = new StringBuilder();
            foreach (T item in enumerable)
            {
                sb.Append(value(item));
                sb.Append(",");
            }
            return sb.ToString().TrimEnd(new []{','});
        }

Use it as follows:

            //get a commma separated list of usernames
            var usernames = users.AsCommaSeparatedList(u => u.Name);
 

Update environment variables in Powershell May 20, 2010

Filed under: Powershell,Systems Administration — Rupert Bates @ 9:27 am

To update an environment variable in Powershell in a way that lasts beyond the current session you can use the SetEnvironmentVariable method:

[Environment]::SetEnvironmentVariable(“theVariableToSet”, “theNewValue, “User”)

The final parameter determines the scope of the variable and can be one of Process, User or Machine.

So to add to your path you can use the following:

$path = [environment]::GetEnvironmentVariable(“path”,”User”) + “C:\Program Files\Git\bin;”
[Environment]::SetEnvironmentVariable(“path”, $path, “User”)