How to use POCOs with Fields in Elasticsearch.Net (NEST)? - nest

How to I get a strongly-typed list of objects back when doing a search that uses Fields()? For example:
var searchResult = client.Search<Person>(s => s
.Fields("title", "name")
.Query(q => q.Match(...etc...)
.Highlight(...etc...)
);
It seems like the generic type parameter is useless when .Fields() is used because the Hits that are returned have a null .Source property.
(I'm hoping there's a way to do it without having to manually map the search results back to my original Person POCO.)

When you use fields parameter in your query, elasticsearch returns specified fields in fields section of response.
{
"took" : 36,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"hits" : {
"total" : 18,
"max_score" : 1.0,
"hits" : [{
"_index" : "nest_test_data-2672",
"_type" : "elasticsearchprojects",
"_id" : "1",
"_score" : 1.0,
"fields" : {
"pingIP" : ["127.0.0.1"],
"country" : ["Faroese"],
"intValues" : [1464623485],
"locScriptField" : [0],
"stupidIntIWantAsLong" : [0],
"floatValues" : [84.96025, 95.19422],
"floatValue" : [31.93136],
"myAttachment" : [""],
"doubleValue" : [31.931359384176954],
"suggest" : [""],
"version" : [""],
"content" : ["Bacon ipsum dolor sit amet tail non prosciutto shankle turducken, officia bresaola aute filet mignon pork belly do ex tenderloin. Ut laboris quis spare ribs est prosciutto, non short ribs voluptate fugiat. Adipisicing ex ad jowl short ribs corned beef. Commodo cillum aute, sint dolore ribeye ham hock bresaola id jowl ut. Velit mollit tenderloin non, biltong officia et venison irure chuck filet mignon. Meatloaf veniam sausage prosciutto qui cow. Spare ribs non bresaola, in venison sint short loin deserunt magna laborum pork loin cillum."],
"longValue" : [-7046341211867792384],
"myBinaryField" : [""],
"name" : ["pyelasticsearch"],
"boolValue" : [false],
"id" : [1],
"startedOn" : ["1994-02-28T12:24:26.9977119+01:00"]
}
}
]
}
}
You can retrieve them from searchResult.FieldSelections or searchResult.Hits[...].Fields.
In your case Source filtering should be much more convenient.
[Test]
public void MatchAllShortcut()
{
var results = this.Client.Search<ElasticsearchProject>(s => s
.From(0)
.Size(10)
.Source(source=>source.Include(f => f.Id, f => f.Country))
.SortAscending(f => f.LOC)
.SortDescending(f => f.Country)
.MatchAll()
);
Assert.NotNull(results);
Assert.True(results.IsValid);
Assert.NotNull(results.Hits);
Assert.GreaterOrEqual(results.Hits.Count(), 10);
Assert.True(results.Hits.All(h => !string.IsNullOrEmpty(h.Source.Country)));
Assert.NotNull(results.Documents);
Assert.GreaterOrEqual(results.Documents.Count(), 10);
Assert.True(results.Documents.All(d => !string.IsNullOrEmpty(d.Country)));
}

Related

ActiveRecord Include result not containing the data of related table

Why is it when i display the jason for #portfolio_items I get the data only for Portfolio and not the technologies but when i do #portfolio_items.technologies I only get the records for the technologies
I want a jason object that contains both
#portfolio_items = Portfolio.includes(:technologies).find(27)
puts json: #portfolio_items
returns
Portfolio id: 27,
title: "Portfolio title: 0",
subtitle: "Angular",
body: "Lorem ipsum dolor sit amet, consectetur adipiscing...",
main_image: "https://via.placeholder.com/600x400",
thumb_image: "https://via.placeholder.com/350x200",
created_at:"2018-12-28 23:18:35",
updated_at: "2018-12-28 23:18:35"
and
puts json: #portfolio_items.technologies
returns
[#<Technology id: 7, name: "Technology 0", portfolio_id: 27, created_at:
"2018-12-28 23:18:35", updated_at: "2018-12-28 23:18:35">,
<Technology id: 8, name: "Technology 1", portfolio_id: 27, created_at
:"2018-12-28 23:18:35", updated_at: "2018-12-28 23:18:35">,
<Technology id: 9, name: "Technology 2", portfolio_id: 27, created_at:
"2018-
12-28 23:18:35", updated_at: "2018-12-28 23:18:35">]
So basically why is #portfolio_items not have the value thats in portfolio_items.technologies
It is because .technologies is a method being called on the #portfolio_items object which will return technologies related to it, but it does not mix them together which I think it what you are asking about. To display this as a nested JSON object you would want something like this.
puts #portfolio_items.as_json(include:{technologies:{}})
This might help if you are curious about it: https://apidock.com/rails/ActiveModel/Serializers/JSON/as_json

Parsing a JSON like text using Oracle SQL

I have a requirement to Parse a XML Text field in Oracle to remove Specific Characters/Strings in the data.
I/p:-
{{Value : "Actual: 15' 0" X 7' 0" Opening: 15' 0" X 7' 0"", Description : "Size", PrintCode : "", PrintSequence : 80},
{Value : "Section Color: Desert Tan-,Trim Board Color: White", Description : "Color", PrintCode : "", PrintSequence : 90},
{Value : "Top Section: Standard-,Board Width: Standard", Description : "Design Modifications", PrintCode : "", PrintSequence : 100},
{Value : "Size: 2"-,Mount: Bracket Mount-,Radius: 15"", Description : "Track", PrintCode : "", PrintSequence : 110},
{Value : "Springs: Standard-,Drums: Standard-,Shaft: 16 Gauge Tube", Description : "Counterbalance", PrintCode : "", PrintSequence : 120},
{Value : "Hinge: Standard-,Struts: Standard", Description : "Hardware", PrintCode : "", PrintSequence : 130}}
I need the O/P like -
"Actual: 15' 0" X 7' 0" Opening: 15' 0" X 7' 0"", Description : "Size",
"Section Color: Desert Tan-,Trim Board Color: White", Description : "Color",
"Top Section: Standard-,Board Width: Standard", Description : "Design Modifications",
"Size: 2"-,Mount: Bracket Mount-,Radius: 15"", Description : "Track",
"Springs: Standard-,Drums: Standard-,Shaft: 16 Gauge Tube", Description : "Counterbalance",
"Hinge: Standard-,Struts: Standard", Description : "Hardware"
1) I would like to remove all the brackets.
2) I would like to remove all the code that starts with PrintCode until the bracket end.
3) Should replace value : string with null.
Any help would be greatly appreciated. Thanks. :)
Try this.
SQL Fiddle
The pattern matches for the curly braces - {} or value : or PrintCode until the bracket end and replaces it with blanks. Additional trims are needed to remove , and space. .+? is for non-greedy match that searches until first occurrence of next character(} in this case).
I hope there are no new lines in the code as you said in the comments. The results could be different otherwise and the n Pattern-Matching option could be used to handle it.
Query 1:
WITH t ( input ) AS
(SELECT '{{Value : "Actual: 15'' 0" X 7'' 0" Opening: 15'' 0" X 7'' 0"", Description : "Size", PrintCode : "", PrintSequence : 80}, {Value : "Section Color: Desert Tan-,Trim Board Color: White", Description : "Color", PrintCode : "", PrintSequence : 90}, {Value : "Top Section: Standard-,Board Width: Standard", Description : "Design Modifications", PrintCode : "", PrintSequence : 100},{Value : "Size: 2"-,Mount: Bracket Mount-,Radius: 15"", Description : "Track", PrintCode : "", PrintSequence : 110},{Value : "Springs: Standard-,Drums: Standard-,Shaft: 16 Gauge Tube", Description : "Counterbalance", PrintCode : "", PrintSequence : 120},{Value : "Hinge: Standard-,Struts: Standard", Description : "Hardware", PrintCode : "", PrintSequence : 130}}'
FROM dual
)
SELECT RTRIM ( TRIM ( REGEXP_REPLACE (input,'({|}|Value +:|PrintCode.+?}(,|}))', '')) ,',') AS output
FROM t
Results:
"Actual: 15' 0" X 7' 0" Opening: 15' 0" X 7' 0"", Description : "Size", "Section Color: Desert Tan-,Trim Board Color: White", Description : "Color", "Top Section: Standard-,Board Width: Standard", Description : "Design Modifications", "Size: 2"-,Mount: Bracket Mount-,Radius: 15"", Description : "Track", "Springs: Standard-,Drums: Standard-,Shaft: 16 Gauge Tube", Description : "Counterbalance", "Hinge: Standard-,Struts: Standard", Description : "Hardware"

Decode and flatten nested json into an Elm record

I just started learning Elm and I have hit a roadblock. Looking for some help from this awesome community.
I'm looking to decode a nested json and pulling in a particular nested value into a elm record.
The json source looks like this:
{
"id": 672761,
"modified": "2018-02-12T00:53:04",
"slug": "Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.",
"type": "post",
"link": "https://awesomelinkhere.com",
"title": {
"rendered": "Sed posuere consectetur est at lobortis."
},
"content": {
"rendered": "Nulla vitae elit libero, a pharetra augue.",
},
"excerpt": {
"rendered": "Donec sed odio dui.",
}
}
and I want to pull apart the title.rendered and content.rendered into a field in my model , the model looks like so:
type alias Post =
{ id : Int
, published : String
, title : String
, link : String
, slug : String
, excerpt : String
}
my naive decoder looks like this
postDecoder : Decoder Post
postDecoder =
Decode.map6 Post
(field "id" Decode.int)
(field "modified" Decode.string)
(field "title" Decode.string)
(field "link" Decode.string)
(field "slug" Decode.string)
(field "excerpt" Decode.string)
Update
As it usually happens I found the answer as soon as I posted this. I reviewed the Json.Decode documentation and stumbled on the at function
my working decoder looks like this now
postDecoder : Decoder Post
postDecoder =
Decode.map6 Post
(field "id" Decode.int)
(field "modified" Decode.string)
(at [ "title", "rendered" ] Decode.string)
(field "link" Decode.string)
(field "slug" Decode.string)
(at [ "excerpt", "rendered" ] Decode.string)

Ember pagination jsonp callback api

I have an API example.com/api/v1
and when navigate to example.com/api/v1/items
I got json data below.
[{
"id":"1",
"title":"Lorem ipsum dolor sit amet",
"link":"/api/v1/items/1"
},
{
"id":"2",
"title":"consectetur adipisicing elit",
"link":"/api/v1/items/2"
},
{
"id":"3",
"title":"sed do eiusmod tempor incididunt",
"link":"/api/v1/items/3"
}]
.
.
.
This api accept parameter offset, count, and callback.
Ex: example.com/api/v1/items?count=10 I will get
[{
"id":"1",
"title":"Lorem ipsum dolor sit amet",
"link":"/api/v1/items/1"
},
.
.
.
{
"id":"10",
"title":"consectetur adipisicing elit",
"link":"/api/v1/items/10"
}]
Ex: example.com/api/v1/items?count=10&offset=10 I will get
[{
"id":"11",
"title":"Lorem ipsum dolor sit amet",
"link":"/api/v1/items/11"
},
.
.
.
{
"id":"20",
"title":"consectetur adipisicing elit",
"link":"/api/v1/items/20"
}]
How to implement pagination with ember?
Thanks for your time.
you could used Ember-pagination where you only have to create and array like:
var Cities = CollectionArray.create({
paginationSelector: "#cities-paging",
itemsPerPage : 6
});
And then the library will do all the hard work for you including a typeahead box where your users could search for specific Items in the collection.
I hope that this can help you

Field being ignored when querying

I have the following document
{
"authors" : "Nanna Friis",
"authorsId" : [ "4642" ],
"description" : "Med denne praktiske og pædagogiske håndbog kommer du hele vejen rundt om at skrive godt til nettet. Du bliver taget ved hånden og får en grundig gennemgang af de helt særlige præmisser, der hersker på nettet. ",
"iSBN" : "9788762904118",
"mediaType" : "10",
"name" : "Kort, klart og klikbart",
"nameSort" : "Kort, klart og klikbart",
"price" : 250.0,
"productId" : "9788762904118",
"publicationAreaCode" : "3077",
"tags" : [ ],
"titleId" : "25004"
}
When doing a query like this http://localhost:9200/titles/_search?q=Nanna* i don't get any results. If i instead query on ie. the productId like this http://localhost:9200/titles/_search?q=9788762904118 im getting the document in question.
What is going on?
you do not specify the query field in the request
in such case you will search the Default Search Field
When not explicitly specifying the field to search on in
the query string syntax, the index.query.default_field will be used to
derive which field to search on. It defaults to _all field.
So, if _all field is disabled, it might make sense to change it to set
a different default field.