How to get inner hits field values in Nest or Elastic.Net library ? Alterantivly how to specify output type in Nest or Elastic.Net library?

Hi,

I am new to Elasticsearch and I am having troubles with the Nest/Elastic.Net library.
I would like to retrieve not entire document but just part of it. I am able to do it in Postman but I cannot do it via Elastic.Net library or Nest library.
Document structure looks like following

{
	“Doc_id”: “id_for_cross_refference_with_othersystem”
	“Ocr”:[
		{
			“word”: “example_word1”,
			“box”: [],
			“cord”: “some_number”,
},
{
			“word”: “example_word2”,
			“box”: [],
			“cord”: “some_number2”,
}
]
}

The document has a huge amount of properties but I am interested only in Doc_id , ocr.word, ocr.box and ocr.cord

The following postman request fully satisfies my needs :slight_smile:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "doc_id": "2a558865-7dc2-4e4d-ad02-3f683159984e"
          }
        },
        {
          "nested": {
            "path": "ocr",
            "query": {
              "match": {
                "ocr.word": "signing"
              }
            },
            "inner_hits": {
             "_source": {
                "includes":[
                  "ocr.word",
                  "ocr.box",
                  "ocr.conf"
                ]
             }
            }
          }
        }
      ]
    }
  },
  "_source":"false"
}

Result of that request is following

{
	"took": 9,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": 18.99095,
		"hits": [
			{
				"_index": "irrelevant",
				"_type": "irrelevant",
				"_id": "irrelevant",
				"_score": 18.99095,
				"_source": {},
				"inner_hits": {
					"ocr": {
						"hits": {
							"total": {
								"value": 1,
								"relation": "eq"
							},
							"max_score": 7.9260864,
							"hits": [
								{
									"_index": "irrelevant",
									"_type": "irrelevant",
									"_id": "irrelevant",
									"_nested": {
										"field": "ocr",
										"offset": 11
									},
									"_score": 7.9260864,
									"_source": {
										"box": [
										],
										"conf": "96.452858",
										"word": "signing"
									}
								}
							]
						}
					}
				}
			},
			{
				"_index": "there_rest _of_object_is_ommited",
				
			},
			{
				"_index": "there_rest _of_object_is_ommited",
				
			}
		]
	}
}

However when I try to convert that request to Nest Query DSL I am not able to achieve the same result.
When I try to use the NEST library I don’t see any way to provide output result model/type. It looks like the Type of Document should match the output type which is not my case.
Query that I am using :

var searchResponse = client2.Search<Model>(s => s
                                    .Query(q1 => q1.Bool(b1 => b1.Must(s1 =>
                                                                             s1.Match(m => m.Field(f => f.doc_id).Query("2a558865-7dc2-4e4d-ad02-3f683159984e")),
                                                                             s2 => s2.Nested(n => n.Path("ocr").Query(q2 => q2.Bool(b => b.Must(m => m.Match(m => m.Field(f => f.ocr.First().word).Query("signing")))))
                                                                            .InnerHits(ih => ih.Source(s => s.Includes(i => i.Field(f => f.ocr.First().word).Field(f => f.ocr.First().conf))))
                                                                                                                                                       )
                                                                            )))
                                                                                    .Source(false)
                                                                                );

Due to the fact that the Model type is created for a document and it doesn’t match the output type I am getting [null, null, null] as the output .

There is property such properties as Hits in ISearchResponse? But when I look into it I cannot see values of fields.

I tried using a low level client (Elastic.Net) and providing json request as a string. But It looks like there is not way of specifying the output type either. When I ran my code with the low level library it returns me 3 object of class Model with empty fields.

My questions are :

  1. Is it possible to specify output type different from document type for Nest query DSL or Elatic.Net library ?
  2. Is it possible to get values of the fields that I specified in the request for inner hits with help of Nest or Elastic.Net libraries?
  3. How would you solve such problem ? I mean we have huge documents and we don’t want to pass unnecessary information back and forth. The inner hits approach looks like a neat solution for us but it doesn’t look like it works with the recommended libraries Unless I am doing some silly mistake.

NOTE: I can achieve desired result with simple use of HTTPClient and manually doing what I need , but I hope to leverage library that is written for this purpose(Nest or Elastic.Net).

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.