Working with Text Search
Topaz supports indexing of literal and blob fields so that an application may mix semantic searches and free text searches in the same query.
For example, the following query will return all foaf:Person objects with givenname starting with 'sam'.
select p from FoafPerson p where search(p.givenname, 'sam*');
Searching blob fields are no different. The following search will search all 'content' blobs of TextRepresentation? objects and return associated Photo objects for the matching TextRepresentation objects:
select p from Photo p, TextRepresentation t where t.photo = p and search (t.content, 'Hawaii scuba');
The search string may contain any of the keywords that lucene supports. For details please see http://lucene.apache.org/java/1_4_3/queryparsersyntax.html
An exception worth noting here is that wild card searches with leading wild-cards are not supported in the lucene resolver. This would mean a search like '*cot*' cannot be performed.
Setting up indexing
Setting up fields for indexing is done by adding an @Searchable annotation. In its simplest form, the Searchable annotation specifies an index name. That is all that is needed for this example.
Additional options include specifying the predicate-uri to associate this within the index or to set up analyzers and pre-processors. These are discussed in detail in the [<insert-link> Text Search] chapter.
@@ -6,6 +6,7 @@ import java.util.Set; import org.topazproject.otm.CascadeType; import org.topazproject.otm.annotations.Entity; import org.topazproject.otm.annotations.Predicate; +import org.topazproject.otm.annotations.Searchable; @Entity(types={"foaf:Person"}) public class FoafPerson extends FoafAgent { @@ -15,10 +16,12 @@ public class FoafPerson extends FoafAgent { public String getGivenname() {return givenname;} @Predicate() + @Searchable(index="lucene") public void setGivenname(String name) {this.givenname = name;} public String getSurname() {return surname;} @Predicate() + @Searchable(index="lucene") public void setSurname(String name) {this.surname = name;} public Set<Photo> getMyPhotos() {return myPhotos;}
Configuring the index
In the above example, the name of the index that was used was 'lucene'. We now need to make sure this is configured and created in Mulgara. So all we need to do is to add this in our package-info.java as :
@@ -6,7 +6,9 @@ @Graph(id = "prefix", uri = "local:///topazproject#prefix", type = Rdf.mulgara + "PrefixGraph"), @Graph(id = "photo", uri = "local:///topazproject#photo"), - @Graph(id = "foaf", uri = "local:///topazproject#foaf") + @Graph(id = "foaf", uri = "local:///topazproject#foaf"), + @Graph(id = "lucene", uri = "local:///topazproject#lucene", + type = Rdf.mulgara + "LuceneModel") }) @Aliases({
This will ensure that Topaz now can translate the index name into a graph-uri in Mulgara. Also our TopazConfigurator now ensures that this graph is created in Mulgara prior to its use.
Doing searches
Now that the foaf:Person names are all indexed, we can now improve our findPeople() method in PersonService? to incorporate wild card text-searches.
@@ -17,11 +17,18 @@ public class PersonService { String query = "select p from FoafPerson p"; String next = " where "; if (givenname.length() > 0) { - query = query + next + " p.givenname = '" + givenname + "'"; + if (wildmatch) + query = query + next + "search(p.givenname, '" + givenname + "')"; + else + query = query + next + "p.givenname = '" + givenname + "'"; next = " and "; } - if (surname.length() > 0) - query = query + next + " p.surname = '" + surname + "'"; + if (surname.length() > 0) { + if (wildmatch) + query = query + next + "search(p.surname, '" + surname + "')"; + else + query = query + next + "p.surname = '" + surname + "'"; + } Results results = session.createQuery(query + ";").execute();
That is all there is to it. Now users can enter wild-cards or other lucene advanced search options in the name field for our app to look-up the name.
Running it
The full source code can be downloaded from topaz-photo-example-5.zip. 'mvn jetty:run-war' will run this. Point your browser to http://localhost:8080/topaz-photo-example/photoManager to try it out.
Attachments
- topaz-photo-example-5.zip (15.2 kB) -
Source Code
, added by pradeep on 01/21/09 12:02:28.
