Добрый день. Пытаюсь освоить работу с Lucene.NET с NHibernate.NET.
| Код | using NHibernate.Search.Attributes; using NHibernate.Search;
namespace Example1 { [Indexed] public class TempBook { private int id; private string author; private string name; private string summary;
[DocumentId] public virtual int Id { get { return id; } set { id = value; } }
[Field(Index.Tokenized, Store = Store.Yes)] public virtual string Author { get { return author; } set { author = value; } }
[Field(Index.Tokenized, Store = Store.Yes)] public virtual string Name { get { return name; } set { name = value; } }
[Field(Index.Tokenized, Store = Store.Yes)] public virtual string Summary { get { return summary; } set { summary = value; } } } }
|
| Код | <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Example1" namespace="Example1">
<class name="TempBook"> <id name="Id" column="TempBookID" type="Int32" > <generator class="assigned"/> </id> <property name="Author" column="Author" type="String" /> <property name="Name" column="Name" type="String" /> <property name="Summary" column="Summary" type="String" /> </class>
</hibernate-mapping>
|
| Код | <?xml version="1.0" encoding="utf-8" ?> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > <session-factory name="Example1"> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="connection.connection_string">Data Source=.\SQLEXPRESS;Initial Catalog=test;Integrated Security=True</property> <property name="show_sql">true</property> <mapping assembly="Example1"/> </session-factory> </hibernate-configuration>
|
| Код | using System; using System.Collections.Generic; using System.Text; using NHibernate.Cfg; using NHibernate; using NHibernate.Search.Engine; using NHibernate.Search.Storage; //RAMDirectoryProvider
using Lucene.Net; using Lucene.Net.Analysis; //StopAnalyzer using NHibernate.Search; //IFullTextSession using NHibernate.Search.Impl; //SearchInterceptor using Lucene.Net.QueryParsers; using System.Collections; using Lucene.Net.Search; //QueryParser
namespace Example1 { class Program { private static Configuration cfg; private static ISessionFactory sf;
static void Main(string[] args) { Configure(); LuceneQueries();
}
private static void LuceneQueries() {
using (IFullTextSession s = Search.CreateFullTextSession(sf.OpenSession(new SearchInterceptor()))) { QueryParser qp = new QueryParser("Id", new StopAnalyzer());
IQuery NHQuery = s.CreateFullTextQuery(qp.Parse("Author: Doe"), typeof(TempBook));
IList<TempBook> books = NHQuery.List<TempBook>(); } }
private static void Configure() { cfg = new Configuration(); cfg.SetProperty("hibernate.search.default.directory_provider", typeof(RAMDirectoryProvider).AssemblyQualifiedName); cfg.SetProperty(NHibernate.Search.Environment.AnalyzerClass, typeof(StopAnalyzer).AssemblyQualifiedName); cfg.Configure(); sf = cfg.BuildSessionFactory(); SearchFactory.Initialize(cfg, sf); } } }
|
Ну и небольшой скрипт для вставки данных в базу...
| Код | INSERT INTO TempBook (TempBookID, Author, Name, Summary) VALUES ( 1, 'Eric Evans', 'Domain-Driven Design: Tackling Complexity in the Heart of Software', 'This book provides a broad framework for making design decisions and a technical vocabulary for discussing domain design. It is a synthesis of widely accepted best practices along with the authors own insights and experiences') INSERT INTO TempBook (TempBookID, Author, Name, Summary) VALUES ( 2, 'John Doe', 'Foo book NHibernate', 'Foo series book')
|
И какие танцы с бубном не делай всегда возвращается нуль записей!!! Есть какие нибудь идеи? |