Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help on setting Type for methods #39

Closed
jptoto opened this issue Jun 12, 2012 · 3 comments
Closed

Help on setting Type for methods #39

jptoto opened this issue Jun 12, 2012 · 3 comments

Comments

@jptoto
Copy link

jptoto commented Jun 12, 2012

Hello!

I have a situation where I'm trying to figure out a way to, for example, call some methods but setting the Type dynamically or in some similar way.

I have DeleteByQuery method

client.DeleteByQuery(q => q.Term(f => f.TY, "test"));

but I will also need to do

client.DeleteByQuery(q => q.Term(f => f.TY, "test"));

This is inside a method like:

    public void DeleteBySomething(string test)
    {
        var client = new ElasticClient(elasticSettings);
        client.DeleteByQuery<ClassType1>(q => q.Term(f => f.TY, test));
    }

I'm trying to figure out a way that I could pass a type or generic into the method that would call this function so that I wouldn't have two DeleteByQuery methods. This would help keep my code more DRY but I'm having a difficult time conceptualizing how I would do this.

Thanks for any help!!! (I realize this isn'ts really an "issue" - maybe I could start and help support a google group for NEST?)

@Mpdreamz
Copy link
Member

If ClassType1 and ClassType2 share a common base class or interface you could rewrite DeleteBySomething to

public void DeleteBySomething<T>(string test) where T : BaseClass //OR IMyInterface
{
    var client = new ElasticClient(elasticSettings);
    client.DeleteByQuery<T>(q => q.Term(f => f.TY, test));
}

If it really means find ANYTHING where f.TY equals string test

public void DeleteBySomething(string test)
{
    var client = new ElasticClient(elasticSettings);
    client.DeleteByQuery(q => q.Terms("TY", test)
   );
}

this will do a deletequery over _all indices and types. which is equivalant too:

public void DeleteBySomething(string test)
{
    var client = new ElasticClient(elasticSettings);
    client.DeleteByQuery(q => q
        .AllIndices()
        .AllTypes()
        .Terms("TY", test)
   );
}

You can limit this by explicitly specifying the indices and types

public void DeleteBySomething(string test)
{
    var client = new ElasticClient(elasticSettings);
    client.DeleteByQuery(q => q
        .Indices(new []{ "index1", "index2" })
        .Types(new[] { "type1", "type2" })
        .Terms("TY", test)
   );
}

or by being explicit on the types only:

public void DeleteBySomething(string test)
{
    var client = new ElasticClient(elasticSettings);
    client.DeleteByQuery(q => q
        .AllIndices()
        .Types(new[] { "type1", "type2" })
        .Terms("TY", test)
   );
}

or explicit indexes and all types which i'll omit because i'm sure you got the gist 2 examples ago :)

I quite like github issues over google groups personally since it feels like its more out in the open but thanks for offering!

@Mpdreamz
Copy link
Member

Also if you have a generic delete but you would like different default indexes for certain types you can do the following:

new ConnectionSettings(....)
    .SetDefaultIndex("index")
    .SetMaximumAsyncConnections(25)
    .MapTypeIndices(m => m
      .Add(typeof(Person), "persons-index") 
      .Add(typeof(Cars), "cars-index")
    );

That way you dont have to explicitly pass the exceptions into a different overload

@jptoto
Copy link
Author

jptoto commented Jun 13, 2012

This is WONDERFUL. Thanks, man! I really appreciate the feedback. I'm just getting to know the NEST fluid interface a bit better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants