ILIAS  trunk Revision v11.0_alpha-1769-g99a433fe2dc
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilLuceneSearcher.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
29 {
30  public const TYPE_STANDARD = 1;
31  public const TYPE_USER = 2;
32 
33  private static ?ilLuceneSearcher $instance = null;
34 
38  private int $page_number = 1;
39  private int $type = self::TYPE_STANDARD;
40 
41  protected ilSetting $setting;
42 
43  private function __construct(ilLuceneQueryParser $qp)
44  {
45  global $DIC;
46 
47  $this->setting = $DIC->settings();
48  $this->result = new ilLuceneSearchResult();
49  $this->result->setCallback([$this,'nextResultPage']);
50  $this->query_parser = $qp;
51  }
52 
56  public static function getInstance(ilLuceneQueryParser $qp): self
57  {
58  if (self::$instance instanceof ilLuceneSearcher) {
59  return self::$instance;
60  }
61  return self::$instance = new ilLuceneSearcher($qp);
62  }
63 
67  public function setType(int $a_type): void
68  {
69  $this->type = $a_type;
70  }
71 
75  public function getType(): int
76  {
77  return $this->type;
78  }
79 
80 
84  public function search(): void
85  {
86  $this->performSearch();
87  }
88 
93  public function highlight(array $a_obj_ids): ?ilLuceneHighlighterResultParser
94  {
95  if (!$this->query_parser->getQuery()) {
96  return null;
97  }
98 
99  // Search in combined index
100  try {
101  $res = ilRpcClientFactory::factory('RPCSearchHandler')->highlight(
102  CLIENT_ID . '_' . $this->setting->get('inst_id', '0'),
103  $a_obj_ids,
104  $this->query_parser->getQuery()
105  );
106  } catch (Exception $e) {
107  ilLoggerFactory::getLogger('src')->error('Highlighting failed with message: ' . $e->getMessage());
108  return new ilLuceneHighlighterResultParser();
109  }
110 
111  $this->highlighter = new ilLuceneHighlighterResultParser();
112  $this->highlighter->setResultString($res);
113  $this->highlighter->parse();
114 
115  return $this->highlighter;
116  }
117 
121  public function nextResultPage(): void
122  {
123  $this->page_number++;
124  $this->performSearch();
125  }
126 
131  {
132  return $this->highlighter;
133  }
134 
138  public function getResult(): ilLuceneSearchResult
139  {
140  return $this->result;
141  }
142 
146  public function getPageNumber(): int
147  {
148  return $this->page_number;
149  }
150 
154  protected function performSearch(): void
155  {
156  if (!$this->query_parser->getQuery()) {
157  return;
158  }
159  try {
160  switch ($this->getType()) {
161  case self::TYPE_USER:
163  $res = ilRpcClientFactory::factory('RPCSearchHandler')->searchUsers(
164  CLIENT_ID . '_' . $this->setting->get('inst_id', '0'),
165  $this->query_parser->getQuery()
166  );
167  break;
168 
169  case self::TYPE_STANDARD:
170  default:
171  $res = ilRpcClientFactory::factory('RPCSearchHandler')->search(
172  CLIENT_ID . '_' . $this->setting->get('inst_id', '0'),
173  $this->query_parser->getQuery(),
174  $this->getPageNumber()
175  );
176  break;
177  }
178  ilLoggerFactory::getLogger('src')->debug('Searching for: ' . $this->query_parser->getQuery());
179  } catch (Exception $e) {
180  ilLoggerFactory::getLogger('src')->error('Searching failed with message: ' . $e->getMessage());
181  return;
182  }
183 
184  ilLoggerFactory::getLogger('src')->dump($res);
185 
186  // Parse results
187  $parser = new ilLuceneSearchResultParser($res);
188  $parser->parse($this->result);
189  }
190 }
ilLuceneSearchResult $result
$res
Definition: ltiservices.php:66
Parses result XML from lucene search highlight.
static ilLuceneSearcher $instance
static getLogger(string $a_component_id)
Get component logger.
static getInstance(ilLuceneQueryParser $qp)
Get singleton instance.
static factory(string $a_package, int $a_timeout=0)
Creates an ilRpcClient instance to our ilServer.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getHighlighter()
get highlighter
Reads and parses lucene search results.
Search result implementing iterator interface.
const CLIENT_ID
Definition: constants.php:41
global $DIC
Definition: shib_login.php:22
performSearch()
search lucene
getPageNumber()
get current page number
setType(int $a_type)
Set search type.
nextResultPage()
get next result page
ilLuceneQueryParser $query_parser
__construct(ilLuceneQueryParser $qp)
highlight(array $a_obj_ids)
ilLuceneHighlighterResultParser $highlighter