LibreOffice Module helpcompiler (master) 1
HelpSearch.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
11#include <osl/file.hxx>
12#include <osl/thread.hxx>
13
14#include "LuceneHelper.hxx"
15#include <CLucene.h>
16
17HelpSearch::HelpSearch(OUString const &indexDir)
18{
19 OUString ustrSystemPath;
20 osl::File::getSystemPathFromFileURL(indexDir, ustrSystemPath);
21 d_indexDir = OUStringToOString(ustrSystemPath, osl_getThreadTextEncoding());
22}
23
24void HelpSearch::query(OUString const &queryStr, bool captionOnly,
25 std::vector<OUString> &rDocuments, std::vector<float> &rScores) {
26
27 lucene::index::IndexReader *reader = lucene::index::IndexReader::open(d_indexDir.getStr());
28 lucene::search::IndexSearcher searcher(reader);
29
30 const TCHAR* field = captionOnly ? L"caption" : L"content";
31
32 bool isWildcard = queryStr[queryStr.getLength() - 1] == L'*';
33 std::vector<TCHAR> aQueryStr(OUStringToTCHARVec(queryStr));
34 lucene::search::Query *pQuery;
35 if (isWildcard)
36 pQuery = _CLNEW lucene::search::WildcardQuery(_CLNEW lucene::index::Term(field, aQueryStr.data()));
37 else
38 pQuery = _CLNEW lucene::search::TermQuery(_CLNEW lucene::index::Term(field, aQueryStr.data()));
39
40 lucene::search::Hits *hits = searcher.search(pQuery);
41 for (size_t i = 0; i < hits->length(); ++i) {
42 lucene::document::Document &doc = hits->doc(i); // Document* belongs to Hits.
43 wchar_t const *path = doc.get(L"path");
44 rDocuments.push_back(TCHARArrayToOUString(path != nullptr ? path : L""));
45 rScores.push_back(hits->score(i));
46 }
47
48 _CLDELETE(hits);
49 _CLDELETE(pQuery);
50
51 reader->close();
52 _CLDELETE(reader);
53}
54
55/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString TCHARArrayToOUString(TCHAR const *str)
std::vector< TCHAR > OUStringToTCHARVec(OUString const &rStr)
void query(OUString const &queryStr, bool captionOnly, std::vector< OUString > &rDocuments, std::vector< float > &rScores)
Query the index for a certain query string.
Definition: HelpSearch.cxx:24
OString d_indexDir
Definition: HelpSearch.hxx:20
HelpSearch(OUString const &indexDir)
Definition: HelpSearch.cxx:17
int i
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)