IndexeurSolr.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using CD67.ModeleMVC.Entity;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Xml;
  8. namespace CD67.ModeleMVC.Solr
  9. {
  10. public class IndexeurSolr
  11. {
  12. public static string SolrUrl = "http://t-lunr4:8080/solr/modele-mvc";
  13. /// <summary>
  14. /// Ajoute le viking à l'index solr
  15. /// </summary>
  16. /// <param name="viking"> Un objet Viking </param>
  17. /// <param name="version"> Un GUID utilisé pour identifier la version des données indéxées </param>
  18. /// <param name="autocommit"> Si autocommit est vrai, les modifications apportées à l'index seront commitées automatiquement </param>
  19. public static void addViking(Viking viking, string version = "", bool autocommit = true)
  20. {
  21. // Si aucun GUID version n'a été donné en paramètre, en génère un pour cet ajout.
  22. if (String.IsNullOrEmpty(version))
  23. {
  24. version = Guid.NewGuid().ToString();
  25. }
  26. // Génère le document XML qui servira à transmettre les données à SolR
  27. XmlDocument toSolr = new XmlDocument();
  28. toSolr.AppendChild(toSolr.CreateElement("add"));
  29. XmlNode node = toSolr.CreateElement("doc");
  30. // Ajoute les noeuds au document XML.
  31. // >> Cette partie est à modifier selon le type d'objet indéxé (ici: Viking)
  32. // Id unique Solr
  33. addFieldToNode(ref node, "id", viking.Id.ToString());
  34. // Version de la donnée
  35. addFieldToNode(ref node, "version", version);
  36. // Donnees
  37. addFieldToNode(ref node, "nom", viking.Nom);
  38. addFieldToNode(ref node, "description", viking.Description);
  39. // Facettes
  40. addFieldToNode(ref node, "type_libelle", viking.TypeViking.Libelle);
  41. addFieldToNode(ref node, "casque_cornu", (viking.CasqueCornu ? "Oui" : "Non"));
  42. addFieldToNode(ref node, "nb_victoires", (viking.NombreVictoires == null) ? "0" : viking.NombreVictoires.ToString());
  43. addFieldToNode(ref node, "annee_creation", viking.DateCreation.Year.ToString());
  44. // Tri
  45. addFieldToNode(ref node, "date_edition", String.Format("{0:yyyyMMddhhmmss}", viking.DateEdition));
  46. // contruit le champ recherche par concaténation
  47. List<string> recherche_strings = new List<string>() { viking.Nom, viking.TypeViking.Libelle, viking.Description };
  48. string recherche = String.Join(" ", recherche_strings);
  49. addFieldToNode(ref node, "recherche", recherche.ToLower());
  50. // Met à jour le document XML
  51. toSolr.DocumentElement.AppendChild(node);
  52. // Envoie la requete de mise à jour au serveur
  53. SolrTools.SolrEngine.SolrUpdate(SolrUrl, toSolr.OuterXml);
  54. // Commit les modifications si besoin
  55. if (autocommit)
  56. {
  57. commit();
  58. }
  59. }
  60. /// <summary>
  61. /// Commit les ajouts / suppressions
  62. /// </summary>
  63. /// <param name="marche"></param>
  64. public static void commit()
  65. {
  66. SolrTools.SolrEngine.SolrUpdate(SolrUrl, "<commit/>");
  67. }
  68. /// <summary>
  69. /// Optimize les indexes
  70. /// (à lancer en fin de traitement)
  71. /// </summary>
  72. /// <param name="marche"></param>
  73. public static void optimize()
  74. {
  75. SolrTools.SolrEngine.SolrUpdate(SolrUrl, "<optimize/>");
  76. }
  77. /// <summary>
  78. /// Supprime l'item de l'index
  79. /// </summary>
  80. /// <param name="id de l'item"></param>
  81. public static void delete(string id)
  82. {
  83. SolrTools.SolrEngine.SolrUpdate(SolrUrl, "<delete><query>id:" + id + "</query></delete>");
  84. }
  85. /// <summary>
  86. /// Ajoute un noeud 'champ solr: valeur' au noeud en cours du document XML
  87. /// </summary>
  88. /// <param name="doc"></param>
  89. /// <param name="name"></param>
  90. /// <param name="value"></param>
  91. /// <param name="updateAttribute"></param>
  92. private static void addFieldToNode(ref XmlNode doc, string name, string value, updateAttributeValues updateAttribute = updateAttributeValues.none)
  93. {
  94. XmlNode node = doc.AppendChild(doc.OwnerDocument.CreateElement("field")); // description d'un champs
  95. node.Attributes.Append(doc.OwnerDocument.CreateAttribute("name")).Value = name;
  96. if (updateAttribute != updateAttributeValues.none) node.Attributes.Append(doc.OwnerDocument.CreateAttribute("update")).Value = updateAttribute.ToString();
  97. node.InnerText = value;
  98. }
  99. enum updateAttributeValues
  100. {
  101. none,
  102. set,
  103. add
  104. }
  105. /// <summary>
  106. /// Send a query string to Solr
  107. /// </summary>
  108. /// <param name="query"></param>
  109. /// <returns></returns>
  110. public static XmlNode request(string query)
  111. {
  112. return SolrTools.SolrEngine.SolrRequest(SolrUrl, query);
  113. }
  114. }
  115. }