With sunspot-rails, how do I conditionally index specific fields? - solr

I would like to add a conditionally index some data inside the sunspot 'searchable' method in my model. Ideally it would look something like this:
searchable do
string :important_text
if address_visible?
string :address
end
end
In the above example, I would like to index the address field only if the address_visible? method (on the model) returns true. Unfortunately, the address_visible? method throws a 'NoMethodError' because the context is now a Sunspot::DSL::Fields, not the model.

I don't think you can actually do exactly what you want. Nonetheless you could index a different value for address when address is not visible. For example:
searchable do
string :important_text
string :address { |model| model.address_visible? ? model.address : '' }
end

Related

Print value of mapped data

I have a question I'm sure it's something easy but I don't find solution.
I'm mapping an array of objects with objects looking like this:
{
categoryFr: "Commande",
categoryEn: "Order"
}
Now I want to to print this data in a div so if I do this, it works I get Commande:
<div>{categoryFr}</div>
But I want to dynamically render accordingly to the language I tried :
<div>`c.name${lang}`</div>
<div>`${c.name}${lang}`</div>
But the string categoryFr is printed instead of the value Commande.
you can access it with something like:
<div>{c[name + lang]}</div>
since obj.property (obj.categoryFr) is the equivalent with obj[property] (obj["category" + "Fr"])

Rails update remove number from an array attribute?

Is there a way to remove a number from an attibute array in an update? For example, if I want to update all of an alchy's booze stashes if he runs out of a particular type of booze:
Alchy has_many :stashes
Stash.available_booze_types = [] (filled with booze.ids)
Booze is also a class
#booze.id = 7
if #booze.is_all_gone
#alchy.stashes.update(available_booze_types: "remove #booze.id")
end
update: #booze.id may or may not be present in the available_booze_types array
... so if #booze.id was in any of the Alchy.stash instances (in the available_booze_types attribute array), it would be removed.
I think you can do what you want in the following way:
if #booze.is_all_gone
#alchy.stashes.each do |stash|
stash.available_booze_types.delete(#booze.id)
end
end
However, it looks to me like there are better ways to do what you are trying to do. Rails gives you something like that array by using relations. Also, the data in the array will be lost if you reset the app (if as I understand available_booze_types is an attribute which is not stored in a database). If your application is correctly set up (an stash has many boozes), an scope like the following in Stash class seems to me like the correct approach:
scope :available_boozes, -> { joins(:boozes).where("number > ?", 0) }
You can use it in the following way:
#alchy.stashes.available_boozes
which would only return the ones that are available.

kotlin "contains" doesn't work as expected

I am working with a method which filters the preferences which match with the ids, I am using the contains method, but even though the values are the same, the contains is showing a false in each iteration. The method looks like:
private fun filterPreferencesByIds(context: MyPodCastPresenterContext): List<FanPreferences> {
return context.preferences?.filter {
context.ids.contains(it.id)
}
}
The values of the arrays are:
for the context.ids:
"B52594F5-80A4-4B18-B5E2-8F7B12E92958" and "3998EDE7-F84B-4F02-8E15-65F535080100"
And for the context.preferences:
But even though, when the first and the final ids have the same id value as the context.ids, the contains is false in the debug. I think it could be related with the types in the context.ids rows Json$JsonTextNode. Because when I did the same with numeric values hardcoded the compare is successful.
Any ideas?
Thanks!
If the type of FanPreferences.id is String and the type of context.ids list element is JsonTextNode, you won't find an element equal to the given id string, because it's not of String type.
Try mapping your context ids to the list of strings before filtering:
val ids = context.ids.map { it.toString() }.toSet()
return context.preferences?.filter {
ids.contains(it.id)
}
Note that calling toString() on JsonTextNode might be not the best way to get the string data from it. It's better to consult the API documentation of that class to find it out.

Getting null value when accessing fields from result in Searcher in Vespa

My Searcher is given below. When I hit search API then in my Searcher, I am getting null values when I tried to get fields of document. I am able to get id, relavance and source but not fields. Why is it so?Am I doing something wrong? Please help.
#Override
public Result search(Query query, Execution execution) {
// pass it down the chain to get a result
Result result = execution.search(query);
String title = result.hits().get(0).getField("title");
System.out.println(title);
// return the result up the chain
return result;
}
I am getting null value in title.
This is because results are initially surfaced without field data added (for performance).
Add
execution.fill(result);
before accessing fields (send the summary class to fill as second argument if you have multiple ones).
Use execution.fill(result) to fill the result before accessing fields. See also https://docs.vespa.ai/documentation/reference/inspecting-structured-data.html

GoogleAppEngine - query with some custom filter

I am quite new with appEnginy and objectify. However I need to fetch a single row from db to get some value from it. I tried to fetch element by ofy().load().type(Branch.class).filter("parent_branch_id", 0).first() but the result is FirstRef(null). However though when I run following loop:
for(Branch b : ofy().load().type(Branch.class).list()) {
System.out.println(b.id +". "+b.tree_label+" - parent is " +b.parent_branch_id);
};
What do I do wrong?
[edit]
Ofcourse Branch is a database entity, if it matters parent_branch_id is of type long.
If you want a Branch as the result of your request, I think you miss a .now():
Branch branch = ofy().load().type(Branch.class).filter("parent_branch_id", 0).first().now();
It sounds like you don't have an #Index annotation on your parent_branch_id property. When you do ofy().load().type(Branch.class).list(), Objectify is effectively doing a batch get by kind (like doing Query("Branch") with the low-level API) so it doesn't need the property indexes. As soon as you add a filter(), it uses a query.
Assuming you are using Objectify 4, properties are not indexed by default. You can index all the properties in your entity by adding an #Index annotation to the class. The annotation reference provides useful info.
Example from the Objectify API reference:
LoadResult<Thing> th = ofy.load().type(Thing.class).filter("foo", foo).first();
Thing th = ofy.load().type(Thing.class).filter("foo", foo).first().now();
So you need to make sure member "foo" has an #Index and use the now() to fetch the first element. This will return a null if no element is found.
May be "parent_branch_id"in your case is a long, in which case the value must be 0L and not 0.

Resources