简体   繁体   English

Elasticsearch中的索引编制

[英]Indexing in Elasticsearch

I am having problems indexing in elasticsearch. 我在Elasticsearch中索引编制时遇到问题。 I have a function which creates an index and another one that creates a type mapping for a json I will be indexing. 我有一个函数可以创建一个索引,另一个函数可以为将要建立索引的json创建类型映射。 Both of these work fine but when I try calling the function to index something, it gives me a WebException saying that the remote server returned an error: (503) Server Unavailable. 这两种方法都可以正常工作,但是当我尝试调用该函数对某些内容建立索引时,它给了我WebException异常,表明远程服务器返回了错误:(503)Server Unavailable。 Any help will be greatly appreciated! 任何帮助将不胜感激! Thanks! 谢谢!

This is my code: 这是我的代码:

public static void Main(string[] args)
    {
        string endpoint = "http://localhost:9200";
        string index = "logs";
        string type = "activity-log";

        createIndex(endpoint, index);
        createMapping(endpoint, index, type);
        indexEvent(endpoint, index, type);
    }

public static void createIndex(string endpoint, string index)
    {
        //Build request url
        WebRequest request = WebRequest.Create(string.Format("{0}/{1}/", endpoint, index));
        request.ContentType = "application/json";   //set content type of json
        request.Method = "PUT";    //use POST method when creating an index
        string json = "{\"settings\":{\"number_of_shards\":3,\"number_of_replicas\":2}}";
        byte[] byteData = Encoding.GetEncoding("UTF-8").GetBytes(json);
        string result = System.Convert.ToBase64String(byteData);
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteData, 0, byteData.Length);
        dataStream.Close();

        //create a web response
        WebResponse response = request.GetResponse();
        StreamReader sr = new System.IO.StreamReader(response.GetResponseStream());
        Console.WriteLine(sr.ReadToEnd().Trim());
        Console.ReadKey();
        response.Close();
    }

 public static void createMapping(string endpoint, string index, string type)
    {
        //Build request url
        WebRequest request = WebRequest.Create(string.Format("{0}/{1}/{2}/_mapping", endpoint, index, type));
        request.ContentType = "application/json";   //set content type of json
        request.Method = "PUT";    //use POST method when creating an index
        string json = "{\"activitylogevent\":{\"properties\":{\"id\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"parentId\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"event\":{\"type\":\"string\",\"index\":\"not_analyzed\"}}}}";
        byte[] byteData = Encoding.GetEncoding("UTF-8").GetBytes(json);
        string result = System.Convert.ToBase64String(byteData);
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteData, 0, byteData.Length);
        dataStream.Close();

        //create a web response
        WebResponse response = request.GetResponse();
        StreamReader sr = new System.IO.StreamReader(response.GetResponseStream());
        Console.WriteLine(sr.ReadToEnd().Trim());
        Console.ReadKey();
        response.Close();
    }

    public static void indexEvent(string endpoint, string index, string type)
    {
        //build the request URL            
        WebRequest request = WebRequest.Create(string.Format("{0}/{1}/{2}/", endpoint, index, type);
        request.ContentType = "application/json";   //set content type of json
        request.Method = "POST";    //use POST method when indexing a record without specifying id, and PUT when you specify an id

        string jsonEvent = "{\"id\":\"123546789\",\"parentId\":\"abc123\",\"event\":\"CloseAccount\"}";

        byte[] byteData = Encoding.GetEncoding("UTF-8").GetBytes(json);
        string result = System.Convert.ToBase64String(byteData);
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteData, 0, byteData.Length);
        dataStream.Close();

        //create a web response
        WebResponse response = request.GetResponse();
        Console.WriteLine("after get response");
        Console.ReadLine();
        StreamReader sr = new System.IO.StreamReader(response.GetResponseStream());
        Console.WriteLine(sr.ReadToEnd().Trim());
        Console.ReadKey();
        response.Close();
    }

I was doing something wrong in my createIndex function but here is my working function incase anyone finds it helpful: 我在createIndex函数中做错了什么,但是这是我的工作函数,以防有人发现它有用:

public static void createIndex(string endpoint, string index)
    {
        //Build request url
        WebRequest request = WebRequest.Create(string.Format("{0}/{1}/", endpoint, index));
        request.ContentType = "application/json";   //set content type of json
        request.Method = "PUT";    //use PUT method when creating an index
        string json = "{\"settings\":{\"activity-log-events\":{\"number_of_shards\":3,\"number_of_replicas\":2}}}";
        byte[] byteData = Encoding.GetEncoding("UTF-8").GetBytes(json);
        string result = System.Convert.ToBase64String(byteData);
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteData, 0, byteData.Length);
        dataStream.Close();

        //create a web response
        WebResponse response = request.GetResponse();
        StreamReader sr = new System.IO.StreamReader(response.GetResponseStream());
        Console.WriteLine(sr.ReadToEnd().Trim());
        Console.ReadKey();
        response.Close();
    }

You have at least one error in your indexEvent() function. 您的indexEvent()函数中至少有一个错误。 You are setting the string jsonEvent but then using an undefined string json when setting up byteData: 您正在设置字符串jsonEvent,但是在设置byteData时使用了未定义的字符串json:

string jsonEvent = "{\"id\":\"123546789\",\"parentId\":\"abc123\",\"event\":\"CloseAccount\"}";

byte[] byteData = Encoding.GetEncoding("UTF-8").GetBytes(json);

You'll note that you are doing this correctly in the other two functions, using the string json in both places. 您会注意到,在其他两个函数中都使用了字符串json正确地完成了此操作。 From createIndex: 从createIndex:

string json = "{\"settings\":{\"number_of_shards\":3,\"number_of_replicas\":2}}";
byte[] byteData = Encoding.GetEncoding("UTF-8").GetBytes(json);

and from createMapping: 并从createMapping中:

string json = "{\"activitylogevent\":{\"properties\":{\"id\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"parentId\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"event\":{\"type\":\"string\",\"index\":\"not_analyzed\"}}}}";
byte[] byteData = Encoding.GetEncoding("UTF-8").GetBytes(json);

There may be other things wrong in the code but that's definitely not going to work. 代码中可能还有其他错误,但这绝对是行不通的。

I made a some changes to my createIndex function and it worked. 我对我的createIndex函数进行了一些更改,并且它起作用了。 I posted the changes above. 我在上面发布了更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM