solrj getCountDistinct() return null? - lucene

I am trying to get unique value of a field with the code:
query.set("q","*:*" );
query.setGetFieldStatistics(true);
query.setGetFieldStatistics("popu_s");
QueryResponse rsp = solr.query(query);
FieldStatsInfo stats = rsp.getFieldStatsInfo().get("popu_s");
System.out.println(stats.getCount());
System.out.println(stats.getCountDistinct());
stats.getCount() gives the correct count. However, stats.getCountDistinct() always returns null.
Any idea?

getCountDistinct() in FieldStatsInfo is null because it was not provided when creating the FieldStatsInfo object (what was returned from solr.query(query)).
There is no guarantee that any of the fields in this pojo are populated.
As an alternative try using stats.getCardinality() although again there is no guarantee this is poulated.

Found the solution!
To make getCountDistinct() return a value, one needs to add this
query.addStatsFieldCalcDistinct("popu_s", true);

Related

BigQuery Java API - Cannot Send Null For TableRow Value

I am trying to set values on BigQuery table using java big query api but its throwing NullPointerException (java.lang.NullPointerException: null value in entry: popup=null ) every time I send the null value.
The null value should be completely acceptable since my Mode is NULLABLE on schema itself. I have bunch of other fields that have null value on them.
Any suggestion on this issue would be really helpful for me, I am stuck no where due to this.
Note : I may ignore and not set those fields having null values on them but that is not the solution I am looking for. My piece of code is below:
TableRow row = new TableRow();
row.set("ip", "test");
row.set("popup", null);
Don't explicitly set the value to null. Simply ignore it. If it's not present in the payload to BigQuery, it will be set to null. You will not be able to set it to null anyway, because the API is checking for null parameters, and you can't change that behaviour.
So, do this instead:
TableRow row = new TableRow();
row.set("ip", "test");

Can't retrieve db Model getId() anymore in activejdbc 1.4.12

I was using activejdbc 1.4.9 and the following sample code was running just fine
Client client = new Client();
client.save();
Assert.assertNotNull(client.getId());
Since I upgraded to 1.4.12, client.getId() is always returning null when save is inserting a new record. i.e. id is not getting refreshed.
Did anyone notice this as well? Do I have to do anything different using this version to get the newly created id?
I cannot confirm this with the version 1.4.12. For instance, I wrote this example: https://github.com/javalite/simple-example/blob/new_id. Check out code in the Main.java. As you can see, the code is identical to yours, but on line 21, it prints out a real value of the new ID.
If you can put together a simple example that replicates your issue, I will take a look.
EDIT:
Now that you provided more info in comments below, the problem is with you setting the ID to empty string: "". Because the ID is not null anymore, the method save() uses update rather than insert. The update then uses the value of ID to update an "existing" record, and, as a result does not do anything. Messing with ID value is possible but not advised. Please see this for more information: http://javalite.io/surrogate_primary_keys

Why Term.Text returns invalid data for Numericfields in Lucene.Net even though it supposed to convert accrodingly?

I was trying to return all values in order to use them later for facets as following:
TermEnum termsEnum = reader.Terms(new Term(groupByField, string.Empty));
But as soon as I added a filed like this:
NumericField tempNumericField = new NumericField("price", Field.Store.YES, true);
Term.Text started to return wrong data for the price field.
Is there a way to return all date for both Field and NumericFields?
NumericFields are stored in an encoded form (allows for correct ordering, ranges etc).
Try using NumericUtils.PrefixCodedToInt (or the appropriate method for long etc)

django "use_natural_foreign_keys=True" issue

I currently use the well documented "use_natural_foreign_keys=True" to return the relevant field data required instead of the id:
all_orders = Orders.objects.all()
resp = serializers.serialize('json', all_orders, use_natural_foreign_keys=True)
What I don't know how to do is return both the id AND the field data required as typically returned by the "use of use_natural_foreign_keys=True".
Anyone know of a quick fix to return both?
Many thanks, Alan.
define a "natural_key" method in your model class, whose id and field_name you like to get. e.g
def natural_key(self):
return (self.id, self.field_name)

Rails 3 after_save old value

How do you work with the old values of a record being updated?
For instance in the following code block how would I run a query using the previous winner_id field after I determine that it has indeed changed?
if self.winner_id_changed?
old_value = self.changed_attributes
User.find(old_value)
#do stuff with the old winner....
end
An example output of self.changed_attributes would be:
{"winner_id"=>6}
Do I really have to convert this to a string and parse out the value in order to perform a query on it? old_value[:winner_id] doesn't seem to do anything.
Use where instead of find, and the following inject method on changes to generate the desired hash:
if self.winner_id_changed?
old_value = self.changes.inject({}) { |h, (k,v)| h[k] = v.first }
old_user = User.where(old_value)
#do stuff with the old user....
end
You can also use ActiveRecord dirty methods such as:
self.winner_id_was
to get specific attribute's old value. Full documentation may be found here.