Is there a way to trigger spring-elastic-search mapping type when a field value is null by default? - spring-data-elasticsearch

In our current Spring-data-elasticsearch v.4.0.4 application, we are using #Document on a POJO and we notice that whenever we have a null value field, the field will not have a mapping type in Elasticsearch. Therefore when we run a query and sort on this field, we will have Elasticsearch error No mapping found for [view_date] in order to sort on
We tried using the storeNulValue option of the #Field in v.4.1.0, the field presents in the index. However, there is still no mapping type for this field in Elasticsearch and we still see the no mapping found error when we sort on this null value field.
Is there a way to trigger the mapping even when a field has null value?

How do you have the mapping for your index created? When creating the mapping with Spring Data Elasticsearch this is done without a special entity and without knowing if a special instance has null valued properties. the storeNullValue argument is used then.
The mapping will be automatically created when you use Spring Data Elasticsearch repositories and the index does not yet exist. Or you need to call the IndexOperations.create() and IndexOperations.putMapping() by yourself.

Related

How to filter with any field values in Object in Firebase console?

I have tried to filter the field from the object from the Firestore console but there is no option to filter that with the object.
If we have a list then we have the option with array-contains in Add Condition.
For Ex: I need to filter with name field from object,
You can put the fully qualified field name in the filter field. So in this case, you'd use TempObj.name to get the filtering you want.

mongoid pluck - not returning correct values with default values in model

I have a User model where there is a department field which has a default value of "Engineering".
This field was introduced 2 months after our website went live,and since its mongo, there was no migration.
When I try to get the object using where, correct value is returned
User.find_by(:name => "John).department
However, if I try to pluck values, it returns nil and not the default value.
User.limit(2).pluck(:department)
returns
[nil,"Finance"]
I researched a bit and came across this blog post http://ahmadsherif.com/blog/2013/01/29/mongoid-default-fields-can-give-you-hard-time/
I think I am facing the same issue. Is there any work around for this? I chose to go with pluck because it's not memory intensive and saves time.
Basically the behaviour here can be explained by the difference in how MongoDB deals with default values vs a traditional relational database like Postgres.
In the SQL world you set defaults via the database schema and the DB will fill a NULL field everytime you insert a row.
Since MongoDB is schemaless document fields have a default value of nil, which cannot be changed* since there is no schema where we could define defaults on the database level. Instead defaults are implemented on the application level. For Mongoid this means when you initialize a new model instance it will fill in default values if they are nil.
In ActiveRecord terms it would look like this:
class Thing < ActiveRecord::Base
after_initialize :set_default_foo, if: -> { self.foo.nil? }
private
def set_default_foo
self.foo = "bar"
end
end
However if you have existing documents and add a new field or defaults to an existing field Mongoid does not update the existing documents for you!
So how does this explain these two cases?
User.find_by(:name => "John").department
User.limit(2).pluck(:department)
In the first case you a pulling a document out of storage and using it to inialize a model instance. When the model instance is initialized a callback is run which sets the default values.
When .pluck is called on the hand Mongoid pulls the values directly from the store without initializing any model instances. Thus for any "legacy" documents it will return a nil value.
To remedy this you you need to set the default value for any document with a nil.
User.where(department: nil).update_all(department: 'engineering')

Grails generating error while altering a table

I am trying to move an already existing PHP application into grails.
I have created the domains based on the existing database and the code worked perfectly.
The issue arises when I need to add an additional boolean field in my domain.
I am getting the following error.
2014-06-10 16:24:54,146 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate - Unsuccessful: alter table entry add expedite tinyint not null
Error |
2014-06-10 16:24:54,163 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate - ALTER TABLE only allows columns to be added that can contain nulls, or have a DEFAULT definition specified, or the column being added is an identity or timestamp column, or alternatively if none of the previous conditions are satisfied the table must be empty to allow addition of this column. Column 'expedite' cannot be added to non-empty table 'entry' because it does not satisfy these conditions.
I have tried to specify default values in the variable itself.
boolean expedite = false
I also tried to add default values in static mapping as below:
static mapping = {
table 'entry'
expedite defaultValue: false
version false
}
But still the error crops up. Any idea where I am going wrong? I am using sql server 2012.
Since by default mysql maps boolean field as one bit value, so the value of the boolean field can not be null.
Update your existing records manually by:
update my_table set expedite = 0;
Or you can use grails database migration plugin to generate migrations for you.
Any primitive data types in a domain class gets default value, so if you would have defined your new field like Boolean expedite then it can work with null values.
So always be sure with primitive & non primitive data types.
Looks like sql server uses 0 and 1 instead of TRUE/FALSE. Is this what you are looking for?
https://forum.hibernate.org/viewtopic.php?f=1&t=996345
another person solves like this...
http://codexplo.wordpress.com/2012/06/21/mapping-a-boolean-with-hibernate/
Looks like you just have to create a method in your domain to intercept and convert.
Try use class, not primitive type: Boolean expedite

Can't understand, what does method getSingularObjectFromString do?

I'm developing own custom field type in JIRA.
My class is very simple, it extends GenericTextCFType.
My goal is to store some identifier (ID) of field value in database but to show human-readable caption of the field value on Issue form.
I searched methods of GenericTextCFType class and found method getSingularObjectFromString, and I don't understand, what it does.
JIRA javadoc says: "Returns a Singular Object, given the string value as passed by the presentation tier"
But what is the Singular Object and what is it needed for?
Yes, it's not a great name. I wrote about it in detail in "Practical JIRA Plugins"
(O'Reilly). Here's an extract from there describing many of the methods in detail (sorry about the formatting). The book also has worked examples available at https://bitbucket.org/mdoar/practical-jira-plugins
CustomFieldType Methods
The example’s custom field type class will implement the CustomFieldType interface as usual, but instead will extend a class higher up in the inheritance hierarchy than NumberCFType. The class we will extend is AbstractCustomFieldType and it’s at the root of most classes that implement CustomFieldType.
The methods in the CustomFieldType interface with “SingularObject” in their name refer to the singular object, in this example a Carrier object. All other methods in JIRA 4 custom fields that refer to an Object are referring the transport object, e.g., a Collection of Carrier objects. JIRA 5 removed the use of Object in most custom field methods.
For more information about what changed in JIRA 5.0 with custom fields see https://developer.atlassian.com/display/JIRADEV/Java+API+Changes+in+JIRA+5.0#JavaAPIChangesinJIRA5.0-CustomFieldTypes. There were some major changes in the class hierarchy, and most classes now have a Java generic as a parameter instead of just using an Object as before.
There are two objects that are typically injected into the constructor of a custom field type’s class. The first is a CustomFieldValuePersister persister object, which is what will actually interact with the database. The second is a GenericConfigManager object that is used for storing and retrieving default values for the custom field. Other objects are injected into the constructor as needed—for example, the DoubleConverter in Example 2-2.
The first set of methods to consider are the ones that the custom field type uses to interact with the database in some way.
getSingularObjectFromString()
This method converts a string taken from the database such as “42.0###The answer” into a Carrier object. A null value means that there is no such object defined.
Fields with Multiple Values
Collection<Carrier> getValueFromIssue(CustomField field, Issue issue)
This is the main method for extracting what a field contains for a given issue. It uses the persister to retrieve the values from the database for the issue, converts each value into a Carrier object and then puts all the Carrier objects into a trans- port object Collection. A null value means that this field has no value stored for the given issue. This is one of the methods that used to return an Object before JIRA 5.0
createValue(CustomField field, Issue issue, Collection<Carrier> value)
updateValue(CustomField field, Issue issue, Collection<Carrier> value)
These methods create a new value or update an existing value for the field in the given issue. The persister that does this expects a Collection of Strings to store, so both of these methods call the method getDbValueFromCollection to help with that.
getDbValueFromCollection()
A private convenience method found in many custom field type classes, sometimes with a different name. It is used to convert a transport object (e.g., a Collection of Carrier objects) to a Collection of Strings for storing in the database.
setDefaultValue(FieldConfig fieldConfig, Collection<Carrier> value)
Convert a transport object (a Collection of Carrier objects) to its database repre- sentation and store it in the database in the genericconfiguration table.
Collection<Carrier> getDefaultValue(FieldConfig fieldConfig)
Retrieve a default value, if any, from the database and convert it to a transport object (a Collection of Carrier objects). The FieldConfig object is what represents the context of each default value in a custom field.
The next set of methods to consider are the ones that interact with a web page in some way. All values from web pages arrive at a custom field type object as part of a Custom FieldParams object, which is a holder for a Map of the values of the HTML input elements.
validateFromParams(CustomFieldParams params, ErrorCollection errors, FieldConfig config)
This is the first method that is called after a user has edited a custom field’s value. Any errors recorded here will be nicely displayed next to the field in the edit page.
getValueFromCustomFieldParams(CustomFieldParams customFieldParams)
This method is where a new value for a field that has been accepted by validate FromParams is cleaned and converted into a transport object. The custom FieldParams object will only contain strings for the HTML elements with a name attribute that is the custom field ID—e.g., customfield_10010. A null value means that there is no value for this field.
getStringValueFromCustomFieldParams(CustomFieldParams parameters)
This method returns an object that may be a String, a Collection of Strings or even a CustomFieldParams object. It’s used to populate the value variable used in Chapter 3: Advanced Custom Field Types Velocity templates. It’s also used in the Provider classes that are used by custom field searchers.
String getStringFromSingularObject(Carrier singularObject)
This method is not the direct opposite of getSingularObjectFromString as you might expect. Instead, it is used to convert a singular object (Carrier) to the string that is used in the web page, not to the database value. The returned String is also sometimes what is stored in the Lucene indexes for searching (“More Complex Searchers” on page 57). The singular object was passed into this method as an Object before JIRA 5.0.
The final set of CustomFieldType methods to consider are:
Set<Long> remove(CustomField field)
This method is called when a custom field is entirely removed from a JIRA instance, and returns the issue ids that were affected by the removal. The correct method to use for deleting a value from a field is updateValue.
String getChangelogValue(CustomField field, Object value)
String getChangelogString(CustomField field, Object value)
These methods are how the text that is seen in the History tab of an issue is gen- erated. When a custom field of this type changes, these methods are called with the before and after values of the field. The difference between the two methods is that the if the value later becomes invalid, the string will be shown instead (https://developer.atlassian.com/display/JIRADEV/Database+Schema#DatabaseSchema-ChangeHistory).
extractTransferObjectFromString()
extractStringFromTransferObject()
These methods are not from the CustomFieldType interface but they exist in the standard Multi fields for use during project imports.
Other Interfaces
There are a few other interfaces that are commonly implemented by custom field types.
ProjectImportableCustomField
The getProjectImporter method from this interface is used to implement how the custom field is populated during importing a project from an XML backup. If you don’t implement this interface then project imports will not import values for your custom field.
MultipleCustomFieldType
MultipleSettableCustomFieldType
These two interfaces are used by custom fields with options and that furthermore can have more than one option. For these classes, the values can be accessed using the Options class, which is a simple subclass of a Java List. These interfaces are not really intended for use by general-purpose multiple value custom field types.
Fields with Multiple Values | 41
SortableCustomField
This interface contains a compare method for comparing two singular objects. This is used by the Issue Navigator when you click on a column’s heading to sort a page of issues. This is actually a slower fallback for custom fields that don’t have a searcher associated with them (see Chapter 4).
RestAwareCustomFieldType
RestCustomFieldTypeOperations
These two interfaces are how the JIRA REST API knows which fields can be retrieved or updated. New in JIRA 5.0.

ObjectContext .CreateDatabaseScript() Ignores some entities although they are mapped correctly

CreateDatabaseScript() method of ObjectContext, generate string containing script to create the desired database, but it ignores some entities with its related FKs
When I look into the script, I found something like this
-- Ignoring entity set with defining query: [FMISDomainModelEFStoreContainer].[Ex_Students]
-- Ignoring association set with participating entity set with defining query: [FK_Ex_Students_City]
Is there a problem in my model? although it is validated correctly.
Oh, Sorry I found it, in XML of the entity model this error
Errors Found During Generation:
warning 6002: The table/view 'FMIS.dbo.MyTableName' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view.
I think that's why it is not generated in StorageModel of my model

Resources