1 | package org.xapian.examples;
|
---|
2 |
|
---|
3 | import org.xapian.Database;
|
---|
4 | import org.xapian.Document;
|
---|
5 | import org.xapian.Enquire;
|
---|
6 | import org.xapian.MSet;
|
---|
7 | import org.xapian.MSetIterator;
|
---|
8 | import org.xapian.Query;
|
---|
9 | import org.xapian.QueryParser;
|
---|
10 | import org.xapian.Stem;
|
---|
11 |
|
---|
12 | public class Search1 {
|
---|
13 |
|
---|
14 | // Command line args - dbpath querystring
|
---|
15 | public static void main(String[] args)
|
---|
16 | {
|
---|
17 | if(args.length < 2)
|
---|
18 | {
|
---|
19 | System.out.println("Insufficient number of arguments (should be dbpath querystring)");
|
---|
20 | return;
|
---|
21 | }
|
---|
22 | search(args[0], args[1]);
|
---|
23 | }
|
---|
24 |
|
---|
25 | public static void search(String dbpath, String queryString)
|
---|
26 | {
|
---|
27 | search(dbpath, queryString, 0, 10);
|
---|
28 | }
|
---|
29 |
|
---|
30 | public static void search(String dbpath, String queryString, int offset, int pagesize)
|
---|
31 | {
|
---|
32 | // offset - defines starting point within result set
|
---|
33 | // pagesize - defines number of records to retrieve
|
---|
34 |
|
---|
35 | // Open the databse we're going to search.
|
---|
36 | Database db = new Database(dbpath);
|
---|
37 |
|
---|
38 | // Set up a QueryParser with a stemmer and suitable prefixes
|
---|
39 | QueryParser queryParser = new QueryParser();
|
---|
40 | queryParser.setStemmer(new Stem("en"));
|
---|
41 | queryParser.setStemmingStrategy(QueryParser.stem_strategy.STEM_SOME);
|
---|
42 | // Start of prefix configuration.
|
---|
43 | queryParser.addPrefix("title", "S");
|
---|
44 | queryParser.addPrefix("description", "XD");
|
---|
45 | // End of prefix configuration.
|
---|
46 |
|
---|
47 | // And parse the query
|
---|
48 | Query query = queryParser.parseQuery(queryString);
|
---|
49 |
|
---|
50 | // Use an Enquire object on the database to run the query
|
---|
51 | Enquire enquire = new Enquire(db);
|
---|
52 | enquire.setQuery(query);
|
---|
53 |
|
---|
54 | // And print out something about each match
|
---|
55 | MSet mset= enquire.getMSet(offset, pagesize);
|
---|
56 | MSetIterator msetIterator = mset.begin();
|
---|
57 |
|
---|
58 | while(msetIterator.hasNext())
|
---|
59 | {
|
---|
60 | long rank = msetIterator.getRank();
|
---|
61 | long docID = msetIterator.getDocId();
|
---|
62 | Document doc = db.getDocument(docID);
|
---|
63 | String title = doc.getValue(0);
|
---|
64 |
|
---|
65 | System.out.println((rank+1)+": #"+docID+" "+title);
|
---|
66 | msetIterator.next();
|
---|
67 | }
|
---|
68 |
|
---|
69 | }
|
---|
70 | }
|
---|