Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License or as specified alternatively below. You may obtain a copy of
8 : * the License at http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * Major Contributor(s):
16 : * Copyright (C) 2012 Gert van Valkenhoef <g.h.m.van.valkenhoef@rug.nl>
17 : * (initial developer)
18 : *
19 : * All Rights Reserved.
20 : *
21 : * For minor contributions see the git repository.
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
25 : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
26 : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
27 : * instead of those above.
28 : */
29 :
30 : #include <helpcompiler/HelpSearch.hxx>
31 : #include <osl/file.hxx>
32 : #include <osl/thread.hxx>
33 :
34 : #include "LuceneHelper.hxx"
35 :
36 0 : HelpSearch::HelpSearch(rtl::OUString const &lang, rtl::OUString const &indexDir)
37 0 : : d_lang(lang)
38 : {
39 0 : rtl::OUString ustrSystemPath;
40 0 : osl::File::getSystemPathFromFileURL(indexDir, ustrSystemPath);
41 0 : d_indexDir = rtl::OUStringToOString(ustrSystemPath, osl_getThreadTextEncoding());
42 0 : }
43 :
44 0 : bool HelpSearch::query(rtl::OUString const &queryStr, bool captionOnly,
45 : std::vector<rtl::OUString> &rDocuments, std::vector<float> &rScores) {
46 :
47 0 : lucene::index::IndexReader *reader = lucene::index::IndexReader::open(d_indexDir.getStr());
48 0 : lucene::search::IndexSearcher searcher(reader);
49 :
50 0 : TCHAR captionField[] = L"caption";
51 0 : TCHAR contentField[] = L"content";
52 0 : TCHAR *field = captionOnly ? captionField : contentField;
53 :
54 0 : bool isWildcard = queryStr[queryStr.getLength() - 1] == L'*';
55 0 : std::vector<TCHAR> aQueryStr(OUStringToTCHARVec(queryStr));
56 : lucene::search::Query *pQuery;
57 0 : if (isWildcard)
58 0 : pQuery = _CLNEW lucene::search::WildcardQuery(_CLNEW lucene::index::Term(field, &aQueryStr[0]));
59 : else
60 0 : pQuery = _CLNEW lucene::search::TermQuery(_CLNEW lucene::index::Term(field, &aQueryStr[0]));
61 :
62 0 : lucene::search::Hits *hits = searcher.search(pQuery);
63 0 : for (unsigned i = 0; i < hits->length(); ++i) {
64 0 : lucene::document::Document &doc = hits->doc(i); // Document* belongs to Hits.
65 0 : wchar_t const *path = doc.get(L"path");
66 0 : rDocuments.push_back(TCHARArrayToOUString(path != 0 ? path : L""));
67 0 : rScores.push_back(hits->score(i));
68 : }
69 :
70 0 : _CLDELETE(hits);
71 0 : _CLDELETE(pQuery);
72 :
73 0 : reader->close();
74 0 : _CLDELETE(reader);
75 :
76 0 : return true;
77 : }
78 :
79 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|