Full text search engine

Now mainstream websites and APPs all have the function of "in-site search": Youku, iQiyi, Lianjia, Ctrip...etc. If you use SQL to do site search,

For example: select * from t where Title like '%Journey to the West%' The disadvantage of this method is: easy full table scan, poor performance, and great pressure on the database


Now our kind of search engine server can solve our problem

Full-text search engine development kits include Lucene, Solr, Elastic Search, etc.

Among them, Lucene is the underlying development kit of the full-text search engine engine written in Java. It also has a .Net ported version, called Lucene.Net. It is difficult to develop based on Lucene. Therefore, someone developed a search engine server based on Lucene, Solr, Elastic Search, etc.


Now we are going to talk about the Elasticc Search search engine server

The Elasticc Search search engine server is referred to as ES, which is developed in Java, so you need to configure the Java runtime environment when running. These search engine servers are all using the "client SDK + server" method

Step 1: Elasticc Search Installation

1> Download and install the Java runtime environment JDK1.8 


2> Java environment variable configuration

Win7 system:  First, right-click [Computer] to enter [Properties], then select [Advanced System Settings] and click [Environment Variables] to enter [Advanced] to enter the environment variable editing interface.


Click New 

The variable name (N) is called JAVA_HOME

The variable value is the installation directory of your Java JDB1.8

Click OK, then the Java environment variable configuration is configured

Step 2: Download and install Elasticc Search

Go to the official website address to download Elasticc Search: https://www.elastic.co/cn/downloads/elasticsearch

I downloaded the latest version: 6.2.3 After downloading, unzip it, and I unzip it to the D drive (note that if the test of version 6.2.3 found that the data saving failed, I later replaced it with version 5.2.0, according to the success of saving)


Open cmd, cd to the bin directory under the D:\elasticsearch-6.2.3 folder, and execute elasticcsearch.bat


Enter to execute

[If Error occurred during initialization of VM is reported during execution

Could not reserve enough space for object heap error] Then we just need to go to the config folder under the D:\elasticsearch-6.2.3 folder to find the jvm.options file, and change the stack space occupied by the service to a smaller point: change the method As shown below


After execution, we can enter 127.0.0.1:9200 in the local browser to check whether the elasticsearch service is running successfully

[Note: Do not close this cmd window after execution, the service will stop after closing]

Seeing the following conditions indicates success. (If you open it with IE browser, a Json file may pop up for you to save. If the content of the Json file is as follows, it means that the service is running successfully)


Step 3: Use

The above servers are all set up, let's demonstrate how to connect this Elasticc Search server through code

There are many drivers in the SDK, such as: ElasticSearch.Net , NEST , PlainElastic.Net etc. can be used, here we use PlainElastic.Net

1> Install PlainElastic.Net through Nuget in the project


In the controller call:

using PlainElastic.Net;
using PlainElastic.Net.Serialization;
using System;
using System.Web.Mvc;

namespace ElasticSearch.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            Person p1 = new Person();
            p1.Id = 100698;
            p1.Age = 18;
            p1.Name = "Lin Daiyuere";
            p1.Describe = "ee Lin Daiyu and Jia Baoyu were young, they had common ideals and rebellious spirits and gradually developed into love. The myth of "Jianzhu and Tears" endowed Lin Daiyu with a charming poetic temperament and injected Baodai's love with a sense of The romantic element of fantasy, while setting a tragic tone.";
            try
            {
                ElasticConnection client = new ElasticConnection("127.0.0.1", 9200);
                var serializer = new JsonNetSerializer();
                //The first parameter is equivalent to "database", the second parameter is equivalent to "table", and the third parameter is equivalent to "primary key"
                IndexCommand cmd = new IndexCommand("yzk", "persons", p1.Id.ToString());
                //Put() The second parameter is the data to be inserted
                OperationResult result = client.Put(cmd, serializer.Serialize(p1));
                var indexResult = serializer.ToIndexResult(result.Result);
                if (indexResult.created)
                {
                    return Content("Created successfully");
                }
                else
                {
                    return Content("Not created" + indexResult.error);
                }
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
                throw ex;
            }

            //How do I know the insertion was successful? See the indexResult return value. If the id already exists, it will not be inserted. If you want to overwrite the update, you must delete it and then insert it.
        }
    }
    public class Person
    {
        public int Id { get; set; }
        public int Age { get; set; }
        public string Name { get; set; }
        public string Describe { get; set; }
    }
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324378545&siteId=291194637