How can I filter properly using Objectify 4 by several parameters, considering that some of those parameters can come empty, which would mean that I don't want to filter by those?
Example:
Please consider I want to filter something like this:
releases = ofy().load().type(Release.class)
.filter("user.name", searchCriteria.getName())
.filter("category", searchCriteria.getCategory())
.filter("city", searchCriteria.getCity()).list();
In order to match with what I said above, I have now the following code, checking every time which of my parameters come empty so I don't put them on the filter in that case:
if (!nameEmpty && !categoryEmpty && !cityEmpty) {
releases = ofy().load().type(Release.class)
.filter("user.name", searchCriteria.getName())
.filter("category", searchCriteria.getCategory())
.filter("city", searchCriteria.getCity()).list();
} else if (!nameEmpty && !categoryEmpty) {
releases = ofy().load().type(Release.class)
.filter("user.name", searchCriteria.getName())
.filter("category", searchCriteria.getCategory()).list();
} else if (!nameEmpty && !cityEmpty) {
releases = ofy().load().type(Release.class)
.filter("user.name", searchCriteria.getName())
.filter("city", searchCriteria.getCity()).list();
} else if ...
...
How can I avoid this crappy way of filtering and make it with just one line (or a few) using Objectify 4?
Query<Release> query = ofy().load().type(Release.class);
if (!nameEmpty)
query = query.filter("user.name", searchCriteria.getName());
if (!categoryEmpty)
query = query.filter("category", searchCriteria.getCategory())
if (!cityEmpty)
query = query.filter("city", searchCriteria.getCity());
releases = query.list();
Related
I am creating a test, and am trying to create a method that pulls a specific job posting ID when it matches the job posting title, the job type and the description, just in case that the case that there are more than one job posting with the same title.
I cannot get the select statement to pull the job posting ID out of the instance variable. Debugging shows that there is indeed the ID nested in the instance variable, but my conditions aren't being met because I am not doing it correctly.
#job_posting is the instance variable that contains the ID that I need, but I need my parameters in select to match so I can subsequently return the ID.
whenever I ONLY use posting title,such as:
target_postings = #job_postings.select{|posting|posting[:posting_title]}
it works and returns the ID I need, however I cannot do this:
def get_specific_posting_id_for_posting(posting_title, job_type, description)
expect(#job_postings.length > 0)
target_postings = #job_postings.select {|posting| posting[:posting_title] == posting_title; posting[:job_type] == job_type; posting[:description] == description}
expect(target_postings.length == 1)
target_posting = target_postings[0]
posting_id = target_posting[:posting_id]
posting_id
end
It looks like
target_postings = #job_postings.select {|posting| posting[:posting_title] == posting_title; posting[:job_type] == job_type; posting[:description] == description}
should probably be
target_postings = #job_postings.select do |posting|
posting[:posting_title] == posting_title
&& posting[:job_type] == job_type
&& posting[:description] == description
end
Your version has three separate checks, the first two of which do nothing, only the last statement in the block is actually being used to determine whether the item matches.
As an aside, since it looks like you only want the single first element that matches your conditions, you might want to consider using find instead of select. It works the same except it will stop iterating and return as soon as it finds the first matching item.
we have a TYPO3 project and use SOLR as search engine. i'm wondering how does a if-statement/conditional look like in the config files written in TypoScript?
TYPO3: 8.7
Apache Solr for TYPO3 - ES: 7.5.3
for example: we use
plugin.tx_solr.search.query.sortBy = data_datax_title_stringS asc
as sort setting for the following facet block:
plugin.tx_solr.search.faceting.facets {
but we only want that sorting if a certain part of the url query string is empty (q= or q=Searchtext)
so the sortBy should be applied if the query string looks like
http://localhost/?tx_solr%5Bq%5D=&L=0&id=2883
and should not be applied on
http://localhost/?tx_solr%5Bq%5D=Searchtext&L=0&id=2883
here is the actual block:
[globalVar = TSFE:id={$site.config.search_page_blasts}]
plugin.tx_solr.search.targetPage = {$site.config.search_page_blasts}
config.defaultGetVars {
tx_solr.filter.0 = result_type_filter:blast
}
plugin.tx_solr.search.query.sortBy = dynamic_field_A_stringS asc
plugin.tx_solr.search.faceting.facets {
facetX < lib.solr.facets.directBlast
facetX.field = dynamic_field_X_boolS
}
any help is highly appreciated
edit:
i found
plugin.tx_solr.search.query.getParameter = q
but this only defines the name for the query get-parameter in case another service needs a specific name. but i would need the value of this parameter and condition the sortBy by its value.
edit:
after some research im now at this state: the sort works if a query string is set - but not if its empty
[globalVar = GP:q = ""]
plugin.tx_solr.search.query.sortBy = wine_winery_title_stringS asc
[else]
plugin.tx_solr.search.query.sortBy =
[end]
[globalVar = TSFE:id={$site.config.search_page_wineries}]
plugin.tx_solr.search.targetPage = {$site.config.search_page_wineries}
config.defaultGetVars {
tx_solr.filter.0 = result_type_filter:winery
}
plugin.tx_solr.search.faceting.facets {
direct_sale < lib.solr.facets.directSale
direct_sale.field = winery_direct_sale_boolS
i dont really know how to format this the right way - nested conditions are not supported right?
We had a similiar use case and could not find a solution with existing featureset, so we ended up using a userfunction to check and redirect using the correct parameters.
Imo this is not the best way it should be solved though, so I am curious about other proposals.
# default sort by wine_winery_title_stringS
page.1558101700 = USER_INT
page.1558101700.userFunc = VENDOR\MyExt\UserFuncs\Solr->applyDefaultSorting
the userfunc could look like this:
/**
* #return void
*/
public function applyDefaultSorting()
{
$requestUrl = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
$solrParameters = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('tx_solr');
$additionalParameterCharacter = '?';
if(strpos($requestUrl, '?') !== false){
$additionalParameterCharacter = '&';
}
// todo: get solr search parameter name from settings
if(isset($solrParameters['q']) === false || empty($solrParameters['q'])){
$redirectUri = $requestUrl . $additionalParameterCharacter . 'tx_solr[sort]=wine_winery_title_stringS+asc';
header('Location: '.$redirectUri);
}
}
hope it helps
[globalVar = GP:q = ""]
plugin.tx_solr.search.query.sortBy = wine_winery_title_stringS asc
[else]
plugin.tx_solr.search.query.sortBy =
[end]
but with [globalString = GP:q = /^$/]?
i ended up extending the outer condition and just having two blocks for that page - i feels redundant but works perfectly fine. the condition outside of the existing one was not working - and nested conditions are not possible. so here is the solution:
with globalString to use regex for comparison
[globalVar = TSFE:id={$site.config.search_page_x}] && [globalString = GP:tx_solr|q = /.+/]
and globalVar for an empty parameter
[globalVar = TSFE:id={$site.config.search_page_x}] && [globalVar = GP:tx_solr|q =]
hope i can save someone a little time researching - thanks to everyone for their input
I'm trying to process an array of JSON objects that have various common attributes, filtering each array entry on one or more of those attributes.
Normally, I'd just do it something like this:
let filteredResultsArray = originalArray.filter((obj) => {
return obj.attribute1 <= 3 && obj.attribute2 > 0 && obj.attribute3 === 10;
});
My problem is that the filter parameters (the part after "return" in the code above) are highly variable (and unpredictable) from run to run, so I can't hard-code them in the filter. I compute them on the fly and store the whole thing in a string in my code. For example, on one run it might be:
myAttributeString = "obj.attribute1 <= 3 && obj.attribute2 > 0 && obj.attribute3 === 10";
I've tried doing this:
let filteredResultsArray = originalArray.filter((obj) => {
return myAttributeString;
});
That's failing to filter anything. Apparently .filter() is not properly interpreting what I've stored in myAttributeString as filter criteria.
I have a sneaking suspicion that eval(myAttributeString) might be one way to pull this off, but unfortunately I'm working on a team where we've got tslint set to disallow the use of eval(), so that's not an option.
Anybody have an idea how I can get this to work?
When you "compute them on the fly", instead of creating a string, create a callback function that you can then pass to filter. For example, instead of
const myAttributeString = "obj.attribute1 <= 3 && obj.attribute2 > 0 && obj.attribute3 === 10";
do
const filterCallback = obj => obj.attribute1 <= 3 && obj.attribute2 > 0 && obj.attribute3 === 10
Then, later, when the appropriate time comes to .filter, simply pass that as the callback:
const filteredResultsArray = originalArray.filter(filterCallback);
If you can't pass functions around, another option would be to build an array of conditions, for example
[
{
prop: "attribute1",
constraint: "<=",
value: 3
},
{
prop: "attribute2",
constraint: ">",
value: 0
},
// ...
]
and then turn the object into the filter function needed.
****************************************UPDATE******************************
As I suspected, eval() did work, but since I can't use it in my delivered code, and thanks to CertainPerformance's suggestion (which put my thinking on the right track) as well as the Node.js documentation site (via a lucky Google search), I was able to find a workaround using the vm module:
import * as vm from "vm";
let filteredResultsArray = originalArray.filter(
vm.runInThisContext("(obj) => {
return " + myAttributeString + ";}"));
Case closed.
You can get the child count via
firebase_node.once('value', function(snapshot) { alert('Count: ' + snapshot.numChildren()); });
But I believe this fetches the entire sub-tree of that node from the server. For huge lists, that seems RAM and latency intensive. Is there a way of getting the count (and/or a list of child names) without fetching the whole thing?
The code snippet you gave does indeed load the entire set of data and then counts it client-side, which can be very slow for large amounts of data.
Firebase doesn't currently have a way to count children without loading data, but we do plan to add it.
For now, one solution would be to maintain a counter of the number of children and update it every time you add a new child. You could use a transaction to count items, like in this code tracking upvodes:
var upvotesRef = new Firebase('https://docs-examples.firebaseio.com/android/saving-data/fireblog/posts/-JRHTHaIs-jNPLXOQivY/upvotes');
upvotesRef.transaction(function (current_value) {
return (current_value || 0) + 1;
});
For more info, see https://www.firebase.com/docs/transactions.html
UPDATE:
Firebase recently released Cloud Functions. With Cloud Functions, you don't need to create your own Server. You can simply write JavaScript functions and upload it to Firebase. Firebase will be responsible for triggering functions whenever an event occurs.
If you want to count upvotes for example, you should create a structure similar to this one:
{
"posts" : {
"-JRHTHaIs-jNPLXOQivY" : {
"upvotes_count":5,
"upvotes" : {
"userX" : true,
"userY" : true,
"userZ" : true,
...
}
}
}
}
And then write a javascript function to increase the upvotes_count when there is a new write to the upvotes node.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.countlikes = functions.database.ref('/posts/$postid/upvotes').onWrite(event => {
return event.data.ref.parent.child('upvotes_count').set(event.data.numChildren());
});
You can read the Documentation to know how to Get Started with Cloud Functions.
Also, another example of counting posts is here:
https://github.com/firebase/functions-samples/blob/master/child-count/functions/index.js
Update January 2018
The firebase docs have changed so instead of event we now have change and context.
The given example throws an error complaining that event.data is undefined. This pattern seems to work better:
exports.countPrescriptions = functions.database.ref(`/prescriptions`).onWrite((change, context) => {
const data = change.after.val();
const count = Object.keys(data).length;
return change.after.ref.child('_count').set(count);
});
```
This is a little late in the game as several others have already answered nicely, but I'll share how I might implement it.
This hinges on the fact that the Firebase REST API offers a shallow=true parameter.
Assume you have a post object and each one can have a number of comments:
{
"posts": {
"$postKey": {
"comments": {
...
}
}
}
}
You obviously don't want to fetch all of the comments, just the number of comments.
Assuming you have the key for a post, you can send a GET request to
https://yourapp.firebaseio.com/posts/[the post key]/comments?shallow=true.
This will return an object of key-value pairs, where each key is the key of a comment and its value is true:
{
"comment1key": true,
"comment2key": true,
...,
"comment9999key": true
}
The size of this response is much smaller than requesting the equivalent data, and now you can calculate the number of keys in the response to find your value (e.g. commentCount = Object.keys(result).length).
This may not completely solve your problem, as you are still calculating the number of keys returned, and you can't necessarily subscribe to the value as it changes, but it does greatly reduce the size of the returned data without requiring any changes to your schema.
Save the count as you go - and use validation to enforce it. I hacked this together - for keeping a count of unique votes and counts which keeps coming up!. But this time I have tested my suggestion! (notwithstanding cut/paste errors!).
The 'trick' here is to use the node priority to as the vote count...
The data is:
vote/$issueBeingVotedOn/user/$uniqueIdOfVoter = thisVotesCount, priority=thisVotesCount
vote/$issueBeingVotedOn/count = 'user/'+$idOfLastVoter, priority=CountofLastVote
,"vote": {
".read" : true
,".write" : true
,"$issue" : {
"user" : {
"$user" : {
".validate" : "!data.exists() &&
newData.val()==data.parent().parent().child('count').getPriority()+1 &&
newData.val()==newData.GetPriority()"
user can only vote once && count must be one higher than current count && data value must be same as priority.
}
}
,"count" : {
".validate" : "data.parent().child(newData.val()).val()==newData.getPriority() &&
newData.getPriority()==data.getPriority()+1 "
}
count (last voter really) - vote must exist and its count equal newcount, && newcount (priority) can only go up by one.
}
}
Test script to add 10 votes by different users (for this example, id's faked, should user auth.uid in production). Count down by (i--) 10 to see validation fail.
<script src='https://cdn.firebase.com/v0/firebase.js'></script>
<script>
window.fb = new Firebase('https:...vote/iss1/');
window.fb.child('count').once('value', function (dss) {
votes = dss.getPriority();
for (var i=1;i<10;i++) vote(dss,i+votes);
} );
function vote(dss,count)
{
var user='user/zz' + count; // replace with auth.id or whatever
window.fb.child(user).setWithPriority(count,count);
window.fb.child('count').setWithPriority(user,count);
}
</script>
The 'risk' here is that a vote is cast, but the count not updated (haking or script failure). This is why the votes have a unique 'priority' - the script should really start by ensuring that there is no vote with priority higher than the current count, if there is it should complete that transaction before doing its own - get your clients to clean up for you :)
The count needs to be initialised with a priority before you start - forge doesn't let you do this, so a stub script is needed (before the validation is active!).
write a cloud function to and update the node count.
// below function to get the given node count.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.userscount = functions.database.ref('/users/')
.onWrite(event => {
console.log('users number : ', event.data.numChildren());
return event.data.ref.parent.child('count/users').set(event.data.numChildren());
});
Refer :https://firebase.google.com/docs/functions/database-events
root--|
|-users ( this node contains all users list)
|
|-count
|-userscount :
(this node added dynamically by cloud function with the user count)
Some background on the issue. From the front end, I have two select boxes with the multiple property. One box is for approved items, the other is for ignored items. I place these into a Map with the key being the company's UID, and the value either being "Y" or "N" depending on which box the UID is in. Inserting HashMap Values to a table using ibatis provided some assistance, but the answer involved manually putting the entries, where as I am dynamically creating the map, so not sure what the keys will be. Below is the code for the Java:
// Set up the map object for the back end
Map<Integer, String> posMap = new HashMap<Integer, String>();
// Get the approved mailers
String[] mailerList = request.getParameterValues("approved");
if (mailerList != null && mailerList.length > 0)
{
for(String mailer : mailerList)
{
posMap.put(Integer.parseInt(mailer), "Y");
}
}
// reset the mailerList
mailerList = null;
// get the ignored mailers
mailerList = request.getParameterValues("ignored");
if (mailerList != null && mailerList.length > 0)
{
for(String mailer : mailerList)
{
posSampleMap.put(Integer.parseInt(mailer), "N");
}
}
// only update POS if the map is not empty
if(!posMap.isEmpty())
{
updateMapper.updatePOSSampling(posMap);
}
Normally, I would have something like this in the mapper.xml file:
<update id="updatePOSSampling" parameterType="hashmap">
UPDATE <table_name>
SET sampling_enabled = ${myMapValue}
WHERE mailer_name = ${myMapKey}
</update>
In the link I provided, they were manually putting in the keys and values, and as such, the example IBATIS could refer to the key. Since I'm not sure what my key is, what would be the best way to generate this query? I've got a temporary workaround of sending a two dimension array, but I feel using a Map DO would be a better way. Thanks in advance for any assistance.