Solr learning Summary (5) basic usage of SolrNet and CURD, solrsolrnet
In the previous article, we talked about the related Solr query parameters. Here we will talk about how C # requests and accepts data from the solr server through the client. SolrNet is recommended here, mainly because SolrNet is very convenient to use and has a large number of users who have been updating it all the time, if you are interested, you can join their contact groups to quickly learn about the latest developments in SolrNet.
SorlNet Source Address: https://github.com/mausch/SolrNet
SolrNet instructions: https://github.com/mausch/SolrNet/tree/master/Documentation
1. Create a project console program and reference SolrNet. dll.Download Demo
Note: SolrNet depends on the HttpWebAdapters. dll and Microsoft. Practices. ServiceLocation. dll files. Therefore, if there is a problem with compilation or testing, it should be OK to reference these two dll files.
2. Add related Filed fields in schema. xml of solr and create an object class to map the fields with Filed fields in schema. xml.
public class Product { [SolrUniqueKey("id")] public int id { get; set; } [SolrField("name")] public string name { get; set; } [SolrField("title")] public string title { get; set; } [SolrField("category")] public string category { get; set; } [SolrField("content")] public string content { get; set; } [SolrField("price")] public double price { get; set; } [SolrField("color")] public string color { get; set; } [SolrField("updatetime")] public DateTime updatetime { get; set; } [SolrField("orderBy")] public int orderBy { get; set; } }
At the same time, schema. add the corresponding Filed field in xml to open the schema under solr_home \ mycore1 \ conf. add the following Field configuration in the xml file. If you do not know how to operate it, refer to the previous article, Solr learning Summary (2) installation and configuration of Solr.
<field name="id" type="int" indexed="true" stored="true" required="true" multiValued="false" /> <field name="name" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="title" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="category" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="content" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="price" type="double" indexed="true" stored="true" required="true" multiValued="false" /> <field name="color" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="orderBy" type="int" indexed="true" stored="true" required="true" multiValued="false" /> <field name="updatetime" type="date" indexed="true" stored="true" required="true" multiValued="false" />
Iii. Start to call solrnet:
1. Initialization
Startup.Init<Product>("http://localhost:8080/solr/mycore1");
2. Add and modify an index (document)
The addition and modification of Solr indexes are both the Add () method. solr will automatically determine whether or not this exists. Therefore, if yes, it will be modified. If not, it will be added.
ISolrOperations<Product> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>(); var p = new Product() { id = 201, name = "product 201", title = "title 201", category = "201", content = "title 201 green", color = "green", price = 67.92, updatetime = DateTime.Now.AddDays(-101), orderBy = 101 }; solr.Add(p); solr.Commit();
3. delete an index
Solrnet overrides multiple delete () methods. Here is only one example. Let's study it by yourself.
ISolrOperations<Product> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>(); var p = new Product() { id = 201, }; solr.Delete(p); solr.Commit();
Note: CallAdd()
OrDelete()
Method, must be added after themCommit()
No, the request will not be processed.
4. Query
ISolrOperations<Product> solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>(); SolrQueryResults<Product> phoneTaggedArticles = solr.Query(new SolrQuery("id:1")); foreach (Product p in phoneTaggedArticles) { Console.WriteLine(string.Format("{0}: {1}", p.id, p.title)); } Console.WriteLine();
At this point, the basic usage of Solrnet has been completed. In the next article, we will talk about some advanced usage of Solr, complex queries of solr, highlighting, and Facet grouping queries.