amiel mendoza
amiel mendoza

Reputation: 3

How to search to the default index using NEST?

This is my default index:

new ConnectionSettings(node).DefaultIndex("profiles")

I currently have this query where I need to perform the search using NEST:

GET profiles/_search

My problem is, NEST is requiring an object for Search method.

ElasticSearch.Search< object>()

How can I perform the search on profiles index itself?

Upvotes: 0

Views: 167

Answers (2)

Russ Cam
Russ Cam

Reputation: 125528

The T in NEST methods like Search<T>() is used for a couple of purposes:

  1. Provide strongly typed access through lambda expressions for properties of T
  2. In the case where an API returns a JSON document such as Search<T>() returning the originally indexed document under the _source field of each hit, T will be the type to which that document is deserialized.

If you don't need or desire either of those behaviours, you can use object or dynamic for T and specify strings for values such as Field. You'll then need to work out how to read that POCO; in the case of dynamic, the type returned is an internal JObject like type, so you could access properties on it dynamically.

In order to perform a search without a type in the URI, you can use

var searchResponse = client.Search<object>(s => s
    .AllTypes()
    .Query(q => q
        .Match(m => m
            .Field("some_field")
            .Query("match query")
        )
    )
);

which would yield a search request like

POST http://localhost:9200/profiles/_search 
{
  "query": {
    "match": {
      "some_field": {
        "query": "match query"
      }
    }
  }
}

Upvotes: 0

ibrahimozgon
ibrahimozgon

Reputation: 1187

ElasticSearch.Search() doesn't search on the object. It uses your profiles index when you call Search method.

You will get a result after a search. The object is needed for it. Create a model class for profile with properties like below.

public class Profile
{
    public string Name{get;set;}
}

And call your ElasticSearch server with

ElasticSearch.Search<Profile>()

It will give you a profile list in result.

Upvotes: 1

Related Questions