Example: Multi-Valued Fields

This is a basic example of indexing a document where there are two fields with the same name but each with different values, performing a search for that document and then enumerating the fields to a console window.

var indexName = "MyIndexName";

// Instantiate and open the LucandraWriter.
var indexWriter = new LucandraWriter(indexName);

// Instantiate a Lucene analyzer (you can use any analyzer).
var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);

// Insert a document with multi-valued fields.
var doc = new Document();
doc.Add(new Field("author", "Someone Cool", Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("title", "A book by Someone Cool", Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("dedication", "Someone Cool's Mom", Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("dedication", "Someone Cool's Dad", Field.Store.YES, Field.Index.ANALYZED));
indexWriter.AddDocument(doc, analyzer);

// Instantiate a LucandraReader and an IndexSearcher on the reader.
var indexReader = new LucandraReader(indexName);
var indexSearcher = new IndexSearcher(indexReader);

// Create the query parser.
var qp = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "dedication", analyzer);

// Perform the search.  This should return one document.
var query = qp.Parse("+dedication:Someone");
var topDocs = indexSearcher.Search(query, 10);

// Retrieve the complete document from Cassandra.
var resultingDoc = indexReader.Doc(topDocs.scoreDocs[0]);

// Get the two "dedication" fields from the document.
var fields = resultingDoc.GetFields("dedication");

// Print the resulting field values to Console.Out.
foreach(var field in fields)

Last edited Nov 24, 2010 at 8:17 AM by cylwit, version 2


No comments yet.