I have a native facet script that checks if a specific field (mapped to type long) in the document is empty, this is how I do it:
Object fieldValue = doc().get("fieldName");
return fieldValue == ScriptDocValues.EMPTY;
However, for some of the documents this returns false even when the field is empty (I've checked this with the exists filter). This behavior is inconsistent and it usually returns the correct result. Furthermore, the same document in a different host with the same mapping, same version and same code - returns the correct result.
Is there a better way to check if a field is empty?
I'm using ElasticSearch 0.90.5 with facet script 1.1.2 and java 1.7u17.
The correct way to check for an empty field is:
ScriptDocValues value = (ScriptDocValues) doc().get("field2");
return value.isEmpty();
Related
I have a situation where i have couple of fields i have to pass while calling the cosmos DB , but those fields may not always have values. Some of them might be null while passing them to the repository method. I am trying to do it like below
public interface CachedRepository{
#Query(value="select * from abc a where (#base=null or a.base=#base) and (#position=null or a.position=#position) and (#active=null or a.active = #active)")
List<BackList> getBackListOptions(#Param("base")String base,#Param("position")String position,#Param("active") String active);
The implementation class
latestDetails = repositoryA.getBackListOptions(p.getBase(),p.getPosition(),p.getActive().get(0));//active is a List and we are passing one value
I am trying to pass the request without the active parameter(i.e. active is null in the request) The exception i am getting is
Can not invoke "java.util.List.get(int)" because the return value of request.Pick.getActive() is null
The cosmos Table is
{
'_id":"a25777-j"
"empId": 2436,
"base":"JH",
"position":"HG",
"active":"J"
.........
}
And i am taking reference from this answer
How to write dynamic sql in Spring Data Azure Cosmos DB
Please let me know where i am doing it wrong
The code you have given should work fine if either null or an actual value is passed for any of the parameters. But in your case, for active, it is neither. Nothing is passed, not even null.
It seems from the comments in your code that "active" is an object of type List, and you are attempting to pass a value in that list to getBackListOptions, and extracting that value directly in the method call with p.getActive().get(0). But I think your implementation code is failing where p.getActive().get(0) is called, before getBackListOptions is even invoked. The error you are getting is not something that is returned from Cosmos Spring client, which I believe is doing what it should. You need to handle the values you are passing properly before passing them. I think you could handle this something like below.
String base = p.getBase(); //set the value here so it is clear where failure is
String position = p.getPosition(); //set the value here so it is clear where failure is
String active = p.getActive() == null ? null : p.getActive().get(0); //I think this line was failing before because p.getActive() was null, but this condition handles that case
latestDetails = repositoryA.getBackListOptions(base,position,active);//active is a List and we are passing one value
I am using hamcrest for first time, i have developed a framework to validate particular JSON field values need to satisfy some business rules, i am trying to check on of the field in JSON should not be " " or null. Please find below for the code,matcherOperator validate JSON field against a Array of values for example in ISINARRAY validate the JSON filed is present in the following list of array values [1,2,3,4] similarly NOTINARRAY validate the JSON filed should not be present given range of array values. I am trying to check whether given field is empty or null i defined array like this [null,""] and using NOTINARRAY to check JSON key value shouldn't be null or "" for null its working but the JSON value is "" its validating to true and i tried using Matchers.blankOrNullString() too but its not working and i saw two question of similar type but they are part of JUNIT testing which i tried to used but not working below are the question i referred
Hamcrest matcher with slashes is interpreted as a part of validation
Hamcrest check if value is null or empty array
public <T extends Comparable<T>> Matcher<String> mymatcher(final FieldOperator matcherOperator,
final List<String> value)
{
switch (matcherOperator) {
case ISINARRAY: {
return Matchers.in(value);
}
case NOTINARRAY: {
return Matchers.not(Matchers.in(value));
}
}
I am not sure where i am doing wrong please help me in solving the issue.
I want to use inStockFlag facet with only one option checked. I've added it - it's extended from ProductInStockFlagValueProvider. It shows on the page, but it shows with both options TRUE and FALSE:
I only need one option and it is "In Stock" checked or not checked to filter products only InStock or all products:
I've tried to add custom valueDisplayNameProvider for this facet and return empty string in case of FALSE, but it's still displaying both options (just one is with empty string). I'm using SAP Hybris v1811.
I guess you can write a custom value provider to check if it has stock. If it has, you can provide the String "In Stock" as value.
And if it doesn't have stock, you can return an empty Collection<FieldValue> from the method getFieldValues instead of providing an empty string.
I have a field that has a subdivision, like:
//name
.startObject(IndexConstants.FIRST_NAME)
.field("type").value("string")
.startObject("fields")
.startObject("folded")
.field("type").value("string")
.field("analyzer").value("folding")
.endObject()
.endObject()
The _all field only searches on firstname, not firstname.folded. If I specifically query on .folded it works, however it is a catch all query so I would not like to have to specify folded.
I have tried the "include_in_all" true for it but no change.
Thanks
As indicated in the official documentation, it makes no sense to use include_in_all in multi-fields:
The original field value is added to the _all field, not the terms produced by a field’s analyzer. For this reason, it makes no sense to set include_in_all to true on multi-fields, as each multi-field has exactly the same value as its parent.
Using copy_to could be an option with versions <2.x. However, using copy_to with multi-fields will be ignored as of 2.0 and even throw an exception as of 2.0.1 and 2.1.
You're better off matching directly on firstname.folded, if it is really important for you to query that sub-field, simply use its folding analyzer on the main firstname field and get rid of the sub-field.
I am using the below mentioned MongoDB query in Java to find the maximun value of field price:
DBCursor cursor = coll.find(query,fields).sort(new BasicDBObject("price",1)).limit(1);
fields argument passing to coll.find function here is having the price field only.
So I am getting the output in the form:
{"price" : value}
Is there any way to get value only in the output without the field name and braces etc, so that it can be assigned to a variable or returned to the calling function etc.
Or if there is any other query or mechanism available that I can use for the same purpose.
Pls suggest..
Thanks & Regards
You can get value of price from the DBCursor object as follows.
while (cursor.hasNext()) {
Double price = (Double) cursor.next().get("price");
}
On the mongo shell you can do it as follows :
db.priceObj.find({},{_id:0, price:1}).sort({price:-1}).limit(1)[0].price
You cannot do this due to the fact that MongoDB communicates using BSON.
A single value like you want would be invalid BSON. It is easy enough to filter it out your side.