Line data Source code
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 :
10 : #include <helpcompiler/HelpSearch.hxx>
11 : #include <osl/file.hxx>
12 : #include <osl/thread.hxx>
13 :
14 : #include "LuceneHelper.hxx"
15 :
16 0 : HelpSearch::HelpSearch(OUString const &lang, OUString const &indexDir)
17 0 : : d_lang(lang)
18 : {
19 0 : OUString ustrSystemPath;
20 0 : osl::File::getSystemPathFromFileURL(indexDir, ustrSystemPath);
21 0 : d_indexDir = OUStringToOString(ustrSystemPath, osl_getThreadTextEncoding());
22 0 : }
23 :
24 0 : bool HelpSearch::query(OUString const &queryStr, bool captionOnly,
25 : std::vector<OUString> &rDocuments, std::vector<float> &rScores) {
26 :
27 0 : lucene::index::IndexReader *reader = lucene::index::IndexReader::open(d_indexDir.getStr());
28 0 : lucene::search::IndexSearcher searcher(reader);
29 :
30 0 : TCHAR captionField[] = L"caption";
31 0 : TCHAR contentField[] = L"content";
32 0 : TCHAR *field = captionOnly ? captionField : contentField;
33 :
34 0 : bool isWildcard = queryStr[queryStr.getLength() - 1] == L'*';
35 0 : std::vector<TCHAR> aQueryStr(OUStringToTCHARVec(queryStr));
36 : lucene::search::Query *pQuery;
37 0 : if (isWildcard)
38 0 : pQuery = _CLNEW lucene::search::WildcardQuery(_CLNEW lucene::index::Term(field, &aQueryStr[0]));
39 : else
40 0 : pQuery = _CLNEW lucene::search::TermQuery(_CLNEW lucene::index::Term(field, &aQueryStr[0]));
41 :
42 0 : lucene::search::Hits *hits = searcher.search(pQuery);
43 0 : for (unsigned i = 0; i < hits->length(); ++i) {
44 0 : lucene::document::Document &doc = hits->doc(i); // Document* belongs to Hits.
45 0 : wchar_t const *path = doc.get(L"path");
46 0 : rDocuments.push_back(TCHARArrayToOUString(path != 0 ? path : L""));
47 0 : rScores.push_back(hits->score(i));
48 : }
49 :
50 0 : _CLDELETE(hits);
51 0 : _CLDELETE(pQuery);
52 :
53 0 : reader->close();
54 0 : _CLDELETE(reader);
55 :
56 0 : return true;
57 : }
58 :
59 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|