Can't get any documents with NEST from elasticsearch

1.4k views Asked by At

I use Searchblox to index and search my files, which itself calls ES 2.x to do the job. Searchblox uses a "mapping.json" file to initialize a mapping upon the creation of an index. Here's the link to that file. As "@Russ Cam" suggested here, I created my own class content with the following code (just like he did with the "questions" index and "Question" class):

public class Content
{
    public string type { get; set; }
    public Fields fields { get; set; }
}

public class Fields
{
    public Content1 content { get; set; }
    public Autocomplete autocomplete { get; set; }
}

public class Content1
{
    public string type { get; set; }
    public string store { get; set; }
    public string index { get; set; }
    public string analyzer { get; set; }
    public string include_in_all { get; set; }
    public string boost { get; set; }
} //got this with paste special->json class

These fields from the content class (type,store etc.) come from the mapping.json file attached above. Now, when I (just like you showed me) execute the following code:

var searchResponse = highLevelclient.Search<Content>(s => s.Query(q => q
         .Match(m => m.Field(f => f.fields.content)
        .Query("service")

All I get as a response on the searchResponse variable is:

Valid NEST response built from a successful low level call on POST: /idx014/content/_search
Audit trail of this API call:
 -HealthyResponse: Node: http://localhost:9200/ Took: 00:00:00.7180404
Request:
{"query":{"match":{"fields.content":{"query":"service"}}}}
Response:
{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}
And no documents in searchResponse.Documents. Contradictorily, when I search for the "service" query on Searchblox or make an API call to localhost:9200 with the Sense extension of Google Chrome, I get 2 documents. (the documents that I was looking for)

In brief, all I want is to be able to :

  1. get all the documents (no criteria)
  2. get all the documents within a time range and based upon keywords.. such as "service"

What am I doing wrong? I can provide with more information if needed.. Thank you all for your detailed answers.

1

There are 1 answers

0
Russ Cam On

Your C# POCO is not correct in regards to your mapping; your document type is "sdoc" and each of the properties under the "properties" property is a field on that document type; These fields map to properties on your C# POCO.

As an example to get you started

public class Document
{
    [String(Name = "uid")]
    public string UId { get; set; }

    public string Content { get; set; }
}

NEST by default will camel case POCO property names, so "content" will be case correctly according to your mapping, however, we use attribute mapping for the "uid" field in order to name it to match the mapping (we can go further here and set additional attribute property values to fully match the mapping; see the automapping documentation).

Now, to search with the document, let's create the connection settings and a client to use

void Main()
{
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var connectionSettings = new ConnectionSettings(pool)
            .InferMappingFor<Document>(t => t
                // change the index name to the name of your index :)
                .IndexName("index-name")
                .TypeName("sdoc")
                .IdProperty(p => p.UId)
            );

    var client = new ElasticClient(connectionSettings);

    // do something with the response
    var searchResponse = client.Search<Document>(s => s
        .Query(q => q
            .Match(m => m
                .Field(f => f.Content)
                .Query("service")
            )
        )
    );
}

We set up the client with some inference rules for the Document type which will be used when interacting with Elasticsearch. The above query emits the following query json

{
  "query": {
    "match": {
      "content": {
        "query": "service"
      }
    }
  }
}

As an aside, I noticed that the mapping contained a multi_field type; multi_field types were removed in Elasticsearch 1.0 (multi fields are still there, just the actual type is not), so be sure that you're actually running Elasticsearch 2.x on Searchblox, as NEST 2.x is only supported against Elasticsearch 2.x.