我遇到了JSON.Net的JObject Class.
从ElasticSearch返回的JsON是否通过JObject类以适合liNQ查询的方式构建?
解决方法 这是关于如何利用liNQ处理来自elasticsearch引擎的JsON查询结果的完整解决方案.连接库 – PlainElastic.NET
首先,我将PlainElastic.NET用于一个目的:连接到我的elasticsearch服务器.该库非常精简,包含简洁的GET,POST和PUT功能.这将在下面与客户端连接对象发挥作用. result.ToString()提供来自elasticsearch服务器的JsON响应.只需将DLL打入您的bin并添加引用即可.
JsON处理器 – JSON.NET
我正在使用NewtonSoft JsON.NET的一个很棒的功能来促进liNQ查询.该库有一个JObject类,它提供了一个可嵌套的可查询结构来执行liNQ.请参阅下面的行,我抓住HitCount以查看获取单个值有多好,当然,请查看liNQ查询以查看如何查询它.请记住,这只是一个数据结构,它是解析JsON字符串的结果.只需将DLL打入您的bin并添加引用即可.
执行针对d性搜索的查询
'Execute the SearchDim 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 queryIf HitCount > 0 AndAlso J("hits")("max_score").Type <> JTokenType.Null Then Maxscore = CDbl(J("hits")("max_score"))
使用liNQ处理结果
现在,使用liNQ提供一个漂亮的匿名对象来绑定到GrIDvIEws,DataLists,Repeater等.我有意提供了一个非平凡的liNQ查询示例.此示例获取嵌套数据(商店,类别)
Dim SearchResults = _ (From X In J("hits")("hits") Select score = CDec(If(X("_score").Type = JTokenType.Null,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 = SearchResultsMyRepeater.DataBind()总结
以上是内存溢出为你收集整理的.net – 使用LINQ处理d性搜索结果全部内容,希望文章能够帮你解决.net – 使用LINQ处理d性搜索结果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)