在Elasticsearch中建立索引

时间:2014-07-18 21:54:19

标签: json indexing elasticsearch webrequest

我在弹性搜索中遇到索引问题。我有一个创建索引的函数和另一个为json创建类型映射的函数我将要编制索引。这两个都工作正常但是当我尝试调用函数来索引某些东西时,它给了我一个WebException,说远程服务器返回了一个错误:(503)服务器不可用。任何帮助将不胜感激!谢谢!

这是我的代码:

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();
    }

我在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();
    }

2 个答案:

答案 0 :(得分:1)

indexEvent()函数中至少有一个错误。您正在设置字符串jsonEvent,但在设置byteData时使用未定义的字符串json:

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

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

你会注意到你在其他两个函数中正确地执行了这个操作,在两个地方使用字符串json。来自createIndex:

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

并从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);

代码中可能还有其他问题,但这肯定不会起作用。

答案 1 :(得分:0)

我对createIndex函数进行了一些更改,但它确实有效。我发布了上面的更改。