Hello there,
I been struggling with NH.Search for almost two days. My problem is that the following code returns nothing although the table used contains many records. I'm running out of ideas here so any help would be much appreciated.
Main method:
Code:
public class Program
{
private static Configuration cfg;
private static ISessionFactory sf;
private static ISessionFactory sessionFactory;
private static void Main()
{
#region Init
cfg = new Configuration();
cfg.SetProperty("connection.driver_class", "NHibernate.Driver.SqlClientDriver");
cfg.SetProperty("hibernate.search.default.directory_provider", "NHibernate.Search.Storage.FSDirectoryProvider, NHibernate.Search");
cfg.SetProperty("hibernate.search.default.indexBase", @"~\Index");
cfg.SetProperty("hibernate.search.analyzer", "Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net");
cfg.SetProperty("cache.use_query_cache", "false");
cfg.SetProperty("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
cfg.SetProperty("dialect", "NHibernate.Dialect.MsSql2005Dialect");
cfg.SetProperty("connection.connection_string", @"server=PC-VIL8-NEW\LOCALTEST;initial catalog=OrbitorHR;user id=sa;password=Zip8920n;Connection Timeout=40;");
cfg.SetProperty("show_sql", "true");
cfg.Configure();
sessionFactory = cfg.BuildSessionFactory();
SearchFactory.Initialize(cfg, sessionFactory);
ISession session = sessionFactory.OpenSession(new SearchInterceptor());
IFullTextSession fullTextSession = Search.CreateFullTextSession(session);
#endregion
string query = "admin";
Analyzer analyzer = new SimpleAnalyzer();
MultiFieldQueryParser parser = new MultiFieldQueryParser(
new string[] { "UserName", "LoweredUserName" },
analyzer);
Query queryObj = null;
try
{
queryObj = parser.Parse(query);
}
catch (ParseException) { }
IFullTextSession session2 = fullTextSession;
IQuery nhQuery = session2.CreateFullTextQuery(queryObj, new Type[] { typeof(User) });
IList<User> users = nhQuery.List<User>();
Console.ReadKey(true);
}
}
User.cs
Code:
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate.Search;
using NHibernate.Search.Attributes;
using NHibernate.Search.Bridge.Builtin;
using Lucene.Net.Analysis.Standard;
namespace NHibernateSearch_Demo.Model
{
[Indexed(Index = "User")]
public class User
{
public User()
{
}
private Guid? userID;
[DocumentId(Name = "UserID")]
[FieldBridge(typeof(GuidBridge))]
public virtual Guid? UserID
{
get { return userID; }
set { userID = value; }
}
private Guid? applicationID;
public virtual Guid? ApplicationID
{
get { return applicationID; }
set { applicationID = value; }
}
private string userName;
[Field(Index.Tokenized, Store = Store.No)]
[Analyzer(typeof(StandardAnalyzer))]
public virtual string UserName
{
get { return userName; }
set { userName = value; }
}
private string loweredUserName;
[Field(Index.Tokenized, Store = Store.No)]
[Analyzer(typeof(StandardAnalyzer))]
public virtual string LoweredUserName
{
get { return loweredUserName; }
set { loweredUserName = value; }
}
private string mobileAlias;
public virtual string MobileAlias
{
get { return mobileAlias; }
set { mobileAlias = value; }
}
private bool isAnonymous;
public virtual bool IsAnonymous
{
get { return isAnonymous; }
set { isAnonymous = value; }
}
private DateTime? lastActivityDate;
public virtual DateTime? LastActivityDate
{
get { return lastActivityDate; }
set { lastActivityDate = value; }
}
}
}
mappings.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernateSearch-Demo"
namespace="NHibernateSearch_Demo.Model">
<class name="User" table="aspnet_Users">
<id name="UserID" type="guid">
<generator class="guid"/>
</id>
<property name="ApplicationID"/>
<property name="UserName" length="256" not-null="true"/>
<property name="LoweredUserName" length="256" not-null="true"/>
<property name="MobileAlias" length="256" not-null="true"/>
<property name="IsAnonymous" not-null="true"/>
<property name="LastActivityDate" not-null="true"/>
</class>
</hibernate-mapping>
GuidBridge.cs
Code:
namespace NHibernate.Search.Bridge.Builtin
{
public class GuidBridge : SimpleBridge
{
public override object StringToObject(string stringValue)
{
return new System.Guid(stringValue);
}
}
}
The code does not throw any exceptions. nhQuery.List<User>() returns empty list. Thanks in advance
Best Regards,
Dave