NEST how to setup completion suggestion context using annotation [duplicate] - elasticsearch

This question already has an answer here:
ElasticSearch 5.x Context Suggester NEST .Net
(1 answer)
Closed 4 years ago.
I"m trying to setup context for my completion suggestion attribute on my POJO class however, most of the documentation online are based on configuration setting. Any ideas how I can achieve this?
[Completion]
public CompletionField Suggest { get; set; }

For indexing:
[Completion(Name = "FieldNameSuggest")]
public CompletionField Suggest
{
get
{
List<string> data = new List<string>();
data.AddRange(new List<string>() {"word1",word2"word3",... });//for suggest
return new CompletionField()
{
Input = data,
Weight = 1
};
}
}
For get suggest:
Query Making:
var sugContainer = new SuggestContainer
{
{ "completion-suggest", new SuggestBucket
{
Prefix ="word",//text for search
Completion = new CompletionSuggester
{
//Fuzzy = new FuzzySuggester
//{
// Fuzziness = Fuzziness.Auto,
// MinLength = 1,
// PrefixLength = 2,
// Transpositions = true,
// UnicodeAware = false
//},
Analyzer = "simple",
Field =new Field("FieldNameSuggest"),
Size =10, //SuggestionCount
SkipDuplicates=true,
}
}
}
};
Finaly for search:
var Result = Client.Search<T>(new SearchRequest<T>
{
Suggest = sugContainer ,
...
});

Related

Multiple sorting on single index of Algolia

Can we put multiple sorting/custom ranking on single index of Algolia without creating multiple replica indexes for each sorting.
Lets understand with example suppose my application is SalesManagement and I create 3 replicas MobileSale,LaptopSale and PhoneSale and then in each one I put custom sorting to type = 'mobile',type = 'laptop' and type = 'phone' respectively.
Instead of doing above I just want to create one index and put sorting on compile time by sending different settings by using IndexSetting in my code so I don't need to create multiple indexes
Using ranking property of IndexSetting it worked for me
private readonly SearchClient algoliaClient;
private readonly SearchIndex feedIndex;
public AlgoliaService( )
{
algoliaClient = new SearchClient("app-id", "api-key");
SearchIndex = algoliaClient.InitIndex(_defaultSettings.Algolia.FeedIndex);
algoliaClient.InitIndex("user-index-name");
}
public async Task SetCustomSortingAttributes(string sortBy)
{
if (sortBy != null)
{
List<string> sortingAttributesList = new List<string>();
StringBuilder sortAttribute = new StringBuilder();
if (sortBy == "most_viewed")
{
sortAttribute.Append("desc(most_viewed)");
}
else if (sortBy == "entityUniqueViews" )
{
sortAttribute.Append("desc(entityUniqueViews)");
}
else if (sortBy == "latest")
{
sortAttribute.Append("desc(latest)");
}
else if (sortBy == "price_asc")
{
sortAttribute.Append("asc(price)");
}
else
{
sortAttribute.Append("desc(price)");
}
sortingAttributesList.Add(sortAttribute.ToString());
IndexSettings settings = new IndexSettings
{
Ranking = sortingAttributesList
};
var setIndexSetting = await feedIndex.SetSettingsAsync(settings);
}
}

Specify default analyzer in NEST or Elasticsearch

How can I specify default analyzer in NEST? Or alternative in Elasticsearch? I want change standard analyzer to language analyzer!
If you are using automap in nest you can use an attribute like so
public class A
{
[Text(Analyzer = "NameOfTheAnalyzer")]
public string Prop1 { get; set; }
}
If you want the default mapping you can set it like so
var request = new CreateIndexRequest(indexName)
{
Mappings = new Mappings()
{
["_default_"] = new TypeMapping()
{
Properties = new Properties
{
["id"] = new KeywordProperty { Index = false },
["title"] = new TextProperty { Analyzer = "NameOfTheAnalyzer" }
}
}
}
};
var create = client.CreateIndex(request);

Error using Object Initializer syntax to create MultiMatchQuery

I'm using Nest 2.2.0 and am trying to build a multimatch query as follows:
var searchQuery = new MultiMatchQuery()
{
Fields = Field<Product>(p=>p.SKUName, 2),
Query = "hello world"
};
When I run it however, it returns:
The non-generic type 'Nest.Field' cannot be used with type arguments.
I don't understand why I'm getting the error, since I've more or less taken this query straight from the documentation found at https://www.elastic.co/guide/en/elasticsearch/client/net-api/2.x/multi-match-usage.html#_object_initializer_syntax_example_35.
In case it matters, I've defined the Product as follows:
[ElasticsearchType(Name="product", IdProperty="Id")]
public class Product
{
[Nest.Number(Store = true)]
public int Id {get;set;}
[String(Name="name", Store = true, Index=FieldIndexOption.Analyzed)]
public string SKUName { get; set; }
}
Is anyone able to help?
The Field type you're looking for is Nest.Infer.Field
var searchQuery = new MultiMatchQuery()
{
Fields = Nest.Infer.Field<Product>(p => p.SKUName, 2),
Query = "hello world"
};
client.Search<Product>(new SearchRequest { Query = searchQuery });

Does Nest.ConnectionSettings.SetJsonSerializerSettingsModifier even work?

Here is my question. Due to project needs, we have to keep our dates within elasticsearch index in the same format. What we've tried is the next way -
var connectionPool = new SniffingConnectionPool(nodeList);
var connectionSettings = new ConnectionSettings(connectionPool)
.SetJsonSerializerSettingsModifier(
m => m.DateFormatString = "yyyy-MM-ddTHH:mm:ss.fffffffK")
// other configuration goes here
But it didn't work out. Searching through ES index, I saw dates with dropped trailing zeros ( like 2015-05-05T18:55:27Z insted of expected 2015-05-05T18:55:27.0000000Z). Neither did next option help:
var connectionPool = new SniffingConnectionPool(nodeList);
var connectionSettings = new ConnectionSettings(connectionPool)
.SetJsonSerializerSettingsModifier(m =>
{
m.Converters.Add(new IsoDateTimeConverter { DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK"});
})
// other configuration goes here
With digging into ElasticClient at run-time, I've found that eventually there is a contract resolver which seems like overrides all those settings:
public class ElasticContractResolver : DefaultContractResolver
{
protected override JsonContract CreateContract(Type objectType)
{
JsonContract contract = base.CreateContract(objectType);
...
if (objectType == typeof(DateTime) || objectType == typeof(DateTime?))
contract.Converter = new IsoDateTimeConverter();
...
if (this.ConnectionSettings.ContractConverters.HasAny())
{
foreach (var c in this.ConnectionSettings.ContractConverters)
{
var converter = c(objectType);
if (converter == null)
continue;
contract.Converter = converter;
break;
}
}
return contract;
}
}
So if I have it right, without specifying a converter explicitly(via Connection Settings.AddContractJsonConverters()), my json settings will be gone since IsoDateTimeConverter is instantiated with the default settings rather than ones I've passed through SetJsonSerializerSettingsModifier.
Has anyone run into this issue? Or I'm just missing something? Thanks in advance!
This is how I handled custom date format for my needs:
public class Document
{
[ElasticProperty(DateFormat = "yyyy-MM-dd", Type = FieldType.Date)]
public string CreatedDate { get; set; }
}
client.Index(new Document {CreatedDate = DateTime.Now.ToString("yyyy-MM-dd")});
My document in ES
{
"_index": "indexname",
"_type": "document",
"_id": "AU04kd4jnBKFIw7rP3gX",
"_score": 1,
"_source": {
"createdDate": "2015-05-09"
}
}
Hope it will help you.

Entity Framework, Code First and Full Text Search

I realize that a lot of questions have been asked relating to full text search and Entity Framework, but I hope this question is a bit different.
I am using Entity Framework, Code First and need to do a full text search. When I need to perform the full text search, I will typically have other criteria/restrictions as well - like skip the first 500 rows, or filter on another column, etc.
I see that this has been handled using table valued functions - see http://sqlblogcasts.com/blogs/simons/archive/2008/12/18/LINQ-to-SQL---Enabling-Fulltext-searching.aspx. And this seems like the right idea.
Unfortunately, table valued functions are not supported until Entity Framework 5.0 (and even then, I believe, they are not supported for Code First).
My real question is what are the suggestions for the best way to handle this, both for Entity Framework 4.3 and Entity Framework 5.0. But to be specific:
Other than dynamic SQL (via System.Data.Entity.DbSet.SqlQuery, for example), are there any options available for Entity Framework 4.3?
If I upgrade to Entity Framework 5.0, is there a way I can use table valued functions with code first?
Thanks,
Eric
Using interceptors introduced in EF6, you could mark the full text search in linq and then replace it in dbcommand as described in http://www.entityframework.info/Home/FullTextSearch:
public class FtsInterceptor : IDbCommandInterceptor
{
private const string FullTextPrefix = "-FTSPREFIX-";
public static string Fts(string search)
{
return string.Format("({0}{1})", FullTextPrefix, search);
}
public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
}
public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
}
public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
RewriteFullTextQuery(command);
}
public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
}
public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
RewriteFullTextQuery(command);
}
public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
}
public static void RewriteFullTextQuery(DbCommand cmd)
{
string text = cmd.CommandText;
for (int i = 0; i < cmd.Parameters.Count; i++)
{
DbParameter parameter = cmd.Parameters[i];
if (parameter.DbType.In(DbType.String, DbType.AnsiString, DbType.StringFixedLength, DbType.AnsiStringFixedLength))
{
if (parameter.Value == DBNull.Value)
continue;
var value = (string)parameter.Value;
if (value.IndexOf(FullTextPrefix) >= 0)
{
parameter.Size = 4096;
parameter.DbType = DbType.AnsiStringFixedLength;
value = value.Replace(FullTextPrefix, ""); // remove prefix we added n linq query
value = value.Substring(1, value.Length - 2);
// remove %% escaping by linq translator from string.Contains to sql LIKE
parameter.Value = value;
cmd.CommandText = Regex.Replace(text,
string.Format(
#"\[(\w*)\].\[(\w*)\]\s*LIKE\s*#{0}\s?(?:ESCAPE N?'~')",
parameter.ParameterName),
string.Format(#"contains([$1].[$2], #{0})",
parameter.ParameterName));
if (text == cmd.CommandText)
throw new Exception("FTS was not replaced on: " + text);
text = cmd.CommandText;
}
}
}
}
}
static class LanguageExtensions
{
public static bool In<T>(this T source, params T[] list)
{
return (list as IList<T>).Contains(source);
}
}
For example, if you have class Note with FTS-indexed field NoteText:
public class Note
{
public int NoteId { get; set; }
public string NoteText { get; set; }
}
and EF map for it
public class NoteMap : EntityTypeConfiguration<Note>
{
public NoteMap()
{
// Primary Key
HasKey(t => t.NoteId);
}
}
and context for it:
public class MyContext : DbContext
{
static MyContext()
{
DbInterception.Add(new FtsInterceptor());
}
public MyContext(string nameOrConnectionString) : base(nameOrConnectionString)
{
}
public DbSet<Note> Notes { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new NoteMap());
}
}
you can have quite simple syntax to FTS query:
class Program
{
static void Main(string[] args)
{
var s = FtsInterceptor.Fts("john");
using (var db = new MyContext("CONNSTRING"))
{
var q = db.Notes.Where(n => n.NoteText.Contains(s));
var result = q.Take(10).ToList();
}
}
}
That will generate SQL like
exec sp_executesql N'SELECT TOP (10)
[Extent1].[NoteId] AS [NoteId],
[Extent1].[NoteText] AS [NoteText]
FROM [NS].[NOTES] AS [Extent1]
WHERE contains([Extent1].[NoteText], #p__linq__0)',N'#p__linq__0 char(4096)',#p__linq__0='(john)
Please notice that you should use local variable and cannot move FTS wrapper inside expression like
var q = db.Notes.Where(n => n.NoteText.Contains(FtsInterceptor.Fts("john")));
I have found that the easiest way to implement this is to setup and configure full-text-search in SQL Server and then use a stored procedure. Pass your arguments to SQL, allow the DB to do its job and return either a complex object or map the results to an entity. You don't necessarily have to have dynamic SQL, but it may be optimal. For example, if you need paging, you could pass in PageNumber and PageSize on every request without the need for dynamic SQL. However, if the number of arguments fluctuates per query, it will be the optimal solution.
As the other guys mentioned, I would say start using Lucene.NET
Lucene has a pretty high learning curve, but I found an wrapper for it called "SimpleLucene", that can be found on CodePlex
Let me quote a couple of codeblocks from the blog to show you how easy it is to use. I've just started to use it, but got the hang of it really fast.
First, get some entities from your repository, or in your case, use Entity Framework
public class Repository
{
public IList<Product> Products {
get {
return new List<Product> {
new Product { Id = 1, Name = "Football" },
new Product { Id = 2, Name = "Coffee Cup"},
new Product { Id = 3, Name = "Nike Trainers"},
new Product { Id = 4, Name = "Apple iPod Nano"},
new Product { Id = 5, Name = "Asus eeePC"},
};
}
}
}
The next thing you want to do is create an index-definition
public class ProductIndexDefinition : IIndexDefinition<Product> {
public Document Convert(Product p) {
var document = new Document();
document.Add(new Field("id", p.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
document.Add(new Field("name", p.Name, Field.Store.YES, Field.Index.ANALYZED));
return document;
}
public Term GetIndex(Product p) {
return new Term("id", p.Id.ToString());
}
}
and create an search index for it.
var writer = new DirectoryIndexWriter(
new DirectoryInfo(#"c:\index"), true);
var service = new IndexService();
service.IndexEntities(writer, Repository().Products, ProductIndexDefinition());
So, you now have an search-able index. The only remaining thing to do is.., searching! You can do pretty amazing things, but it can be as easy as this: (for greater examples see the blog or the documentation on codeplex)
var searcher = new DirectoryIndexSearcher(
new DirectoryInfo(#"c:\index"), true);
var query = new TermQuery(new Term("name", "Football"));
var searchService = new SearchService();
Func<Document, ProductSearchResult> converter = (doc) => {
return new ProductSearchResult {
Id = int.Parse(doc.GetValues("id")[0]),
Name = doc.GetValues("name")[0]
};
};
IList<Product> results = searchService.SearchIndex(searcher, query, converter);
The example here http://www.entityframework.info/Home/FullTextSearch is not complete solution. You will need to look into understand how the full text search works. Imagine you have a search field and the user types 2 words to hit search. The above code will throw an exception. You need to do pre-processing on the search phrase first to pass it to the query by using logical AND or OR.
for example your search phrase is "blah blah2" then you need to convert this into:
var searchTerm = #"\"blah\" AND/OR \"blah2\" ";
Complete solution would be:
value = Regex.Replace(value, #"\s+", " "); //replace multiplespaces
value = Regex.Replace(value, #"[^a-zA-Z0-9 -]", "").Trim();//remove non-alphanumeric characters and trim spaces
if (value.Any(Char.IsWhiteSpace))
{
value = PreProcessSearchKey(value);
}
public static string PreProcessSearchKey(string searchKey)
{
var splitedKeyWords = searchKey.Split(null); //split from whitespaces
// string[] addDoubleQuotes = new string[splitedKeyWords.Length];
for (int j = 0; j < splitedKeyWords.Length; j++)
{
splitedKeyWords[j] = $"\"{splitedKeyWords[j]}\"";
}
return string.Join(" AND ", splitedKeyWords);
}
this methods uses AND logic operator. You might pass that as an argument and use the method for both AND or OR operators.
You must escape none-alphanumeric characters otherwise it would throw exception when a user enters alpha numeric characters and you have no server site model level validation in place.
I recently had a similar requirement and ended up writing an IQueryable extension specifically for Microsoft full text index access, its available here IQueryableFreeTextExtensions

Resources