serilog on ElasticsearchSinkOptions mapper_parsing_exception - serilog

when try to use elastic search with serilog show me this error on log some data
Failed to store event with template 'HttpClient Request: {RequestName} {#Request}' into Elasticsearch. Elasticsearch reports for index logs the following: {"type":"mapper_parsing_exception","reason":"object mapping for [fields.Request] tried to parse field [null] as object, but found a concrete value"}
how ignore null value when Serialize to Json or how change Serializer with custom options?
did not find any example for custom serialize on elasticsearch

Related

Rails JSON store value gets quoted upon saving

I have this problem with my Rails (5.1.6) application that is running on a PostgreSQL instance.
I have a model with a JSON type column (t.json :meta). The model has a store accessor like
store :meta, accessors: [:title], coder: JSON
The problem is now when I set this value that it shows up in the database as
"{\"title\":\"I am a title\"}"
making it text rather than a JSON value, which in turn makes that I cannot use the JSON query operator (->>) to query my JSON fields. I already tried without the coder option, but this results in it being saved as YAML.
The serialize function also did not change anything for me (adding serialize :meta, JSON)
Any and all help is appreciated!
serialize and store are not intended to be used for native JSON columns. Their purpose is to marshal and un-marshal data into string columns.
This was a "poor mans" JSON storage before native JSON support existed (and was supported by ActiceRecord). Using it on a JSON column will result in a double encoded string as you have noticed.
You don't actually have to do anything to use a JSON column. Its handled by the adapter.
See:
http://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Serialization/ClassMethods.html#method-i-serialize
http://guides.rubyonrails.org/active_record_postgresql.html#json-and-jsonb

Rails JSON API - json doesn't contain errors

I am rendering an array of ActiveRecord items. Each of them has passed through valid? method - so the errors are already defined.
I render the array like this:
render json: array_of_objects
I have ActiveModelSerializers.config.adapter = :json_api set.
But the outcome json lacks errors - it contains only the data of objects.
How can I retrieve json (of each one by one or all at once) items with the errors included?
The JSON API specification is very fuzzy when it comes to how validation errors should be handled:
A server MAY choose to stop processing as soon as a problem is
encountered, or it MAY continue processing and encounter multiple
problems. For instance, a server might process multiple attributes and
then return multiple validation problems in a single response.
When a server encounters multiple problems for a single request, the
most generally applicable HTTP error code SHOULD be used in the
response. For instance, 400 Bad Request might be appropriate for
multiple 4xx errors or 500 Internal Server Error might be appropriate
for multiple 5xx errors.
Error objects can be included - but they should be included at the top level of the document:
Error objects provide additional information about problems
encountered while performing an operation. Error objects MUST be
returned as an array keyed by errors in the top level of a JSON API
document.
ActiveModel::Serializers JSON API Adapter does not provide error object handling as it would be far to complex to handle the various use cases.
Especially your case as you seem to be creating/modifying several records at once. In the case of creating its especially tricky since you need to link the error to the attribute in the input that caused the error since there is no id to point to.
You can possibly roll your own serializer:
class PostSerializer < ActiveModel::Serializer
attributes :title, :body, :errors
def errors
object.errors.full_messages if object.errors.any?
end
end

Wrong data returned from elasticsearch after recreate the index

for testing and development reasons we reindex our data from a rails app by deleting an index and recreate with mapping and import existing documents.
But after recreating the index, elasticsearch returns other results than expected and before recreating. If we restart the elasticsearch instance, the results as expected.
This is how we recreate the index.
Tire.index indexname do
delete
create _mappings
import _objects
refresh
end
We also checked the search query directly via curl on elastic search, but we got not the expected result. After restarting the elastic search daemon, same query returns expected data.
What have to be done or what is expected of an elasticsearch instance to return correct data after recreating an index with the same name without restarting? We also tried creating new indexes with timestamp names and aliasing the index name to these index, but with same results.
thanks in advance

Mongoid: getting mongo error code from Moped::Errors

I'm building a Rails server with the model stored in MongoDB using Mongoid.
There are cases where a user can attempt to add a document to the mongo database with a duplicate index value. Is there a way to retrieve the MongoDB error code (in this case 11000) without parsing the error message so that I can make my exception handling more robust?
EDIT: Title had Mongoid::Errors instead of Moped::Errors
I developed the mongoid_token gem and encountered this exact problem, since the core functionality of this gem relies on being able to identify if a particular field (in this case, the token) is the cause of the key duplication.
If all you're after is the error code, the yes - you can get this. However, if you need more precise details (such as the field name), you will need parse the error description.
Also, if you're testing for duplicate keys, I think you'll need to check for both error codes 11000 and 11001 (duplicate key on update). A partial list of the mongoDB error codes is here.
I've paraphrased some of the code from the gem below:
begin
#... do whatever
rescue Moped::Errors::OperationFailure => e
description = e.details['err']
if [11000, 11001].include?(e.details['code'])
# Duplicate key error
end
end

Getting ODBC connection results as ruby object

I've had to add an ODBC connection to a remote mysql server into my app.
This is the first time I've done something like this.
I was expecting the results to return as a ruby object, just like the rest of the ActiveRecord requests.
However, I'm beginning to think that maybe ODBC isn't handled the same way through the ruby-odbc gem.
When I output a debug statement on my models which connect through a standard connection, I get the standard
--- !ruby/object:ModelName
attributes:
etc.etc.
but when I output a debug statement on the model connected through the ODBC, i get
[#<ModelName modelID: 1, name: "name" etc. etc. ]
Is there any way for me to get ODBC connections working like the rest of my models?
UPDATE
To clarify, I am hoping to get the data as an object so I can get the data like
<%= #something.name %>
With the way I'm getting the data back now, I can't refer to it as an object.
It looks like it's returning an array of objects, instead of just one. If you only want a single object, just add .first to the result.

Resources