Insert into index, Update in index and Delete from index don't work


(Guilian Nkamga) #1

Hello,

I implemented four methods for my web app:

  • Search(): searchs datasets by name (works)
  • InsertIntoIndex(): adds a newly created dataset into the index (doesn't work)
  • UpdateInIndex(): updates to values of an edited dataset in the index (doesn't work)
  • DeleteFromIndex(): removes a deleted dataset from the index (doesn't work)

Two weeks ago everything worked when I added the port number to the elastic search url ("http://...:9200"). Now it doesn't work anymore.
If I remove the port number from the elastic search url, only the search method works.
If I add the port number to the elastic search url, no method works.

Here is the Debug information (by running the Delete() Method):

Invalid NEST response built from a unsuccessful low level call on DELETE: /indexname/type/815
# Audit trail of this API call:
 - [1] BadResponse: Node: http://.../ Took: 00:00:00.1918780
# OriginalException: System.Net.WebException: The remote server returned an error: (404) Not Found.
   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Elasticsearch.Net.HttpConnection.<RequestAsync>d__14`1.MoveNext()
# Request:
<Request stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>
# Response:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>404 - File or directory not found.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;} 
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;} 
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
 <div class="content-container"><fieldset>
  <h2>404 - File or directory not found.</h2>
  <h3>The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.</h3>
 </fieldset></div>
</div>
</body>
</html>

I dont understand what I'm doing wrong and why it doesn't work.
Please can you help me to fix this problem?

Here is my code:

private readonly ElasticClient _elasticClient;

// Constructor

public SearchRepository(string elasticSearchUrl, string environment)
{
        var settings = new ConnectionSettings(new Uri(elasticSearchUrl))
            .DefaultMappingFor<MyClass>(descriptor => descriptor.BuildMapping()
                .IndexName(Index.GetFullName(environment, typeof(MyClass).ToString())))
            .DisableDirectStreaming();

        _elasticClient = new ElasticClient(settings);
        _environment = environment;
}

// Search Method

public async Task<IEnumerable<T>> Search<T>(string searchTerm) where T : class
{
        var request = CreateSearchQuery<T>(searchTerm);
        var response = await _elasticClient.SearchAsync<T>(request);

        var items = response.Documents;
        return items;
}

// Insert Method

public async Task<IndexResponse> Insert<TEntity>(string documentTypeName, TEntity document) where TEntity: Entity
{
        var indexName = Index.GetFullName(_environment, documentTypeName);

        if ( string.IsNullOrWhiteSpace(indexName) || !Index.ExistsForType(documentTypeName))
        {
            return null;
        }

        var request = new IndexRequest<TEntity>(document, indexName);
        var response = await _elasticClient.IndexAsync(request);

        if (response.Result != Result.Created)
        {
            return new IndexResponse(State.Error, $"Error while inserting document {document} into Index {indexName}.");
        }

        return new IndexResponse(State.Ok);
}

// Delete Method()

public async Task<IndexResponse> Delete(string documentTypeName, long id)
{
        var indexName = Index.GetFullName(_environment, documentTypeName);

        if (string.IsNullOrWhiteSpace(indexName))
        {
            return null;
        }

        var request = new DeleteRequest(indexName, documentTypeName, id);
        var response = await _elasticClient.DeleteAsync(request);

        if (response.Result != Result.Deleted)
        {
            return new IndexResponse(State.Error, $"Error while deleting document with ID {id} into Index {indexName}.");
        }

        return new IndexResponse(State.Ok);
}

// Update Method

public async Task<IndexResponse> Update<TEntity>(string documentTypeName, TEntity document) where TEntity : Entity
{
        var indexName = Index.GetFullName(_environment, documentTypeName);
        if (string.IsNullOrWhiteSpace(indexName))
        {
            return new IndexResponse(State.IndexNotFound, $"No index found for document type {documentTypeName}.");
        }

        var updateResponse = await _elasticClient.UpdateAsync(DocumentPath<TEntity>.Id(document.Id),
            descriptor => descriptor
                .Index(indexName)
                .Doc(document));

        if (updateResponse.Result != Result.Updated)
        {
            return new IndexResponse(State.Error, $"Error while updating document {document} into Index {indexName}.");
        }

        return new IndexResponse(State.Ok);
}

(David Turner) #2

This response is not coming from Elasticsearch. It looks like you are talking to the wrong server.


(Guilian Nkamga) #3

Thank you for your reply David. Could you explain me why the request stream can't be captured?


(David Turner) #4

I'm not too familiar with the .NET client, but did you try the suggestion in the message?

Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>

(Guilian Nkamga) #5

Yes I tried it but it doen't help me to fix the problem


(David Turner) #6

The problem isn't that the request stream can't be captured, it's that the client isn't talking to an Elasticsearch server. It looks like it's talking to something that serves HTML.


(Guilian Nkamga) #7

Thank you for your feedback David.
I was able to solve the problem. This was really due to a wrong HTML.