com.hp.hpl.jena.util
Class ModelIndex

java.lang.Object
  |
  +--com.hp.hpl.jena.util.ModelIndex

public class ModelIndex
extends java.lang.Object

Utility that provides a simple wrapper onto a Lucene text index for performing searches across a Model or MultiModel.

Simple literal-valued properties are indexed as text strings.

Properties pointing to resources are handled in several ways. If the resource is a bNode the literal-valued properties of that bNode can be optionally added as index values for the root resource. Resources that appear in an optional ontology will be indexed using label terms for the class/concept in the ontology (uses rdfs:label and skos labels). Otherwise the localName of the resource is used as the index.

The fields used for properties can either be full URI's or just the localName part (to ease interactive construction of Lucene query strings).

Version:
$Revision: 1.4 $ on $Date: 2004/06/30 15:19:22 $
Author:
Dave Reynolds

Nested Class Summary
static class ModelIndex.SearchResults
          Inner class used to return the results from a free text search.
 
Field Summary
static java.lang.String ALL_FIELD
          The field name for all index terms
static java.lang.String URI_FIELD
          The field name for the base URI
 
Constructor Summary
ModelIndex(java.lang.String path, com.hp.hpl.jena.rdf.model.Model data, com.hp.hpl.jena.rdf.model.Model ontology)
          Constructor.
 
Method Summary
protected  void addBnodeClosure(com.hp.hpl.jena.rdf.model.Resource base, java.lang.StringBuffer value, java.util.Set found)
          Add all literal values fo the given bNode to the index string
protected  void addLabelStrings(com.hp.hpl.jena.rdf.model.Property p, com.hp.hpl.jena.rdf.model.Resource base, java.lang.StringBuffer value)
          Add to the current index string all literal values of the given property in the ontology.
 void addModelIndex(com.hp.hpl.jena.rdf.model.Model newData)
          Add a new set of data to the index.
protected  void doIndex(com.hp.hpl.jena.rdf.model.Model newData, org.apache.lucene.index.IndexWriter indexWriter)
          Index all the subject nodes in the given RDF model.
protected  org.apache.lucene.analysis.Analyzer getAnalyzer()
          Return the default analyzer to use for string tokenizing.
protected static com.hp.hpl.jena.rdf.model.Resource getResourceFor(java.lang.String label, com.hp.hpl.jena.rdf.model.Model data)
          Return a resource from its uri/bnode label
protected static java.lang.String getUriFor(com.hp.hpl.jena.rdf.model.Resource r)
          Return a uri/bnode label for a resource
 void index()
          Rebuild the entire index against the given RDF data, previous index information will be lost.
 java.lang.Object isQueryWellformed(java.lang.String query)
          Check a query string is legal.
static void main(java.lang.String[] args)
          Temporary development only.
 ModelIndex.SearchResults search(java.lang.String query)
          Search the index.
 void setIndexBnodeClosure(boolean bnodeClosure)
          Set how bNode values are indexed
 void setIndexByLocalName(boolean localName)
          Set how property names are mapped to searchable fields.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

URI_FIELD

public static final java.lang.String URI_FIELD
The field name for the base URI

See Also:
Constant Field Values

ALL_FIELD

public static final java.lang.String ALL_FIELD
The field name for all index terms

See Also:
Constant Field Values
Constructor Detail

ModelIndex

public ModelIndex(java.lang.String path,
                  com.hp.hpl.jena.rdf.model.Model data,
                  com.hp.hpl.jena.rdf.model.Model ontology)
Constructor.

Parameters:
path - the file path to where the index is stored
data - the data to be searched (which may be a MultiModel or other union as well as a simple model)
ontology - an optional Model containing any class/concept information, ObjectProperties pointing to resources in the ontology will be indexed by label.
Method Detail

index

public void index()
           throws java.io.IOException
Rebuild the entire index against the given RDF data, previous index information will be lost.

java.io.IOException

addModelIndex

public void addModelIndex(com.hp.hpl.jena.rdf.model.Model newData)
                   throws java.io.IOException
Add a new set of data to the index. Existing information will not be lost. Useful when indexing over a MultiModel and a new Model is being added to the MultiModel. The original data Model is not modified by this operation.

java.io.IOException

setIndexByLocalName

public void setIndexByLocalName(boolean localName)
Set how property names are mapped to searchable fields.

Parameters:
localName - if true then only the localName part of the property name is used used as the field name.

setIndexBnodeClosure

public void setIndexBnodeClosure(boolean bnodeClosure)
Set how bNode values are indexed

Parameters:
bnodeClosure - if true then the label of a bNode is taken to be the union of all property values of the bNode (including recursive bNodes)

search

public ModelIndex.SearchResults search(java.lang.String query)
                                throws com.hp.hpl.jena.shared.JenaException
Search the index. If the index doesn't yet exist it will be created. Throws a JenaException if the query is malformed or the index can't be found or created.

Parameters:
query - a lucene query string (which thus supports fielded searches and boolean searches as well as standard free text)
Returns:
a SearchResults object from which the results can be obtained.
com.hp.hpl.jena.shared.JenaException

isQueryWellformed

public java.lang.Object isQueryWellformed(java.lang.String query)
Check a query string is legal. Return an error message if it is not otherwise return Boolean.TRUE.


doIndex

protected void doIndex(com.hp.hpl.jena.rdf.model.Model newData,
                       org.apache.lucene.index.IndexWriter indexWriter)
                throws java.io.IOException
Index all the subject nodes in the given RDF model.

java.io.IOException

addLabelStrings

protected void addLabelStrings(com.hp.hpl.jena.rdf.model.Property p,
                               com.hp.hpl.jena.rdf.model.Resource base,
                               java.lang.StringBuffer value)
Add to the current index string all literal values of the given property in the ontology.


addBnodeClosure

protected void addBnodeClosure(com.hp.hpl.jena.rdf.model.Resource base,
                               java.lang.StringBuffer value,
                               java.util.Set found)
Add all literal values fo the given bNode to the index string


getAnalyzer

protected org.apache.lucene.analysis.Analyzer getAnalyzer()
Return the default analyzer to use for string tokenizing.


getUriFor

protected static java.lang.String getUriFor(com.hp.hpl.jena.rdf.model.Resource r)
Return a uri/bnode label for a resource


getResourceFor

protected static com.hp.hpl.jena.rdf.model.Resource getResourceFor(java.lang.String label,
                                                                   com.hp.hpl.jena.rdf.model.Model data)
Return a resource from its uri/bnode label


main

public static void main(java.lang.String[] args)
Temporary development only.



Copyright © 2004 Hewlett-Packard. All Rights Reserved.