build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / helpcompiler / source / HelpSearch.cxx
blob6f0fa2eeaf6a4a2705db22a7d4c26d794384b236
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include <helpcompiler/HelpSearch.hxx>
11 #include <osl/file.hxx>
12 #include <osl/thread.hxx>
14 #include "LuceneHelper.hxx"
16 HelpSearch::HelpSearch(OUString const &indexDir)
18 OUString ustrSystemPath;
19 osl::File::getSystemPathFromFileURL(indexDir, ustrSystemPath);
20 d_indexDir = OUStringToOString(ustrSystemPath, osl_getThreadTextEncoding());
23 void HelpSearch::query(OUString const &queryStr, bool captionOnly,
24 std::vector<OUString> &rDocuments, std::vector<float> &rScores) {
26 lucene::index::IndexReader *reader = lucene::index::IndexReader::open(d_indexDir.getStr());
27 lucene::search::IndexSearcher searcher(reader);
29 const TCHAR* field = captionOnly ? L"caption" : L"content";
31 bool isWildcard = queryStr[queryStr.getLength() - 1] == L'*';
32 std::vector<TCHAR> aQueryStr(OUStringToTCHARVec(queryStr));
33 lucene::search::Query *pQuery;
34 if (isWildcard)
35 pQuery = _CLNEW lucene::search::WildcardQuery(_CLNEW lucene::index::Term(field, &aQueryStr[0]));
36 else
37 pQuery = _CLNEW lucene::search::TermQuery(_CLNEW lucene::index::Term(field, &aQueryStr[0]));
39 lucene::search::Hits *hits = searcher.search(pQuery);
40 for (size_t i = 0; i < hits->length(); ++i) {
41 lucene::document::Document &doc = hits->doc(i); // Document* belongs to Hits.
42 wchar_t const *path = doc.get(L"path");
43 rDocuments.push_back(TCHARArrayToOUString(path != nullptr ? path : L""));
44 rScores.push_back(hits->score(i));
47 _CLDELETE(hits);
48 _CLDELETE(pQuery);
50 reader->close();
51 _CLDELETE(reader);
54 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */