Ticket #84: enquire_sort_bug_demo.php

File enquire_sort_bug_demo.php, 2.3 KB (added by alec, 18 years ago)

demonstration code

Line 
1<?
2 /* FROM phpinfo()
3 * xapian
4 *
5 * Xapian Support => enabled
6 * Xapian Compiled Version => 0.9.6
7 */
8
9 // set up a search db
10 $index_path = "xapian_db";
11 $index_pointer = new_WritableDatabase($index_path, DB_CREATE_OR_OPEN);
12
13 $document_pointer = new_document();
14
15 // add some documents with different values on the 0:th position
16 $number_to_add = 5;
17 for ($i = 1; $i <= $number_to_add; $i++)
18 {
19 $document_pointer = new_document();
20
21 // add a random value in position 0
22 document_add_value($document_pointer, 0, rand(1, 500));
23
24 // add something to search on
25 document_add_term($document_pointer, "bug", 1);
26
27 $document_id = writabledatabase_add_document($index_pointer, $document_pointer);
28 }
29
30 WritableDatabase_flush($index_pointer);
31
32 // now search for 'bug'
33 $query = "bug";
34 $query_parser = new_QueryParser();
35 $query_pointer = QueryParser_parse_query($query_parser, $query);
36
37 // create an enquire resource
38 $enquire_pointer = new_Enquire($index_pointer);
39 Enquire_set_query($enquire_pointer, $query_pointer);
40
41 // order the results by the value in position zero in ascending order (none of these appear to work)
42 Enquire_set_sort_by_value($enquire_pointer, 0, true);
43// Enquire_set_sort_by_value($enquire_pointer, 0, false);
44// Enquire_set_sort_by_value_then_relevance($enquire_pointer, 0, true);
45// Enquire_set_sort_by_value_then_relevance($enquire_pointer, 0, false);
46
47 $result_pointer = Enquire_get_mset($enquire_pointer, 0, 10);
48 $result = getResultArray($result_pointer);
49
50 print_r($result);
51
52
53
54
55
56
57
58
59 function getResultArray($result_pointer)
60 {
61 $result["total_hits"] = MSet_get_matches_estimated($result_pointer);
62
63 $i = 0;
64 $mset_counter = mset_begin($result_pointer);
65 while (!msetiterator_equals($mset_counter, mset_end($result_pointer)))
66 {
67 $document_pointer = msetiterator_get_document($mset_counter);
68
69 $result["result"][$i]["document_id"] = msetiterator_get_docid($mset_counter);
70
71 $value_counter = document_values_begin($document_pointer);
72 while (!valueiterator_equals($value_counter, document_values_end($document_pointer)))
73 {
74 $result["result"][$i]["values"][] = valueiterator_get_value($value_counter);
75
76 valueiterator_next($value_counter);
77 }
78
79 $i++;
80 msetiterator_next($mset_counter);
81 }
82
83 return $result;
84 }
85?>