Click or drag to resize

Getting Started

This walkthrough will guide you through using the fluent API to perform full-text queries and store and delete documents from an index. As an example, we'll be creating an index that contains books.

Prerequisites

This walkthrough assumes that you have installed SphinxConnector.NET and have a working local installation of Sphinx that is configured to listen on 127.0.0.1 and port 9306 using the MySQL protocol. You also need to disable the legacy SphinxQL quirks compatiblity mode by setting compat_sphinxql_magics = 0 in your sphinx.conf file (if you haven't already done so).

Adding the index definition to Sphinx

  1. Open your sphinx.conf file and paste the following index definition:

    index books
    {
      type              = rt
      path              = books
    
      rt_field          = title
      rt_attr_string    = title
      rt_field          = author
      rt_attr_string    = author
      rt_attr_float     = price
      rt_attr_timestamp = releasedate
      rt_attr_uint      = ebookavailable
      rt_attr_multi     = categories
    
      charset_table     = 0..9, A..Z->a..z, a..z
      charset_type      = utf-8
    }
  2. Save the changes you made to the sphinx.conf file.

Setting up SphinxConnector.NET

  1. Create a class named Book:

    C#
    public class Book
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Author { get; set; }
        public decimal Price { get; set; }
        public bool EbookAvailable { get; set; }
        public DateTime ReleaseDate { get; set; }
        public IList<int> Categories { get; set; }
    }
  2. Create and initialize the FulltextStore class:

    C#
    IFulltextStore fulltextStore = new FulltextStore().Initialize();

    We are using the default conventions, which means that we don't have to configure anything. SphinxConnector.NET will map the Book class to the index 'books' and treat the property names converted to lower case, as names for the attributes. Also, SphinxConnector.NET will look at the app.config file for a connection string named 'sphinx' and use that if present. If it doesn't find one, it'll check if another connection string has been configured via ConnectionString. Otherwise the default connection string of 'datasource=localhost;port=9306' will be used.

Inserting Data

  • To insert data into the books index we'll be using the Save method. The following code inserts two books into the index.

    C#
    using (IFulltextSession session = fulltextStore.StartSession())
    {
        session.Save(new Book
                         {
                             Id = 1,
                             Author = "George R.R. Martin",
                             Title = "A Game of Thrones: A Song of Ice and Fire: Book One",
                             EbookAvailable = true,
                             Categories = new [] { 1, 2 },
                             Price = 5.60m,
                             ReleaseDate = new DateTime(1997, 8, 4)
                         });
    
        session.Save(new Book
                         {
                             Id = 2,
                             Author = "George R.R. Martin",
                             Title = "A Clash of Kings: A Song of Ice and Fire: Book Two",
                             EbookAvailable = true,
                             Categories = new [] { 1, 2 },
                             Price = 7.10m,
                             ReleaseDate = new DateTime(2000, 9, 5)
                         });
    
        session.FlushChanges();
    }
    Note Note

    Notice the call to FlushChanges at the end of the using statement. All changes that you make within a session are only persisted the moment you call FlushChanges. All statements that are executed then, are made in a single transaction (one per index).

See Also