Hi,
I have a list of “items” that i would like to search in and retrieve documents by name. so far, i have created an index already and the search seems to work… kind of. The rows get returned but all of the row values are null. FullTextQueryRow works different than an Resultset. I must be missing something there but i don’t really know what im doing wrong. The index is there and the query runs but yet the documents it returns are empty.
Also, is there a way to check if that index has already been created? Right now i just create it everytime i run a search and i think this is not the way i should be doing it.
Here is my code.
public static List<Search> SearchItems(string searchterm)
{
List<Search> searches = new List<Search>();
if (String.IsNullOrEmpty(searchterm))
{
searches = LoadAllListItems();
}
else
{
bool startswithletter = !String.IsNullOrEmpty(searchterm) && Char.IsLetter(searchterm[0]);
if (startswithletter)
{
using (var searchesDatabase = new Database("search"))
{
searchesDatabase.CreateIndex(new[] { "key", "itemnamenl" }, IndexType.FullTextIndex, null);
var query = Query.Select(SelectResult.Expression(Expression.Property("itemnamenl")),
SelectResult.Expression(Expression.Property("key")),
SelectResult.Expression(Expression.Property("itemid"))
)
.From(DataSource.Database(searchesDatabase))
.Where(Expression.Property("itemnamenl").Match(searchterm+ "*"))
.OrderBy(Ordering.Property("itemnamenl"));
try
{
var rows = query.Run();
if (rows != null)
{
foreach (var row in rows) //row properties always are null!
{
var document = row;
searches.Add(
new Search
{
key = document.GetString(key: "key"),
itemnamenl = document.GetString(key: "itemnamenl"),
itemid = document.GetString(key: "itemid")
});
}
}
}
catch (Exception e)
{
var ex = e;
}
}
}
else
{
using (var searchesDatabase = new Database("search"))
{
searches = new List<Search>();
searchesDatabase.CreateIndex(new[] { "key", "itemnamenl" }, IndexType.FullTextIndex, null);
var query = Query.Select(SelectResult.Expression(Expression.Property("key")),
SelectResult.Expression(Expression.Property("itemid")),
SelectResult.Expression(Expression.Property("itemnamenl"))
)
.From(DataSource.Database(searchesDatabase))
.Where(Expression.Property("key").Match(zoekterm))
.OrderBy(Ordering.Property("itemnamenl"));
try
{
var rows = query.Run();
if (rows != null)
{
foreach (var row in rows)
{
var document = row;
searches.Add(
new Search
{
key = document.GetString(key: "key"),
itemnamenl = document.GetString(key: "itemnamenl"),
itemid = document.GetString(key: "itemid")
});
}
}
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
}
}
}
return searches;
}