Add a simple CLucene search app written by Ankur Sethi. I just made some smaller modifications. Don't look at the coding style, it is just for testing at the moment, this has to be integrate somewhere else anyway, there is no point to fix it at this state. There are also some memory leaks...
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@39173 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
SubDir HAIKU_TOP src tests add-ons ;
|
||||
|
||||
SubInclude HAIKU_TOP src tests add-ons input_server ;
|
||||
SubInclude HAIKU_TOP src tests add-ons index_server ;
|
||||
SubInclude HAIKU_TOP src tests add-ons kernel ;
|
||||
SubInclude HAIKU_TOP src tests add-ons media ;
|
||||
SubInclude HAIKU_TOP src tests add-ons opengl ;
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
SubDir HAIKU_TOP src tests add-ons index_server ;
|
||||
|
||||
SubInclude HAIKU_TOP src tests add-ons index_server fulltext_search ;
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* Copyright 2009-2010 Haiku, Inc.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*
|
||||
* Authors:
|
||||
* Ankur Sethi ([email protected])
|
||||
* Clemens Zeidler <[email protected]>
|
||||
*/
|
||||
|
||||
#include "BeaconSearcher.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include <Alert.h>
|
||||
#include <VolumeRoster.h>
|
||||
|
||||
#include "IndexServerPrivate.h"
|
||||
|
||||
|
||||
using namespace lucene::document ;
|
||||
using namespace lucene::search ;
|
||||
using namespace lucene::index ;
|
||||
using namespace lucene::queryParser ;
|
||||
|
||||
|
||||
BeaconSearcher::BeaconSearcher()
|
||||
{
|
||||
BVolumeRoster volumeRoster ;
|
||||
BVolume volume ;
|
||||
IndexSearcher *indexSearcher ;
|
||||
|
||||
while(volumeRoster.GetNextVolume(&volume) == B_OK) {
|
||||
BPath indexPath = GetIndexPath(&volume);
|
||||
if(IndexReader::indexExists(indexPath.Path())) {
|
||||
indexSearcher = new IndexSearcher(indexPath.Path());
|
||||
fSearcherList.AddItem(indexSearcher);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
BeaconSearcher::~BeaconSearcher()
|
||||
{
|
||||
IndexSearcher *indexSearcher;
|
||||
while(fSearcherList.CountItems() > 0) {
|
||||
indexSearcher = (IndexSearcher*)fSearcherList.ItemAt(0);
|
||||
indexSearcher->close();
|
||||
delete indexSearcher;
|
||||
fSearcherList.RemoveItem((int32)0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
BPath
|
||||
BeaconSearcher::GetIndexPath(BVolume *volume)
|
||||
{
|
||||
BDirectory dir;
|
||||
volume->GetRootDirectory(&dir);
|
||||
BPath path(&dir);
|
||||
path.Append(kIndexServerDirectory);
|
||||
path.Append("FullTextAnalyser");
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
BeaconSearcher::Search(const char* stringQuery)
|
||||
{
|
||||
// CLucene expects wide characters everywhere.
|
||||
int size = strlen(stringQuery) * sizeof(wchar_t) ;
|
||||
wchar_t *wStringQuery = new wchar_t[size] ;
|
||||
if (mbstowcs(wStringQuery, stringQuery, size) == -1)
|
||||
return ;
|
||||
|
||||
IndexSearcher *indexSearcher ;
|
||||
Hits *hits ;
|
||||
Query *luceneQuery ;
|
||||
Document doc ;
|
||||
Field *field ;
|
||||
wchar_t *path ;
|
||||
|
||||
/*
|
||||
luceneQuery = QueryParser::parse(wStringQuery, _T("contents"),
|
||||
&fStandardAnalyzer) ;
|
||||
|
||||
hits = fMultiSearcher->search(luceneQuery) ;
|
||||
for(int j = 0 ; j < hits->length() ; j++) {
|
||||
doc = hits->doc(j) ;
|
||||
field = doc.getField(_T("path")) ;
|
||||
path = new wchar_t[B_PATH_NAME_LENGTH * sizeof(wchar_t)] ;
|
||||
wcscpy(path, field->stringValue()) ;
|
||||
fHits.AddItem(path) ;
|
||||
}*/
|
||||
|
||||
for(int i = 0 ; (indexSearcher = (IndexSearcher*)fSearcherList.ItemAt(i))
|
||||
!= NULL ; i++) {
|
||||
luceneQuery = QueryParser::parse(wStringQuery, _T("contents"),
|
||||
&fStandardAnalyzer) ;
|
||||
|
||||
hits = indexSearcher->search(luceneQuery) ;
|
||||
|
||||
for(int j = 0 ; j < hits->length() ; j++) {
|
||||
doc = hits->doc(j) ;
|
||||
field = doc.getField(_T("path")) ;
|
||||
path = new wchar_t[B_PATH_NAME_LENGTH * sizeof(wchar_t)] ;
|
||||
wcscpy(path, field->stringValue()) ;
|
||||
fHits.AddItem(path) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
wchar_t*
|
||||
BeaconSearcher::GetNextHit()
|
||||
{
|
||||
if(fHits.CountItems() != 0) {
|
||||
wchar_t* path = (wchar_t*)fHits.ItemAt(0) ;
|
||||
fHits.RemoveItem((int32)0) ;
|
||||
return path ;
|
||||
}
|
||||
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2009 Haiku, Inc.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*
|
||||
* Authors:
|
||||
* Ankur Sethi ([email protected])
|
||||
*/
|
||||
|
||||
#ifndef _BEACON_SEARCHER_H_
|
||||
#define _BEACON_SEARCHER_H_
|
||||
|
||||
#include <CLucene.h>
|
||||
|
||||
#include <Directory.h>
|
||||
#include <List.h>
|
||||
#include <Path.h>
|
||||
#include <Volume.h>
|
||||
|
||||
// #include <CLucene/search/MultiSearcher.h>
|
||||
using namespace lucene::analysis::standard ;
|
||||
// using namespace lucene::search ;
|
||||
|
||||
|
||||
class BeaconSearcher {
|
||||
public:
|
||||
BeaconSearcher() ;
|
||||
~BeaconSearcher() ;
|
||||
wchar_t* GetNextHit() ;
|
||||
void Search(const char* query) ;
|
||||
|
||||
private:
|
||||
BPath GetIndexPath(BVolume *volume);
|
||||
|
||||
BList fSearcherList;
|
||||
BList fHits;
|
||||
StandardAnalyzer fStandardAnalyzer;
|
||||
// MultiSearcher *fMultiSearcher ;
|
||||
} ;
|
||||
|
||||
#endif /* _BEACON_SEARCHER_H_ */
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
SubDir HAIKU_TOP src tests add-ons index_server fulltext_search ;
|
||||
|
||||
UsePrivateHeaders index_server shared ;
|
||||
SubDirSysHdrs $(HAIKU_CLUCENE_HEADERS) ;
|
||||
|
||||
|
||||
Application FullTextSearch :
|
||||
BeaconSearcher.cpp
|
||||
main.cpp
|
||||
SearchWindow.cpp
|
||||
: be $(HAIKU_CLUCENE_LIBS) $(TARGET_LIBSTDC++)
|
||||
# : Sudoku.rdef
|
||||
;
|
||||
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright 2009 - 2010 Haiku, Inc.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*
|
||||
* Authors:
|
||||
* Ankur Sethi ([email protected])
|
||||
* Clemens Zeidler ([email protected])
|
||||
*/
|
||||
|
||||
#include "SearchWindow.h"
|
||||
|
||||
#include <Alert.h>
|
||||
#include <Application.h>
|
||||
#include <GroupLayout.h>
|
||||
#include <GroupLayoutBuilder.h>
|
||||
#include <Roster.h>
|
||||
|
||||
#include "BeaconSearcher.h"
|
||||
|
||||
|
||||
const uint32 kSearchMessage = '&sea';
|
||||
const uint32 kLaunchMessage = '&lnc';
|
||||
|
||||
|
||||
SearchWindow::SearchWindow(BRect frame)
|
||||
:
|
||||
BWindow(frame, "Fulltext Search", B_TITLED_WINDOW,
|
||||
B_QUIT_ON_WINDOW_CLOSE | B_AUTO_UPDATE_SIZE_LIMITS)
|
||||
{
|
||||
fSearchButton = new BButton("Search", new BMessage(kSearchMessage));
|
||||
fSearchField = new BTextControl("", "", new BMessage(kSearchMessage));
|
||||
|
||||
fSearchResults = new BListView();
|
||||
fSearchResults->SetInvocationMessage(new BMessage(kLaunchMessage));
|
||||
|
||||
fScrollView = new BScrollView("SearchResults", fSearchResults, 0,
|
||||
true, true);
|
||||
|
||||
SetLayout(new BGroupLayout(B_VERTICAL));
|
||||
|
||||
AddChild(BGroupLayoutBuilder(B_VERTICAL, 10)
|
||||
.Add(BGroupLayoutBuilder(B_HORIZONTAL, 10)
|
||||
.Add(fSearchField)
|
||||
.Add(fSearchButton)
|
||||
.SetInsets(5, 5, 5, 5)
|
||||
)
|
||||
.Add(fScrollView)
|
||||
.SetInsets(5, 5, 5, 5)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SearchWindow::MessageReceived(BMessage *message)
|
||||
{
|
||||
switch(message->what) {
|
||||
case kSearchMessage:
|
||||
_Search();
|
||||
break;
|
||||
|
||||
case kLaunchMessage:
|
||||
_LaunchFile(message);
|
||||
break;
|
||||
|
||||
default:
|
||||
BWindow::MessageReceived(message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SearchWindow::_Search()
|
||||
{
|
||||
fSearchResults->MakeEmpty();
|
||||
BeaconSearcher searcher;
|
||||
searcher.Search(fSearchField->Text());
|
||||
wchar_t *wPath;
|
||||
char *path;
|
||||
while((wPath = searcher.GetNextHit()) != NULL) {
|
||||
path = new char[wcslen(wPath)*sizeof(wchar_t)];
|
||||
wcstombs(path, wPath, wcslen(wPath)*sizeof(wchar_t));
|
||||
fSearchResults->AddItem(new BStringItem(path));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SearchWindow::_LaunchFile(BMessage *message)
|
||||
{
|
||||
BListView *searchResults;
|
||||
int32 index;
|
||||
|
||||
message->FindPointer("source", (void**)&searchResults);
|
||||
message->FindInt32("index", &index);
|
||||
BStringItem *result = (BStringItem*)searchResults->ItemAt(index);
|
||||
|
||||
entry_ref ref ;
|
||||
BEntry entry(result->Text());
|
||||
entry.GetRef(&ref);
|
||||
be_roster->Launch(&ref);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2009 - 2010 Haiku, Inc.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*
|
||||
* Authors:
|
||||
* Ankur Sethi ([email protected])
|
||||
* Clemens Zeidler ([email protected])
|
||||
*/
|
||||
#ifndef _SEARCH_WINDOW_H
|
||||
#define _SEARCH_WINDOW_H
|
||||
|
||||
#include <Button.h>
|
||||
#include <ListView.h>
|
||||
#include <ScrollView.h>
|
||||
#include <TextControl.h>
|
||||
#include <Window.h>
|
||||
|
||||
|
||||
class SearchWindow : public BWindow {
|
||||
public:
|
||||
SearchWindow(BRect frame);
|
||||
|
||||
void MessageReceived(BMessage *message);
|
||||
|
||||
private:
|
||||
void _Search();
|
||||
void _LaunchFile(BMessage *message);
|
||||
|
||||
// Window controls.
|
||||
BButton* fSearchButton;
|
||||
BTextControl* fSearchField;
|
||||
BListView* fSearchResults;
|
||||
BScrollView* fScrollView;
|
||||
} ;
|
||||
|
||||
#endif /* _SEARCH_WINDOW_H_ */
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2009-2010 Haiku, Inc.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*
|
||||
* Authors:
|
||||
* Ankur Sethi ([email protected])
|
||||
* Clemens Zeidler <[email protected]>
|
||||
*/
|
||||
#include <Application.h>
|
||||
|
||||
#include "SearchWindow.h"
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
BApplication app("application/x-vnd.Haiku-FullTextSearch");
|
||||
|
||||
SearchWindow* window = new SearchWindow(BRect(50, 50, 500, 250));
|
||||
window->Show();
|
||||
|
||||
app.Run();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user