Use LINQ to process elastic search results

What is the most efficient method for using LINQ to process ElasticSearch results?

I came across a JSON.Net JObject Class .

Is JSON return from ElasticSearch structured in a way that lends itself to proper LINQ queries through the JObject class?

+5
source share
1 answer

This is a complete decision on how to use LINQ to process the results of a JSON request from the elasticsearch engine.


Connection Library - PlainElastic.NET

-, PlainElastic.NET : elasticsearch. GET, POST PUT. client. result.ToString() JSON elasticsearch. DLL .

JSON - JSON.NET

NewtonSoft JSON.NET LINQ. JObject, , LINQ. . , HitCount, , , , , LINQ, , . , , JSON. DLL .

'Execute the Search
Dim client = New ElasticConnection("localhost", 9200)
Dim result = client.Post("myindex/mytype/_search", elastic_query) 'elastic_query = well-formatted elasticsearch query (json string)'
Dim J As JObject = JObject.Parse(result.ToString())

'How many results were located? 
Dim HitCount = CInt(J("hits")("total"))

'What was the maximum score? Maybe you want to know this for normalizing scores to 0-100.
' - We make sure we have hits first
' - Also, make sure scoring is turned on. It might be turned off if an alternate sort method is used in your query
If HitCount > 0 AndAlso J("hits")("max_score").Type <> JTokenType.Null Then MaxScore = CDbl(J("hits")("max_score"))

LINQ

LINQ Gridviews, DataLists, Repeaters .. LINQ. (Stores, Categories)

Dim SearchResults = _
  (From X In J("hits")("hits")
    Select
      score = CDec(If(X("_score").Type = JTokenType.Null, 0, X("_score"))),
      boost = CDbl(X("_source")("_boost")),
      InstitutionID = CInt(X("_source")("InstitutionID")),
      Name = CStr(X("_source")("Name")),
      Stores = (From S In X("_source")("Stores") Select CInt(S("StoreID"))).ToList(),
      Categories = (From Z In X("_source")("Categories") Select CatID = CInt(Z("CatID")), CatName = CStr(Z("CatName")), InstitutionID = CInt(X("_source")("InstitutionID"))).ToList()
 Order By score Descending, boost Descending
)

Repeater, DataList Gridview

MyRepeater.DataSource = SearchResults
MyRepeater.DataBind()
+6

All Articles