ducene 0.0.2 copy "ducene: ^0.0.2" to clipboard
ducene: ^0.0.2 copied to clipboard

outdatedDart 1 only

An instant search library for Dart.

ducene #

An instant search library for Dart.

Usage #

A simple usage example:

  Analyzer kw = new KeywordAnalyzer();
  Analyzer ws = new WhitespaceAnalyzer();

  // 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', kw)
    .append('text', text, ws)
    .append('filter', 'x', kw)
    .append('cat', 'CAT-A', kw)
    .append('subcat', 'CAT-A-1', kw)
    .append('price', '80', kw);
  text = 'This is the text of #2.';
  Document doc2 = new Document()
    .append('id', '2', kw)
    .append('text', text, ws)
    .append('filter', 'x', kw)
    .append('cat', 'CAT-A', kw)
    .append('subcat', 'CAT-A-2', kw)
    .append('price', '70', kw);
  text = "the text which 'or' matched";
  Document doc3 = new Document()
    .append('id', '3', kw)
    .append('text', text, ws)
    .append('filter', 'x', kw)
    .append('cat', 'CAT-C', kw)
    .append('subcat', 'CAT-C-1', kw)
    .append('price', '100', kw);
  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', ws)
    .append('filter', 'x', kw, 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, ['id', 'text']);
    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);

Features and bugs #

Please file feature requests and bugs at the issue tracker.

0
likes
0
pub points
0%
popularity

Publisher

unverified uploader

An instant search library for Dart.

Homepage

License

unknown (LICENSE)

More

Packages that depend on ducene