Using solr in. net to improve search efficiency (getting started). netsolr
Overview:
When developing a website, you often need to query certain content. In this case, if the search function is based on the database query, the efficiency is often very poor because Fuzzy Matching is required for multiple fields. In this case, Solr can be used to improve the search efficiency. Solr is an independent enterprise-level search application server that provides APIs similar to Web-service. This section describes the Solr environment and configuration, and then describes how to use the SolrNet client to operate the Solr server.
1. Install Solr
Software Download: Solr 5.0, JDK 7 or above
Install jdk .. Solr can be directly decompressed and started using the command line.
Run the following command to start the solr service:
Enter the following address in the browser: http: // localhost: 8983/solr /. The following page indicates that the solr service has started properly.
Solr is developed based on java and therefore requires the jdk environment. The downloaded Solr package contains the jetty Web server. Here we use the built-in.
Ii. Solr Configuration
New core:
In the web interface opened above, you can perform operations. Click the Core Admin menu to go to the Core Management page.
Click <Add Core> to create a new core. A prompt is displayed, indicating that the configuration file is required.
So we create a folder named test under the path \ solr-5.0.0 \ server \ solr. Go to the solr-5.0.0 \ server \ solr \ configsets \ basic_configs folder and copy the Conf folder to the test folder you just created. In this way, the required configuration file is provided, and the configuration of the configuration file will be introduced later.
Click Add Core to create the Core.
Configure Schema. xml of Solr:
Generally, solr can be used as long as the Schema file is configured. The Schema file is used to describe the data structure of Solrs indexes.
FieldType: describes the data types supported by solr.
UniqueKey: primary key field marking solr
Field: field marking the solr Index
DynamicField: an index field that supports wildcard characters. A field that cannot be found matches this field.
Generally, you need to add a field node. The field corresponds to the field you want to query. Added the title field.
Then set the primary key in uniqueKey. Set the id field as the primary key.
Iii. index construction by SolrNet
Download solrnet.
Suggestion: http://download.csdn.net/download/tp4479/4666325. Download and compile it yourself. Although it can be downloaded from Nuget, this version seems to have a bug.
Use solr to build an index:
The following code adds an index to the solr Server. Batch index addition is similar.
1 Startup.Init<SolrDocument>("http://localhost:8983/solr/test");2 var solr = ServiceLocator.Current.GetInstance<ISolrOperations<SolrDocument>>();3 var doc = new SolrDocument() { Id="key1",Title="tt1"};4 solr.Add(doc);5 solr.Commit();
Iv. SolrNet Query
Use solr to query data:
1 var solr = ServiceLocator. current. getInstance <ISolrOperations <SolrDocument> (); 2 QueryOptions options = new QueryOptions (); 3 options. rows = 10; // number of results retrieved 4 options. start = 0; // the Start position of the result, used for paging 5 IList <ISolrQuery> qlist = new List <ISolrQuery> (); 6 var qfied = new SolrQueryByField ("Title ", "tt1"); 7 var qkey = new SolrQueryByField ("Id", "key1"); 8 qlist. add (qfied); 9 qlist. add (qkey); 10 var qs = new SolrMultipleCriteriaQuery (qlist, "or"); // relationship between Query conditions 11 var res2 = solr. query (qs, options );
Fuzzy query: Implemented with wildcards
Var res1 = solr. Query (new SolrQuery ("Title: * a1 *"), options );
Summary
This section briefly introduces how to use the. net client SolrNet to operate Solr. We hope that our friends who have never been familiar with solr can have a general concept of solr applications.