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\"")
)
)
);
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.
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.
It's not clear how you would like suggesters to work in your case. The simplest approach to start with is to:
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()
)
)
);
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));
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.
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.
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
The count of results is 0
This could for a number of reasons:
There is no data indexed in Elasticsearch. A quick search against GET /_search should determine whether this is true or not.
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.
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.
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.