ducene 0.0.6 ducene: ^0.0.6 copied to clipboard
An instant search library for Dart.
ducene #
An instant search library for Dart.
Demo #
Usage #
A simple usage example:
import 'package:ducene/ducene.dart';
Analyzer st = new StandardAnalyzer();
// Store the index in memory:
IndexDirectory directory = new RAMIndexDirectory();
// To store an index on disk, use this instead:
//IndexDirectory directory = new FSIndexDirectory(new Directory("testindex"));
IndexWriter writer = new IndexWriter(directory);
String text = 'This is the text of #1.';
Document doc1 = new Document()
.append('id', '1')
.append('text', text, analyzer: st)
.append('filter', 'x')
.append('cat', 'CAT-A')
.append('subcat', 'CAT-A-1')
.append('price', '80');
text = 'This is the text of #2.';
Document doc2 = new Document()
.append('id', '2')
.append('text', text, analyzer: st)
.append('filter', 'x')
.append('cat', ['CAT-B', 'CAT-A'])
.append('subcat', 'CAT-A-2')
.append('price', '70');
text = "the text which 'or' matched";
Document doc3 = new Document()
.append('id', '3')
.append('text', text, analyzer: st)
.append('filter', 'x')
.append('cat', 'CAT-C')
.append('subcat', 'CAT-C-1')
.append('price', '100');
writer.write([doc1, doc2, doc3]);
// Now search the index:
IndexReader reader = DirectoryReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
BoolQuery query = new BoolQuery(Op.or)
.append('text', 'this text', analyzer: st)
.append('filter', 'x', filter: true);
TopDocs hits = searcher.search(query, 1000, new MatchScoreSort(query));
assert(3 == searcher.count(query));
assert(3 == hits.totalHits);
// Iterate through the results:
for (ScoreDoc hit in hits.scoreDocs) {
Document hitDoc = searcher.doc(hit.doc);
assert(hitDoc.get('text').contains('the text'));
print('${hitDoc.get('id')} : ${hit.score.toString()}');
// Highlight:
String snippet
= FieldHighlight.getSnippet(query, 'text', hitDoc.get('text'));
assert(snippet.contains('<b>'));
print(snippet);
}
// Facet + Stats:
Map<String,Set<int>> facet = FieldFacet.getCount(hits.docSet, 'cat', reader);
assert(facet.keys.toList()[0] == 'CAT-A'
&& facet.values.toList()[0].length == 2);
var stats_A = FieldStats.getStats(facet.values.toList()[0], 'price', reader);
assert(stats_A['sum'] == 150);
assert(stats_A['mean'] == 75);
assert(facet.keys.toList()[1] == 'CAT-B'
&& facet.values.toList()[1].length == 1);
assert(facet.keys.toList()[2] == 'CAT-C'
&& facet.values.toList()[2].length == 1);
var stats_C = FieldStats.getStats(facet.values.toList()[2], 'price', reader);
assert(stats_C['min'] == 100);
assert(stats_C['max'] == 100);
var subFacet = FieldFacet.getCount(facet.values.toList()[0], 'subcat', reader);
assert(subFacet.keys.toList()[0] == 'CAT-A-1'
&& subFacet.values.toList()[0].length == 1);
var stats_A_1 = FieldStats.getStats(subFacet.values.toList()[0], 'price', reader);
assert(stats_A_1['sum'] == 80);
assert(subFacet.keys.toList()[1] == 'CAT-A-2'
&& subFacet.values.toList()[1].length == 1);
var stats_A_2 = FieldStats.getStats(subFacet.values.toList()[1], 'price', reader);
assert(stats_A_2['sum'] == 70);
Features and bugs #
Please file feature requests and bugs at the issue tracker.