Today i have a database with all users, now i want to copy the users to elasticsearch. When i search users in the db, i use 2 sql queries,
- SELECT * FROM Users WHERE Username LIKE 'An%' (for autocomplete)
- SELECT * FROM Users WHERE Username LIKE '%An%' (for free text search)
Now i want to do the same thing in elasticsearch. This is what i have:
public static string IndexName => "users";
public static List<UserInfo> FindUsersStartsWith(string name, int pageSize, int currentPage,
ref long unitItems)
{
var Result = Client.Search<UserInfo>(s => s
.Index(IndexName)
.Query(q => q
.Match(m => m
// specify the field
.Field(f => f.UserName)
.Query(name)
)
)
.Size(pageSize)
.From((currentPage - 1) * pageSize)
);
unitItems = Result.Total;
return Result.Documents.ToList();
}
public static List<UserInfo> FindUsersContains(string name, int pageSize, int currentPage,
ref long unitItems)
{
var Result = Client.Search<UserInfo>(s => s
.Index(IndexName)
.Query(q => q
.Match(m => m
// specify the field
.Field(f => f.UserName)
.Query(name)
)
)
.Size(pageSize)
.From((currentPage - 1) * pageSize)
);
unitItems = Result.Total;
return Result.Documents.ToList();
}
public static void SetUser(UserInfo ui)
{
if (!client.IndexExists(IndexName).Exists)
{
client.CreateIndex(IndexName);
}
Client.Update<UserInfo, UserInfo>
(DocumentPath<UserInfo>
.Id(ui.UserNr), descriptor => descriptor.Doc(ui).DocAsUpsert().Refresh()
);
}
[Serializable, ElasticsearchType(IdProperty = "UserNr")]
public class UserInfo
{
public string UserName { get; set; }
public string Name { get; set; }
public string Avatar { get; set; }
public int UserNr { get; set; }
}
Edit: My idea is to have 2 functions. One where i can search on users that starts with, "An" (upper- or lowercase doesn't matter) and returns users like "Andy", not "Ronan". The other function will return all users that contains "An" (upper- or lowercase doesn't matter).
An(case sensitive)? ShouldAnonly be found at the start of word boundaries e.g.Andy, or can it be found anywhere within a word e.g. Ronan? Please edit your question to include any such restrictions, as this may affect the approach to takeConnectionSettingsandElasticClientcan be singletons and live for the lifetime of your application; you can construct them at startup and reuse them and also check and create the index at startup too :)