Reputation: 1893
I have an application with index called zzz
and I indexed few documents into the index.
string configvalue1 = ConfigurationManager.AppSettings["http://localhost:9200/"];
var pool = new SingleNodeConnectionPool(new Uri(configvalue1));
var defaultIndex = "zzz";
**settings = new ConnectionSettings(pool)
.DefaultIndex(defaultIndex)
.MapDefaultTypeNames(m => m.Add(typeof(Class1), "type"))
.PrettyJson()
.DisableDirectStreaming();
client = new ElasticClient(settings);**
if (client.IndexExists(defaultIndex).Exists && ConfigurationManager.AppSettings["syncMode"] == "Full")
{
client.DeleteIndex(defaultIndex);
client.CreateIndex(defaultIndex);
}
return client;
Now in an entire new application, i have to check if zzz
is existing or not and just use it for some search operation. Do I still have to write everything that is in between **
in the above code or just connect to the pool and check for index?
Here is my take:
configvalue1 = ConfigurationManager.AppSettings["http://localhost:9200/"];
var pool = new SingleNodeConnectionPool(new Uri(configvalue1));
settings = new ConnectionSettings(pool);
client = new ElasticClient(settings);
// to check if the index exists and return if exist
if (client.IndexExists("zzz").Exists)
{
return client;
}
Just adding to the above question:
I want to implement some condition like this before indexing:
Index doesnt exist && sync mode == full --> Create index
Index exist && sync mode==full --> Delete old index and create a new
Index doesnt exist && sync mode == new --> Create index
Index exist && sync mode==new --> Use the existing index
TIA
Upvotes: 0
Views: 144
Reputation: 125538
You would need at a minimum
string configvalue1 = ConfigurationManager.AppSettings["http://localhost:9200/"];
var pool = new SingleNodeConnectionPool(new Uri(configvalue1));
var defaultIndex = "zzz";
var settings = new ConnectionSettings(pool)
.DefaultIndex(defaultIndex);
var client = new ElasticClient(settings);
if (!client.IndexExists(defaultIndex).Exists &&
ConfigurationManager.AppSettings["syncMode"] == "Full")
{
// do something when the index is not there. Maybe create it?
}
If you're going to be using Class1
in this application and don't want to specify the type for it on all requests then add
.MapDefaultTypeNames(m => m.Add(typeof(Class1), "type"))
to connection settings.
With
.PrettyJson()
this is useful for development purposes but I would not recommend using in production as all requests and responses will be larger as the json will be indented.
Similarly, with
.DisableDirectStreaming()
again, I would not recommend using this in production unless you have a need say to log all requests and responses from the application side; this setting causes all request and response bytes to be buffered in a MemoryStream
so that they can be read outside of the client, for example, in OnRequestCompleted
.
More details on the settings can be found in the documentation.
Upvotes: 1