Using NEST - No Results

This is my result of search by calling:
http://localhost:9200/servers/server/_search?q=servername:"server1-9"

{"took":1233,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":1.3121848,"hits":[{"_index":"servers","_type":"server","_id":"Server1-9","_score":1.3121848,"_source":{"upload":false,"processcount":0,"ip":"127.0.0.1","description":"HP Blade1 Bay9","subtitlefolderpath":null,"ipvalid":"127.0.0.1","type":null,"processsize":0,"switch_device":"Cisco3550","@timestamp":"2017-10-22T18:47:31.680Z","port_number":"GI-1-1","servername":"Server1-9","dnsname":"127.0.0.1","ftpusername":null,"@version":"1","action":"Convert","location":"teh","id":30,"webservice":"http://127.0.0.1/webservice.asmx/CpuAndHDD","ftppassword":null}}]}}

How Can I implement this serarch with nest? I used this :

public class Servers
{
public string ServerName { get; set; }
public string Action { get; set; }
public bool Upload { get; set; }
}

     var responses = client.Search<Servers>(s => s.Index("servers").Type("server").Query(q => q.Term(t => 
     t.ServerName.ToLower(), "server1-9")));
     var response = client.Search<Servers>(s => s.AllTypes().AllIndices().MatchAll());
     var result = response.Documents.ToList();
     foreach (var server in result)
     {
        Response.Write("ServerName : " + server.ServerName + " Action : " + server.Action + "Upload : " + 
       server.Upload + "<br/>");
     }

But no results returned.

In your example, you're executing a query_string query so the equivalent in NEST is

var response = client.Search<Servers>(s => s
    .Index("servers")
    .Type("server")
    .Query(q => q
        .QueryString(qs => qs
            .Query("servername:\"server1-9\"")
        )
    )
);

If searches for Server types should always use "servers" index and "server" type, then you can specify these as defaults to use for Server on ConnectionSettings

var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var connectionSettings = new ConnectionSettings(pool)
    .InferMappingFor<Servers>(m => m
        .IndexName("servers")
        .TypeName("server")
    );

var client = new ElasticClient(connectionSettings);

Then you can simply do

var response = client.Search<Servers>(s => s
    .Query(q => q
        .QueryString(qs => qs
            .Query("servername:\"server1-9\"")
        )
    )
);
1 Like

Thank you so much. How can I have "server" word in results ,searching "server" word no returns result.
And for sorting results what can I do?

Take a look at analysis and analyzers, and the NEST section on writing analyzers.

Configuring analyzers to fit the search problem of your domain is an iterative approach you can take to improve the search experience.

Take a look at sorting and the Sort Usage documentation for NEST.

Thank you

I want to implement suggest and search wtih recommended structure at top that it returns the index word in search results,have sort option and autocomplete option.
But I don't know how can I implement this query.
The documents are confusing for me.
For search I perform as this below:

   string word="hello";
        var response = client.Search<Servers>(s => s
        .Query(q => q
        .QueryString(qs => qs
        .Query("servername:\"" + Word + "\"" + "+OR+"+ "action:\""+ Word +"\"").Analyzer("videos")
                )
            ).Sort(st=>st.Descending(vs.Gorean_Date))
        );

it has no any results, and I know somthings in my work is wrong.
And for suggest, I don't know how can implment that.

Anyone can help me?

It's very difficult to know what is wrong in your particular case, but I would recommend using string interpolation for the query_string query. I think it'll make your query easier to read and reason about i.e.

"servername:\"" + Word + "\"" + "+OR+"+ "action:\""+ Word +"\""

becomes

$"servername:\"{Word}\" OR action:\"{Word}\""

I don't think you need the + around OR.

Take a look at the Suggesters documentation, then at the Suggest usage in NEST. The NEST documentation provides examples of using term, completion and phrase suggesters in one API call, with both fluent lambda syntax and object initializer syntax examples.

Can you give me an simple example base on my search for suggestion option.

It's not clear how you would like suggesters to work in your case. The simplest approach to start with is to:

  1. Map a field with the completion datatype

    public class Question
    {
        public CompletionField TitleSuggest { get; set; }
    }
    
    var createIndexResponse = client.CreateIndex("my_index", c => c
        .Mappings(m => m
            .Map<Question>(u => u
                .AutoMap()
            )
        )
    );
    
  2. Index a document with completion inputs

    var question = new Question
    {
        TitleSuggest = new CompletionField
        {
            Input = new[] { "This is the title" },
            Weight = 5
        }
    };
    
    client.Index(question, i => i.Index("my_index").Refresh(Refresh.WaitFor));
    
  3. Search using the completion suggester

    var response = client.Search<Question>(s => s
    	.Index("my_index")
        .Suggest(su => su
    		.Completion("suggested_titles", cs => cs
    			.Field(f => f.TitleSuggest)
    			.Prefix("This is")
    			.Fuzzy(f => f
    				.Fuzziness(Fuzziness.Auto)
    			)
    			.Size(5)
    		)
        )
    );
    
    // do something with suggestions
    var suggestions =
        from suggest in response.Suggest["suggested_titles"]
        from option in suggest.Options
        select new
        {
            Id = option.Source.Id,
            Text = option.Text
        };
    

I implement this below for searching on my two columns of table:
Fields : "Subject" , "Description"
TableName : "Video"

   string word="Hello World";
    public class Video
    {
        public CompletionField Subject { get; set; }
        public CompletionField Description { get; set; }
    }
    var createIndexResponse = client.CreateIndex("my_video", c => c
   .Mappings(m => m
    .Map<Video>(u => u
        .AutoMap()
    )
)

);

for indexing document :

         var question = new Video
         {
             Subject = new CompletionField
             {
                 Input = new[] { Word },
                 Weight = 5
             },
            Description = new CompletionField
            {
                Input = new[] { Word },
                Weight = 5
            }
         };
         client.Index(question, i => i.Index("my_video").Refresh(Refresh.WaitFor));

and search with using completion suggester :

        var response = client.Search<Video>(s => s
        .Index("my_video")
        .Suggest(su => su
            .Completion("suggested_titles", cs => cs
                .Field(f => f.Subject).Field(x=>x.Description)
                .Prefix(Word)
                .Fuzzy(f => f
                    .Fuzziness(Fuzziness.Auto)
                    )
                    .Size(5)
                )
            )
        );

I don't know why response no has any results.
What I must be do?

Rather than creating two completion fields, you may consider including multiple inputs into one completion field.

You can't chain multiple successive calls to .Field(); a suggester operates on a single field, but you can send multiple suggesters in one request e.g.

var response = client.Search<Video>(s => s
    .Index("my_video")
    .Suggest(su => su
        .Completion("suggested_subjects", cs => cs
            .Field(f => f.Subject)
            .Prefix(Word)
            .Fuzzy(f => f
                .Fuzziness(Fuzziness.Auto)
                )
                .Size(5)
            )
        )
        .Completion("suggested_descriptions", cs => cs
            .Field(x => x.Description)
            .Prefix(Word)
            .Fuzzy(f => f
                .Fuzziness(Fuzziness.Auto)
                )
                .Size(5)
            )
        )
    );

Consider whether this is really what you want though, as opposed to using one completion field with multiple inputs.

Completion suggesters are designed for performance to be used in "provide suggestions as you type" scenarios e.g. autocompletion, but they are more limited in "search expressiveness" compared to forming a comprehensive search strategy leveraging different analysis on multiple fields. They may fit your use case perfectly or they may form part of your approach to your search problem.

I would recommend having a look at the troubleshooting section to see what is going to and coming from Elasticsearch. Make sure that any existing "my_video" index is deleted before creating a new index with the same name and different mappings.

With making debugging as you recommendation, I just replace response variable with this :

        var response = client.Search<Video>(s => s
        .MatchAll()
        );

and it returns this Error:

Error converting value "بهترین روش برای بازکردن موز " to type 'Nest.CompletionField'. Path 'hits.hits[0]._source.subject

That indicates that the mapping in the index for type Video does not align the with POCO structure. In order to change the field mapping for a POCO property, you should delete the index and create it again with the correct mapping.

I couldn't find any solution for making POCO structure base on my columns type for autocomplete.
How can I do?

Using the CompletionField type as the property, as per previous answers and examples.

I do that, the response has this result :

Valid NEST response built from a successful low level call on POST: /my_videos/my_video/_search

but the count of results is 0.

Can you help me?
What I can do?
Any Recommendation?
I do that as this answer but counts of results is zero.

Let's break it down:

  1. The request to Elasticsearch is successful as indicated by the DebugInformation:

Valid NEST response built from a successful low level call on POST: /my_videos/my_video/_search

  1. The count of results is 0

This could for a number of reasons:

  1. There is no data indexed in Elasticsearch. A quick search against GET /_search should determine whether this is true or not.
  2. There is data indexed in Elasticsearch, but it is either not in the index my_videos, or is not of the type my_video. Again, a match_all query search against GET /my_videos/my_video/_search and whether it returns results should rule this out.
  3. No documents match the query that is being executed. From previous posts, we know that the query being executed is valid, so perhaps there is a problem in the query logic. This can be investigated during development by logging out all requests and responses by passing a delegate to OnRequestCompleted() on ConnectionSettings, and writing them out somewhere e.g. to Trace, Console, file, etc.

I would suggest exploring these avenues.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.