简体   繁体   中英

ElasticSearch/NEST: Determine if Mapping already exists

I'm using the NEST client to access my ElasticSearch indexes/mappings. I'm trying to determine if a mapping already exists, and if not create a new mapping. There's a method for IndexExistsAsync() but there doesn't appear to be a MappingExistsAsync() method. GetMappingAsync() returns an IGetMappingResponse which has a boolean IsValid , that I thought I might be able to check, but calling GetMappingAsync() just throws an exception if the mapping doesn't exist. I can just check if an exception is thrown and create the mapping, but is there a better way of handling this?

shouldn't you use

TypeExists defined on the interface IElasticClient

Instead?

I mean if the type exists the mapping of that type also exists.

I figured it out. The issue was that I had set up the client to throw exceptions on failure instead of allowing the IsValid to be what determines success.

var connectionPool = new SingleNodeConnectionPool(node);
var settings = new ConnectionSettings(connectionPool)
    .BasicAuthentication(username, password)
    .DisableDirectStreaming()
    .PrettyJson();
    //.ThrowExceptions(); <-- This line had to be commented out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM