Sorting strings based on the integer value they represent in hibernate search - hibernate-search

I have an entity with a string field in it.
Sometimes this string field stores actual words, so sorting based on lexicographical order makes sense. However, there is a use case where this field stores string values like "0%", "10%", "100%", "20%".
The default string sorting generates the following sequence: 0% 10% 100% 20%.
The ideal sequence after sorting would be 0%, 10%, 20%, 100%(determined by the percentage they represent).
It is guaranteed that there is no mixed data, i.e, you won't have "10%" and "word" appear in the same sequence that needs to be sorted.
My question is if there is a way to achieve the string sorting based on the numerical value they represent?

Ideally, you should alter your model to have two fields: one of integer type where you'll hold the percentage, and one of string type where you will hold the string value:
#Entity
#Indexed
public class MyEntity {
#Field
#SortableField
private int percentage;
#Field
#SortableField
private String notPercentage;
// ... other properties, getters and setters ...
}
Then when sorting you'll just sort on both fields. Since you are certain they are mutually exclusive, it's not a problem to sort on both:
QueryBuilder queryBuilder = ...;
FullTextQuery ftQuery = ...;
ftQuery.sort(queryBuilder.sort().byField("percentage").andByField("notPercentage").createSort());
Alternatively, if you really want to keep a single String property in your ORM model (to each his own...), you can use a custom bridge to apply padding to your values, so that they are sorted correctly:
#Entity
#Indexed
public class MyEntity {
#Field(bridge = #FieldBridge(impl = PaddingIfPercentageBridge.class))
#SortableField
private String percentageOrNot;
// ... other properties, getters and setters ...
}
public class PaddingIfPercentageBridge implements TwoWayStringBridge {
private static final Pattern PERCENTAGE = Pattern.compile("[0-9]+%");
#Override
public String objectToString(Object object) {
if ( object == null ) {
return null;
}
String string = (String) object;
if ( !PERCENTAGE.matcher( string ).matches() ) {
return string;
}
StringBuilder paddedPercentage = new StringBuilder();
for ( int padIndex = string.length(); padIndex < 4; padIndex++ ) {
paddedPercentage.append( '0' );
}
return paddedPercentage.append( string ).toString();
}
#Override
public Object stringToObject(String stringValue) {
// Ideally you should remove the padding here...
return stringValue;
}
}
Then you can sort on a single field:
QueryBuilder queryBuilder = ...;
FullTextQuery ftQuery = ...;
ftQuery.sort(queryBuilder.sort().byField("percentageOrNot").createSort());

Related

Filter over Nested Map value's in Spring data mongo query-dsl

I am integrating the Spring-Data-mongo with query-dsl, so i need to Generate Q-files for query-dsl queries
Here is my Order POJO:
public class Order {
private List<Map<String,Object>> items;
}
My Q file of Order.java
public class QOrder extends EntityPathBase<Order> {
private static final long serialVersionUID = -682690651L;
public static final QOrder order = new QOrder("order");
public final ListPath<java.util.Map<String, Object>, SimplePath<java.util.Map<String, Object>>> items = this.<java.util.Map<String, Object>, SimplePath<java.util.Map<String, Object>>>createList("items", java.util.Map.class, SimplePath.class, PathInits.DIRECT2);
public QOrder(String variable) {
super(Order.class, forVariable(variable));
}
public QOrder(Path<? extends Order> path) {
super(path.getType(), path.getMetadata());
}
public QOrder(PathMetadata metadata) {
super(Order.class, metadata);
}
}
and sample json's of order is
{
"items": [{
"itemName": "phone",
"quantity": <Integer-Number>
}
]
}
Now i want to retrieve all the order's from mongo for which any Item exist with qunatity 1.
Now i am generating my predicate like below.
"QSensorData.sensorData.data.any().eq(Some-QueryDSL-Expression)".
I am unable to identify what need to pass in eq method for filtering nested map values.
Change Order class to include List attribute where Item contains itemName and quantity fields. Something like
public class Order {
private List<Item> items;
}
public class Item {
private String itemName;
private Integer quantity;
}
Generate Q classes.
Use below query to return all items where there is atleast one item with quantity with 1.
BooleanExpression expression = QOrder.order.items.any().quantity.eq(1);
List<Order> results = repository.findAll(expression);
As noted in the comment to return all filtered items with value 1 you have to use aggregation query.
Something like
Static Imports
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
import static org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.filter;
import static org.springframework.data.mongodb.core.aggregation.ComparisonOperators.Eq.valueOf;
Aggregation Query
Aggregation aggregation = newAggregation(
project().and(filter("items")
.as("item")
.by(valueOf(
"item.quantity")
.equalToValue(
1)))
.as("items");
);
List<Order> results = mongoTemplate.aggregate(aggregation, Order.class, Order.class)

Is possible to sum a relationship field? [Using Realm]

I am trying to find a away to SUM the relationship field, but always throw an exception~
So it`s possible to sum a relationship filed by using realm database?
if u knows where i write wrong, please tell me, thanks a lot :)
Assuming I have the following dataset:
public class Service extends RealmObject {
private String id;
private RealmList<Service> extras;
private boolean selected;
// getters and setters
}
public class ServiceExtra extends RealmObject {
private String id;
private float price;
private boolean selected;
// getters and setters
}
and then, i guess the query code like this
Realm.getDefaultInstance()
.where(Service.class)
.equalTo("selected",true)
.equalTo("extras.selected",true)
.sum("extras.price");
but always throw
java.lang.IllegalArgumentException: Field does not exist: extras.price
at io.realm.RealmObjectSchema.getAndCheckFieldIndex(RealmObjectSchema.java:756)
at io.realm.RealmQuery.sum(RealmQuery.java:1592)
UPDATE
Realm realm = Realm.getDefaultInstance();
Service service = realm.where(Service.class).equalTo("selected",true).findFirst();
int totalPrice = service.extras.where().equalTo("selected",true).sum("price");
or
Realm realm = Realm.getDefaultInstance();
RealmResult<Service> services = realm.where(Service.class)
.equalTo("selected",true).finAll();
int totalPrice;
for(Service service : services){
totalPrice += service.extras.where()
.equalTo("selected",true).sum("price");
}
Try the following to sum, min, max, average field
RealmResults<User> results = realm.where(User.class).findAll();
long sum = results.sum("age").longValue();
long min = results.min("age").longValue();
long max = results.max("age").longValue();
double average = results.average("age");
long matches = results.size();

How to use hidden field to store data model in wicket

I have a entity, name Product.It have two property is unit (byte) and unitName(String). unit property is mapped on database. Ex: 0:Kg ; 1:g;.... I want when input a valid unit, unit property is stored; unless, it save to unitName
Product
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "product_id")
private int productId;
#Column(name = "product_name")
private String productName;
#Column(name = "unit")
private Byte unit;
#Transient
private String unitName;
}
In unit text field, I use a UnitConvert
UnitConvert
public class UnitConverter implements IConverter<Byte> {
private static final long serialVersionUID = 4798262219257031818L;
public UnitConverter() {
}
#Override
public Byte convertToObject(String value, Locale locale) {
return Text.isEmpty(value) ? 0 : UtilCommon.getTaniCode(value);
}
#Override
public String convertToString(Byte value, Locale locale) {
return (value == null || value==0 ) ? "" : UtilCommon.getTaniName(value);
}
}
I only think about HiddenField to do that, but I don't know how to do that.
Someone know how to use or anything can help me. Thank you very much
So from what I understood you want to save the input of a Model to a different database property depending on certain checks before hand. You can do that in your Form.onSubmit() method.
A very simple implementation could look like this:
public ProductPanel(String id, final IModel<Object> productModel) {
super(id, productModel);
// we're putting the productModel into the constructor.
// Therefore it's guaranteed to be detached
// -> it's okay to have it with final modifier.
IModel<String> formModel = Model.of("");
Form<String> form = new Form<String>("form", formModel) {
#Override
protected void onSubmit() {
super.onSubmit();
String productName = getModelObject();
Object realProduct = productModel.getObject();
if (isAcceptableUnit(productName)) {
realProduct.setUnit(parseUnit(productName));
} else {
realProduct.setUnitName(productName);
}
layer.saveProduct();
}
};
add(form);
TextField<String> productName = new TextField<String>("textField", formModel);
form.add(productName);
}
private boolean isAcceptableUnit(String productName) {
// your logic to determine if it's okay to cast to byte here...
return true;
}
private byte parseUnit(String productName) {
// your logic to parse the String to byte here...
return 0;
}
Some additional comments since I'm uncertain if the code snippets you provided are just for simplicity or actually code pieces:
You should try to avoid declaring your db object Serializable. Should you use normal Model objects to save your DTOs wicket will actually serialize them and you won't be able to do anything with them (well with hibernate at least).
Database object should use LoadableDetachableModel and save the primary key to load the entity in the load() method of it.
This would enable you now to work directly on those objects by using CompoundPropertyModel etc (which has it's pros and cons which I will not explain in detail here).
Still in your case I would add an Model<String> to the form and let the server decide how the input should be handled and mapped to the actual domain object.

Represent a single-rowed table in EF?

I have a configuration table in my database and it contains exactly one row.
ConfirmedScheduleColor | OverlappedScheduleColor | ColN
Currently, I'm retrieving the configuration like this:
var db = new SchedulingDbContext();
var config = db.Configurations.FirstOrDefault();
It's currently working fine and I can access my configurations and all. The thing is, the code looks awkward since I'm accessing the Configurations DbSet as if it contains many records (FirstOrDefault()); although actually, it contains only one record. I want to access my configurations like I'm accessing a static object. How to do that in EF?
You could simply add a property to your DbContext that returns Configurations.FirstOrDefault() and privatize the DbSet:
public class SchedulingDbContext : DbContext
{
private DbSet<Configuration> Configurations { get; set; }
public Configuration Configuration
{
get
{
return Configurations.FirstOrDefault();
}
}
}
I have a class in my project that has static methods to retrieve config settings. I use the ConfigurationManager rather than the database, but you could adapt it to get the setting from wherever you are storing the value.
In my example I have written a GetFromDb method for you that takes a key as parameter but that is because if I was storing my config settings in the database I wouldn't want to add a column every time I needed a new config setting. I would have a table with Key/Value columns. If you are wedded to the single row table then you might want to do without such a method.
public class Config
{
private _ConfirmedScheduleColor;
public static string ConfirmedScheduleColor
{
get
{
if(_ConfirmedScheduleColor == null)
_ConfirmedScheduleColor = GetFromDb("ConfirmedScheduleColor");
return _ConfirmedScheduleColor;
}
}
public static string OverlappedScheduleColor
{
get { return GetValue("OverlappedScheduleColor", "Pink"); }
}
public static int ColN
{
get { return GetValue("ColN", 2); }
}
private static string GetFromDb(string key)
{
if(key == "ConfirmedScheduleColor")
{
var config = db.Configurations.FirstOrDefault();
return config.ConfirmedScheduleColor;
}
}
private static string GetValue(string key, string defaultValue)
{
return ConfigurationManager.AppSettings[key] ?? defaultValue;
}
private static string GetValue(string key, int defaultValue)
{
int i;
if(int.TryParse(ConfigurationManager.AppSettings[key], out i))
return i;
return defaultValue;
}
}
In EF Core you can set the check constraint for the primary key. It enforces that column Id must have value that is equal to 1 which means only one record can exist in table if you have the primary key.
modelBuilder.Entity<YourTable>(e =>
{
e.HasCheckConstraint("CK_Table_Column", "[Id] = 1");
e.HasData(...) //optionally add some initial date for Id = 1
});

EclipseLink native query result into POJO - Missing descriptor for [Class]

I'm using EclipseLink to run some Native SQL. I need to return the data into a POJO. I followed the instructions at EclipseLink Docs, but I receive the error Missing descriptor for [Class]
The query columns have been named to match the member variables of the POJO. Do I need to do some additional mapping?
POJO:
public class AnnouncementRecipientsFlattenedDTO {
private BigDecimal announcementId;
private String recipientAddress;
private String type;
public AnnouncementRecipientsFlattenedDTO() {
super();
}
public AnnouncementRecipientsFlattenedDTO(BigDecimal announcementId, String recipientAddress, String type) {
super();
this.announcementId = announcementId;
this.recipientAddress = recipientAddress;
this.type = type;
}
... Getters/Setters
Entity Manager call:
public List<AnnouncementRecipientsFlattenedDTO> getNormalizedRecipientsForAnnouncement(int announcementId) {
Query query = em.createNamedQuery(AnnouncementDeliveryLog.FIND_NORMALIZED_RECIPIENTS_FOR_ANNOUNCEMENT, AnnouncementRecipientsFlattenedDTO.class);
query.setParameter(1, announcementId);
return query.getResultList();
}
I found out you can put the results of a Native Query execution into a List of Arrays that hold Objects. Then one can iterate over the list and Array elements and build the desired Entity objects.
List<Object[]> rawResultList;
Query query =
em.createNamedQuery(AnnouncementDeliveryLog.FIND_NORMALIZED_RECIPIENTS_FOR_ANNOUNCEMENT);
rawResultList = query.getResultList();
for (Object[] resultElement : rawResultList) {
AnnouncementDeliveryLog adl = new AnnouncementDeliveryLog(getAnnouncementById(announcementId), (String)resultElement[1], (String)resultElement[2], "TO_SEND");
persistAnnouncementDeliveryLog(adl);
}
You can only use native SQL queries with a class if the class is mapped. You need to define the AnnouncementRecipientsFlattenedDTO class as an #Entity.
Otherwise just create the native query with only the SQL and get an array of the data back and construct your DTO yourself using the data.
Old question but may be following solution will help someone else.
Suppose you want to return a list of columns, data type and data length for a given table in Oracle. I have written below a native sample query for this:
private static final String TABLE_COLUMNS = "select utc.COLUMN_NAME, utc.DATA_TYPE, utc.DATA_LENGTH "
+ "from user_tab_columns utc "
+ "where utc.table_name = ? "
+ "order by utc.column_name asc";
Now the requirement is to construct a list of POJO from the result of above query.
Define TableColumn entity class as below:
#Entity
public class TableColumn implements Serializable {
#Id
#Column(name = "COLUMN_NAME")
private String columnName;
#Column(name = "DATA_TYPE")
private String dataType;
#Column(name = "DATA_LENGTH")
private int dataLength;
public String getColumnName() {
return columnName;
}
public void setColumnName(String columnName) {
this.columnName = columnName;
}
public String getDataType() {
return dataType;
}
public void setDataType(String dataType) {
this.dataType = dataType;
}
public int getDataLength() {
return dataLength;
}
public void setDataLength(int dataLength) {
this.dataLength = dataLength;
}
public TableColumn(String columnName, String dataType, int dataLength) {
this.columnName = columnName;
this.dataType = dataType;
this.dataLength = dataLength;
}
public TableColumn(String columnName) {
this.columnName = columnName;
}
public TableColumn() {
}
#Override
public int hashCode() {
int hash = 0;
hash += (columnName != null ? columnName.hashCode() : 0);
return hash;
}
#Override
public boolean equals(Object object) {
if (!(object instanceof TableColumn)) {
return false;
}
TableColumn other = (TableColumn) object;
if ((this.columnName == null && other.columnName != null) || (this.columnName != null && !this.columnName.equals(other.columnName))) {
return false;
}
return true;
}
#Override
public String toString() {
return getColumnName();
}
}
Now we are ready to construct a list of POJO. Use the sample code below to construct get your result as List of POJOs.
public List<TableColumn> findTableColumns(String table) {
List<TableColumn> listTables = new ArrayList<>();
EntityManager em = emf.createEntityManager();
Query q = em.createNativeQuery(TABLE_COLUMNS, TableColumn.class).setParameter(1, table);
listTables = q.getResultList();
em.close();
return listTables;
}
Also, don't forget to add in your POJO class in persistence.xml! It can be easy to overlook if you are used to your IDE managing that file for you.
Had the same kind of problem where I wanted to return a List of POJOs, and really just POJOs (call it DTO if you want) and not #Entity annotated Objects.
class PojoExample {
String name;
#Enumerated(EnumType.STRING)
SomeEnum type;
public PojoExample(String name, SomeEnum type) {
this.name = name;
this.type = type;
}
}
With the following Query:
String query = "SELECT b.name, a.newtype as type FROM tablea a, tableb b where a.tableb_id = b_id";
Query query = getEntityManager().createNativeQuery(query, "PojoExample");
#SuppressWarnings("unchecked")
List<PojoExample> data = query.getResultList();
Creates the PojoExample from the database without the need for an Entity annotation on PojoExample. You can find the method call in the Oracle Docs here.
edit:
As it turns out you have to use #SqlResultSetMapping for this to work, otherwise your query.getResultList() returns a List of Object.
#SqlResultSetMapping(name = "PojoExample",
classes = #ConstructorResult(columns = {
#ColumnResult(name = "name", type = String.class),
#ColumnResult(name = "type", type = String.class)
},
targetClass = PojoExample.class)
)
Just put this anywhere under your #Entity annotation (so in this example either in tablea or tableb because PojoExample has no #Entity annotation)