public SimpleElasticSearchContext(ElasticSearchConnectionSettings settings) { ConnectionSettings config = new ConnectionSettings(new System.Uri(settings.ConnectionString)) .DefaultIndex("Id"); ElasticClient = new ElasticClient(config); }
public static void CreateIndex() { var connectionToEs = ElasticSearchConnectionSettings.connection(); if (connectionToEs.IndexExists(assetIndexName).Exists) { var response = connectionToEs.DeleteIndex(assetIndexName); } var createIndexResponse = connectionToEs.CreateIndex(assetIndexName, c => c.Map <AssetDocument>(m => m.AutoMap())); using (DB.DOAMEntities db = new MyApp.Infrastructure.DB.DOAMEntities()) { foreach (var asset in db.Assets) { var assetDocument = new AssetDocument() { AssetID = asset.AssetID, TypeAssetName = GetNameAssetParTypeId(asset.AssetID), Name = asset.Name, Url = asset.Url, ThumbnailUrl = asset.ThumbnailUrl, Description = asset.Description, Compteur = asset.Compteur }; var assetMetadata = asset.AssetMetaDatas.Select(t => t).Distinct(); foreach (var assets in assetMetadata) { assetDocument.AssetMetadatas.Add(new IndexDocuments.AssetMetaData { AssetMetaDataID = assets.AssetMetaDataID, AssetName = assets.MetaData.Title, Value = assets.Value, }); } var assetTag = asset.AssetTags.Select(t => t.Tag).Distinct(); foreach (var assetTags in assetTag) { assetDocument.Tags.Add(new IndexDocuments.Tag { TagID = assetTags.TagID, Label = assetTags.Label, }); } var indexResponse = connectionToEs.Index(assetDocument, c => c.Index(assetIndexName)); } } }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddAuthorization(); //ElasticSearch var elasticSearchConnectionSettings = new ElasticSearchConnectionSettings(); Configuration.GetSection("ElasticSearch").Bind(elasticSearchConnectionSettings); services.AddSingleton <IOptions <ElasticSearchConnectionSettings> >(new OptionsWrapper <ElasticSearchConnectionSettings>(elasticSearchConnectionSettings)); //https://www.elastic.co/guide/en/elasticsearch/client/net-api/master/lifetimes.html services.AddSingleton <IElasticSearchManager, ElasticSearchManager>(); services.AddScoped <IRuleExecutor, RuleExecutor>(); //emailsettings var emailSettings = new EmailSettings(); Configuration.GetSection("EmailSettings").Bind(emailSettings); services.AddSingleton <IOptions <EmailSettings> >(new OptionsWrapper <EmailSettings>(emailSettings)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { #region " MongoDB Connection Settings and Singletons " //MongoConnectionSettings settings = new MongoConnectionSettings(); //Configuration.Bind("MongoConnectionSettings", settings); //SimpleMongoContext<SimpleMessage> dataContext = new SimpleMongoContext<SimpleMessage>(settings); //services.AddSingleton<ISimpleMongoContext<SimpleMessage>>(dataContext); #endregion #region " SQL Server Connection Settings and Singletons " //SQLServerConnectionSettings settings = new SQLServerConnectionSettings(); //Configuration.Bind("SQLServerConnectionSettings", settings); //ISimpleMessageRepository repo = new SimpleMessageRepository(settings); //ISimpleSQLServerContext<SimpleMessage> dataContext = new SimpleSQLServerContext<SimpleMessage>(repo); //services.AddSingleton(repo); //services.AddSingleton(dataContext); #endregion #region " ElasticSearch Connection Settings and Singletons " ElasticSearchConnectionSettings settings = new ElasticSearchConnectionSettings(); Configuration.Bind("ElasticSearchConnectionSettings", settings); ISimpleElasticSearchContext <SimpleMessage> dataContext = new SimpleElasticSearchContext(settings); services.AddSingleton(dataContext); #endregion services.AddSingleton(new SimpleBotUser(dataContext)); services.AddMvc(); }
//[HttpPost] /// <summary> /// Méthode pour faire la recherche /// </summary> /// <param name="recherche"></param> /// <param name="categorie"></param> /// <param name="currentFilter"></param> /// <param name="page"></param> /// <returns>la liste des élément trouvées </returns> public ActionResult SearchAction(string recherche, string categorie, string motClé, int?page) { try { MyApp.Infrastructure.ElasticSearch.ElasticSearchServiceAgent.AssetSearchService.CreateIndex(); var connectionToEs = ElasticSearchConnectionSettings.connection(); var ListOfDataIndexed = new List <MyApp.Infrastructure.ElasticSearch.IndexDocuments.AssetDocument>(); int pageSize = 6; int pageNumber = (page ?? 1); //on test si les schamps de rechercher sont vides if (string.IsNullOrEmpty(recherche) && string.IsNullOrEmpty(categorie) && string.IsNullOrEmpty(motClé)) { return(View("DataNotFound")); } //au cas ou les deux champs ne sont pas null else if (!string.IsNullOrEmpty(recherche) && !string.IsNullOrEmpty(categorie)) { ViewBag.recherche = recherche; ViewBag.categorie = categorie; //on execute la connection pour élastic search var reponse = connectionToEs.Search <MyApp.Infrastructure.ElasticSearch.IndexDocuments.AssetDocument> (s => s.Index(MyApp.Infrastructure.ElasticSearch.ElasticSearchServiceAgent.AssetSearchService.assetIndexName) .Size(50) .Query(q => q.MultiMatch(m => m.Query(recherche).Fuzziness(Fuzziness.Auto)) //première recherche && q.Match(m => m.Field(f => f.TypeAssetName) //filtrage de la prmiere recherche .Query(categorie)) ) ); ListOfDataIndexed = (from hits in reponse.Hits select hits.Source).ToList(); if (!ListOfDataIndexed.Any()) { return(View("DataNotFound")); } else { return(View(ListOfDataIndexed.ToList().ToPagedList(pageNumber, pageSize))); } } //au cas ou on fais la recherche sur un seul champs avec le système de fuzinness auto //Generates an edit distance based on the length of the term. //Low and high distance arguments may be optionally provided AUTO:[low],[high] // par défaut it takes [3 , 6] else if (!string.IsNullOrEmpty(recherche)) { ViewBag.recherche = recherche; var reponse = connectionToEs.Search <MyApp.Infrastructure.ElasticSearch.IndexDocuments.AssetDocument> (s => s.Index(MyApp.Infrastructure.ElasticSearch.ElasticSearchServiceAgent.AssetSearchService.assetIndexName) .Size(50) .Query(q => q.MultiMatch(m => m .Query(recherche).Fuzziness(Fuzziness.Auto)) ) ); ListOfDataIndexed = (from hits in reponse.Hits select hits.Source).ToList(); if (!ListOfDataIndexed.Any()) { return(View("DataNotFound")); } else { return(View(ListOfDataIndexed.ToList().ToPagedList(pageNumber, pageSize))); } } //faire une recherche sur base de type asset (vidéo, audio, image ...) else if (!string.IsNullOrEmpty(categorie)) { ViewBag.categorie = categorie; var reponse = connectionToEs.Search <MyApp.Infrastructure.ElasticSearch.IndexDocuments.AssetDocument> (s => s.Index(MyApp.Infrastructure.ElasticSearch.ElasticSearchServiceAgent.AssetSearchService.assetIndexName) .Size(50) .Query(q => q .Match(m => m .Field(f => f.TypeAssetName) .Query(categorie)) ) ); ListOfDataIndexed = (from hits in reponse.Hits select hits.Source).ToList(); if (!ListOfDataIndexed.Any()) //on verifie si la listet est vide et on retourne: data non trouvé { return(View("DataNotFound")); } else { return(View(ListOfDataIndexed.ToList().ToPagedList(pageNumber, pageSize))); } } //faire un recherche par mot clé le système chercher le mot exact else if (!string.IsNullOrEmpty(motClé)) { ViewBag.motClé = motClé; var reponse = connectionToEs.Search <MyApp.Infrastructure.ElasticSearch.IndexDocuments.AssetDocument> (s => s.Index(MyApp.Infrastructure.ElasticSearch.ElasticSearchServiceAgent.AssetSearchService.assetIndexName) .Size(50) .Query(q => q.MultiMatch(m => m .Query(motClé).Fuzziness(Fuzziness.EditDistance(0))) ) ); ListOfDataIndexed = (from hits in reponse.Hits select hits.Source).ToList(); if (!ListOfDataIndexed.Any()) { return(View("DataNotFound")); } else { return(View(ListOfDataIndexed.ToList().ToPagedList(pageNumber, pageSize))); } } return(View(ListOfDataIndexed.ToList().ToPagedList(pageNumber, pageSize))); } catch (Exception ex) { return(View("SearchAction2", new HandleErrorInfo(ex, "Home", "Index"))); } }