‘api’ Archive

Sphinx Multi-Value Attributes

6 July 2009 11:03

Sphinx allows you to create a multi-value attribute index which is great for document tags or categories. By default if you search across these the search is an OR match. If you want to run an AND match you must specify multiple filters rather than passing an array of values.

In ColdFusion using the Java API requires a little tweak as you can’t pass a normal ColdFusion array to sphinx, you have to use a basic java type array. Tim Blair has a good article on how to create them but here’s an example of calling the API.

This example will search for document tagged with either 100 or 200. Note the array creation method.

<cfset variables.sphinx = createobject("java", "org.sphx.api.SphinxClient").init()>
<cfset variables.sphinx.SetLimits(0, 10)>
<cfset variables.arrObj = createobject("java", "java.lang.reflect.Array")>
<cfset variables.jClass = createobject("java", "java.lang.Integer").TYPE>
<cfset variables.jArr = variables.arrObj.newInstance(variables.jClass, 2)>
<cfset variables.arrObj.setInt(variables.jArr, 0, 100)>
<cfset variables.arrObj.setInt(variables.jArr, 1, 200)>
<cfset variables.sphinx.SetFilter("tag", variables.jArr, FALSE)>

If you want to search for 100 AND 200 you’d do it like this:

<cfset variables.sphinx = createobject("java", "org.sphx.api.SphinxClient").init()>
<cfset variables.sphinx.SetLimits(0, 10)>
<cfset variables.sphinx.SetFilter("tag", 100, FALSE)>
<cfset variables.sphinx.SetFilter("tag", 200, FALSE)>

Filed: Technology // Tagged: , , , , // No Comments

sphinx, ColdFusion and geodist

30 June 2009 23:16

Over the past few days I’ve been looking into a new search engine for an application. The chosen search daemon is sphinx and it’ll be called via the Java API (that comes with sphinx) from ColdFusion. Getting the basic searching going was pretty easy, creating an index, do a basic search, set some filters, done. The next step was to look into geodist matching, calcuating how far apart two latitude / longitude’s are, slightly more tricky. In CF you can do it with some crazy curvature of the earth calculations but in sphinx it’s real easy – when you know what it wants. The key there is radians.

You’ll find plenty of tidbits on the web about this but no real end to end examples so hopefully this will help!

If you need to geocode some postcode or zip values I can highly recommend tinygeocoder.com. It’s basic and gives you a straight lat / long value which is perfect. If you want something with a bit more information you’ll need to check out the Yahoo! YDN geocode API or Google’s HTTP API. I’ve got the YDN working with ColdFusion (I’ll post about that later).

First up you’ll need some data, I’ve created a basic DB table called sphinx_test.sql which you can download. It’s been written for MySQL. Quick note on the float values, if you don’t specific float 10,6 (or something similar) your lat/longs will automatically be chopped to only 4 decimal places.

The SQL contains 3 locations in England. The British Airways London Eye (51.502893,-0.118811), Millennium Dome (51.501984,0.004764) and Windsor Castle (51.481971,-0.600686).

Once you’ve created your table you’re ready to create an index. I’m going to assume you’ve already installed sphinx in /opt/sphinx (this is on a CentOS platform). To get the Java API part for ColdFusion is easy as well.

[[email protected] ~]# cd /root/sphinx-0.9.9-rc2/api/java
[[email protected] ~]# make
[[email protected] ~]# cp sphinxapi.jar /opt/coldfusion8/runtime/servers/lib
[[email protected] ~]# /opt/coldfusion8/bin/coldfusion restart

This is the sphinx.conf you’ll need to get this running. Now there’s a whole load of stuff you could do in the configure file but I’ll just give you a basic one which should be enough.

Note you have two options when it comes to getting the lat/long from MySQL, you can either use RADIANS(val) or use a pre-stored the radian value of the lat / long In this case we’re going to use the latter but I’ve included a commented line for the former.

Now for the actual code that does the work. I’ve created a file called sphinx.cfm (view as text or click it to actually see it working) with comments inline. The interesting parts are:

<!--- sort order must be @geodist to allow distance matching --->
<cfset variables.sphinx.SetSortMode(variables.sphinx.SPH_SORT_EXTENDED, [email protected] desc')>

<!--- where are we starting, this will be the london eye latlong converted to radians --->
<cfset variables.sphinx.SetGeoAnchor('latitude', 'longitude', degreesToRadians(51.502893), degreesToRadians(-0.118811))>

All of the API calls are the same as the documented PHP functions so you can refer to the documentation for more information.

Thanks go out to various bloggers and Tim to get this running.

Filed: Technology // Tagged: , , , , // No Comments

Calling the Mailbuild API from CF

4 December 2007 15:42

Had to help with someone out today who was trying to call the Mailbuild API from ColdFusion. It’s a webservice and was proving a little annoying to get going, but after a bit of playing the final code looks like this.

params = structNew();
params.ApiKey = '{yourkey}';
params.ListID = '{yourlistid}';
params.Email = [email protected]';
params.Name = 'Name';

<cfinvoke webservice="http://{company}.createsend.com/api/api.asmx?wsdl"
parameters = "#params#">

This is adding a new subscriber to a given list. Hopefully this might help someone integrating with CF!

Filed: Technology // Tagged: , , // No Comments

Top 10 Web Developer Libraries

26 June 2006 20:35

One of the many lists you’ll read and then forget about again, Top 10 Web Developer Libraries. Still quite useful if you’ve never seen any of the web2.0 stuff about.

Filed: General // Tagged: , , // No Comments

Google Web Toolkit

17 May 2006 12:28

Another beta release from Google:

Google Web Toolkit (GWT) is a Java software development framework that makes writing AJAX applications like Google Maps and Gmail easy for developers who don’t speak browser quirks as a second language. Writing dynamic web applications today is a tedious and error-prone process; you spend 90% of your time working around subtle incompatabilities between web browsers and platforms, and JavaScript’s lack of modularity makes sharing, testing, and reusing AJAX components difficult and fragile.

Google Web Toolkit – Build AJAX apps in the Java language

Filed: Technology // Tagged: , , // No Comments

Twitter   •   About   •   Contact
©2017 Ian Winter. All Rights Reserved.   •   Powered by WordPress   •   Hosted at Memset