Basic Retrieval Tasks in Java

From LemurWiki

Jump to: navigation, search

To perform a basic retrieval using the Java wrappers, first, create a QueryEnvironment object, and use the addIndex call to open your index. Use the runQuery call to get query results. To get the names of the documents, use the documentMetadata call.

The majority of the functionality in the exposed QueryEnvironment class in Java in much the same as the C++ equivalent. For detailed information, see the QueryEnvironment JavaDoc.

[edit] Example Retrieval in Java

The following code snippet is an example of how to use the Indri retrieval functions from Java. The main class (RunQuery) takes two arguments - the first is the path to the index, and the second is the query. The code simply opens the index, runs the query, and dumps out the raw results.

  1.  
  2. import lemurproject.indri.*;
  3.  
  4. public class RunQuery throws Exception {
  5. public static void main( String[] args ) {
  6. QueryEnvironment env = new QueryEnvironment();
  7. String myIndex = args[1];
  8. String myQuery = args[2];
  9. ScoredExtentResult[] results;
  10. String[] names;
  11.  
  12. // open an Indri repository
  13. env.addIndex( myIndex );
  14.  
  15. // run an Indri query, returning 10 results
  16. results = env.runQuery( myQuery, 10 );
  17.  
  18. // fetch the names of the retrieved documents
  19. names = env.documentMetadata( results, "docno" );
  20.  
  21. for( int i=0; i<results.length; i++ ) {
  22. System.out.println( names[i] + " " +
  23. results[i].score + " " +
  24. results[i].begin + " " +
  25. results[i].end );
  26. }
  27.  
  28. env.close();
  29. }
  30. }
  31.  
Personal tools