Reputation: 8273
I pretty new to ElasticSearch
.
I'm unable to search using ElasticSearch
i tried couple of ways, Nothing seems to be working for me.
If i use sense chrome toolkit Sense extension for chrome and query this
GET /employee/_search?q=FirstName="Eldho"
It is working Fine.
I have already looked out this answer, doesn't work for me either.
Unable to search using Nest
protected ElasticClient Client;
IndexName index = "employee";
public ElasticSearchRepository(Uri elasticServerUri)
{
var connection = new ConnectionSettings(elasticServerUri).DefaultIndex("employee");
this.Client = new ElasticClient(connection);
}
//This is how i create Index
public void CreateIndex()
{
var settings = new IndexSettings();
settings.NumberOfReplicas = 1;
settings.NumberOfShards = 1;
var indexstate = new IndexState();
indexstate.Settings = settings;
Client.CreateIndex(index, g => g.Index(index)
.InitializeUsing(indexstate)
.Mappings(j => j.Map<Employee>(h => h.AutoMap(1))));
}
public List<Employee> Search(string search)
{
//All 3 searches are not wokring for me
var response = Client.Search<Employee>(s => s
.AllIndices()
.AllTypes()
.From(0)
.Size(10)
.Query(q =>q
.Term(t => t.FirstName, "Eldho")));
var result = Client.Search<Employee>(h => h
.Query(q => q
.Match(m => m.Field("FirstName").Query(search))));
var result2 = Client.Search<Employee>(h => h
.Type("employee")
.Query(k => k
.Term(g => g.FirstName, "Eldho")));
return result.Documents.ToList();
}
Please let me know what i'm doing wrong.
Upvotes: 3
Views: 2149
Reputation: 125488
I can't see anywhere in your code where you're indexing any documents, but I'm guessing that you have already indexed them.
The first search
var response = Client.Search<Employee>(s => s
.AllIndices()
.AllTypes()
.From(0)
.Size(10)
.Query(q =>q
.Term(t => t.FirstName, "Eldho")));
won't match because you are using a term
query with "Eldho"
, but the mapping for the field that will be performed using .AutoMap()
will by default use a standard analyzer on the string
field which will, amongst other things, lowercase tokens. If you were to change to a term
query using "eldho"
, I would expect to find a match.
Your second search
var result = Client.Search<Employee>(h => h
.Query(q => q
.Match(m => m.Field("FirstName").Query(search))));
Will not match because by default, NEST camelcases property names when indexing, mapping, search, etc. So a property named FirstName
on your POCO will map to a field named firstName
in the mappings for that document type.
Your third search
var result2 = Client.Search<Employee>(h => h
.Type("employee")
.Query(k => k
.Term(g => g.FirstName, "Eldho")));
suffers from the same issue as your first search.
To perform a query string query in NEST that would match the query you send in Sense
client.Search<Employee>(s => s
.Query(q => q
.QueryString(qs => qs
.Fields(f => f.Field(ff => ff.FirstName))
.Query("Eldho")
)
)
);
if you have indexed the document with NEST using the connection settings in your question, then this should work although I notice in your Sense query that you get results using the field "FirstName"
. To change the field inference in NEST so that it does not camel case field/property names, we can use
var settings = new ConnectionSettings(elasticServerUri)
.DefaultFieldNameInferrer(p => p);
var client = new ElasticClient(settings);
Upvotes: 5