| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- using CD67.ModeleMVC.Entity;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml;
- namespace CD67.ModeleMVC.Solr
- {
- public class IndexeurSolr
- {
- public static string SolrUrl = "http://t-lunr4:8080/solr/modele-mvc";
- /// <summary>
- /// Ajoute le viking à l'index solr
- /// </summary>
- /// <param name="viking"> Un objet Viking </param>
- /// <param name="version"> Un GUID utilisé pour identifier la version des données indéxées </param>
- /// <param name="autocommit"> Si autocommit est vrai, les modifications apportées à l'index seront commitées automatiquement </param>
- public static void addViking(Viking viking, string version = "", bool autocommit = true)
- {
- // Si aucun GUID version n'a été donné en paramètre, en génère un pour cet ajout.
- if (String.IsNullOrEmpty(version))
- {
- version = Guid.NewGuid().ToString();
- }
- // Génère le document XML qui servira à transmettre les données à SolR
- XmlDocument toSolr = new XmlDocument();
- toSolr.AppendChild(toSolr.CreateElement("add"));
- XmlNode node = toSolr.CreateElement("doc");
- // Ajoute les noeuds au document XML.
- // >> Cette partie est à modifier selon le type d'objet indéxé (ici: Viking)
- // Id unique Solr
- addFieldToNode(ref node, "id", viking.Id.ToString());
- // Version de la donnée
- addFieldToNode(ref node, "version", version);
- // Donnees
- addFieldToNode(ref node, "nom", viking.Nom);
- addFieldToNode(ref node, "description", viking.Description);
- // Facettes
- addFieldToNode(ref node, "type_libelle", viking.TypeViking.Libelle);
- addFieldToNode(ref node, "casque_cornu", (viking.CasqueCornu ? "Oui" : "Non"));
- addFieldToNode(ref node, "nb_victoires", (viking.NombreVictoires == null) ? "0" : viking.NombreVictoires.ToString());
- addFieldToNode(ref node, "annee_creation", viking.DateCreation.Year.ToString());
- // Tri
- addFieldToNode(ref node, "date_edition", String.Format("{0:yyyyMMddhhmmss}", viking.DateEdition));
- // contruit le champ recherche par concaténation
- List<string> recherche_strings = new List<string>() { viking.Nom, viking.TypeViking.Libelle, viking.Description };
- string recherche = String.Join(" ", recherche_strings);
- addFieldToNode(ref node, "recherche", recherche.ToLower());
- // Met à jour le document XML
- toSolr.DocumentElement.AppendChild(node);
-
- // Envoie la requete de mise à jour au serveur
- SolrTools.SolrEngine.SolrUpdate(SolrUrl, toSolr.OuterXml);
- // Commit les modifications si besoin
- if (autocommit)
- {
- commit();
- }
- }
- /// <summary>
- /// Commit les ajouts / suppressions
- /// </summary>
- /// <param name="marche"></param>
- public static void commit()
- {
- SolrTools.SolrEngine.SolrUpdate(SolrUrl, "<commit/>");
- }
- /// <summary>
- /// Optimize les indexes
- /// (à lancer en fin de traitement)
- /// </summary>
- /// <param name="marche"></param>
- public static void optimize()
- {
- SolrTools.SolrEngine.SolrUpdate(SolrUrl, "<optimize/>");
- }
- /// <summary>
- /// Supprime l'item de l'index
- /// </summary>
- /// <param name="id de l'item"></param>
- public static void delete(string id)
- {
- SolrTools.SolrEngine.SolrUpdate(SolrUrl, "<delete><query>id:" + id + "</query></delete>");
- }
- /// <summary>
- /// Ajoute un noeud 'champ solr: valeur' au noeud en cours du document XML
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="name"></param>
- /// <param name="value"></param>
- /// <param name="updateAttribute"></param>
- private static void addFieldToNode(ref XmlNode doc, string name, string value, updateAttributeValues updateAttribute = updateAttributeValues.none)
- {
- XmlNode node = doc.AppendChild(doc.OwnerDocument.CreateElement("field")); // description d'un champs
- node.Attributes.Append(doc.OwnerDocument.CreateAttribute("name")).Value = name;
- if (updateAttribute != updateAttributeValues.none) node.Attributes.Append(doc.OwnerDocument.CreateAttribute("update")).Value = updateAttribute.ToString();
- node.InnerText = value;
- }
- enum updateAttributeValues
- {
- none,
- set,
- add
- }
-
- /// <summary>
- /// Send a query string to Solr
- /// </summary>
- /// <param name="query"></param>
- /// <returns></returns>
- public static XmlNode request(string query)
- {
- return SolrTools.SolrEngine.SolrRequest(SolrUrl, query);
- }
- }
- }
|