I have a class Restaurant:
Restaurant(id: integer, name: string, url: string, address: string
I would like to get all the different combinations of url and name and count them. How can I do that?
I've tried
Restaurant.select(:url, :name).distinct.count
but I get:
No function matches the given name and argument types. You might need to add explicit type casts.
Did you try?
Restaurant.pluck(:url, :name).uniq.count
More here http://api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-pluck
And also check this question Rails: select unique values from a column
Related
I'm working in F# with a CSV that looks like this:
When,Where,HowMuch
11/24/2019,Germany,100 EUR
11/25/2019,France,100 EUR
11/26/2019,Switzerland,50 CHF
11/27/2019,USA,75 USD
I'm using the CSV Type Provider in the FSharp.Data package to parse this data.
type CurrencyDetector = CsvProvider<"Currencies.csv">
Obviously the first column is a date, and the second is a string.
For the third, I'd like to use this type:
type Money (amountAndDenomination : string) =
let parts = amountAndDenomination.Split ' '
member __.Amount = Decimal.Parse parts.[0]
member __.Denomination = parts.[1]
I've tried a few permutations of the Schema argument in my CsvProvider line, but nothing has worked so far. For example:
type CurrencyDetector = CsvProvider<"Currencies.csv",Schema="When (date),Where (string),HowMuch (Money)">
When comes out as DateTime and Where as string, but HowMuch becomes a string property named HowMuch (Money):
Is there a way to use my own classes with the CsvProvider, or is this just not possible?
According to the documentation for the CsvProvider, I don't think it is possible:
Schema parameter: "Optional column types, in a comma separated list. Valid types are int, int64, bool, float, decimal, date, guid, string, int?, int64?, bool?, float?, decimal?, date?, guid?, int option, int64 option, bool option, float option, decimal option, date option, guid option and string option. You can also specify a unit and the name of the column like this: Name (type<\unit>), or you can override only the name. If you don't want to specify all the columns, you can reference the columns by name like this: ColumnName=type."
Note in the above, however, that there is the possibility of using units of measure. So you might explore creating units of measure for the currency denominations. The type provider might require a different format for that column though.
In the documentation for the CSV Type Provider, you can find further information about units of measure and also how to "transform the columns themselves by using Map", which should enable you to map the string type to a custom type. Looking at the source code, suggests that the Rows collection is a sequence of Row objects, each of which is a tuple. So you should be able to use Seq.map and/or any other function in the Seq module to post-process the generated collection.
I have a index named product in elastic-search with field id, name etc..
I want to search products by id or name but my id field is integer and name is a text, I tried following but getting error while searching by name.
error type":"number_format_exception","reason":"For input string:
\"test\""
def self.search_by_name_or_id(query)
__elasticsearch__.search({
query: {
bool: {
must:{
multi_match: {
query: query,
fields: ["id", "name"]
}
}
}
}
})
end
Problem
Exception is clear, that you are trying to search test which is a String in the integer field and elasticsearch is not able to convert test in integer, instead of test if you had search for 10 or 100, then elasticsearch would convert it into integer and would not throw the exception.
Solution
You are trying to mix 2 things here, I am not sure about your design but if your id field can contain pure numbers i.e. integers, then it's not possible to achieve in a single query the way you are doing.
If you can convert your id field to String, then multi_match query would work perfectly fine, otherwise, you need to first check in your application, that search term can be converted to number or not, i.e. 10 or 100 would work fine but test10 or test100 would not and anyway there is no point of searching these terms in id field as it won't be present as its defined as integer in ES and ES would reject documents containing these terms during indexing time only. So based on your application code check you can construct the ES query which may or may not include the id field in multi-match.
I have a list of U.S. states where duplicate values are also available. I need to find how many unique states are there in a list.
'Unique' function returns all the unique states but in conjunction with 'query' function the following error message is returned:
Unable to parse query string for Function QUERY parameter 2: NO_COLUMN: A
=query(unique(A3:A26),"Select count(A)")
What am I doing wrong?
Having reconstructed the data (after UNIQUE it is different) you can't then continue with letters for column references. So:
=query(unique(A3:A26),"Select count(Col1)")
I am seeing Rails generate the wrong query value when I use an enum col value in a where clause, like this (self added for clarity). dominant_product_strategy is an enum.
def some_model_method_on_myModel
MyModel.where(dominant_product_strategy: self.dominant_product_strategy)
end
This produces the correct value (again, self just added for clarity):
MyModel.where(dominant_product_strategy: self.attributes["dominant_product_strategy"])
I'm guessing that Rails sees the enum as a string, and then converts to a integer value of zero. Ughhhhh!
Am I missing something?
This also works:
MyModelwhere(dominant_product_strategy: MyModel.dominant_product_strategies[dominant_product_strategy])
It seems to be that you have answered your question by yoursef. Enum variables is a hash:
{str1: int1, str2: int2, ...}
The value (integer) is storing in DB, and the string is just representation of the int value. When you call self.dominant_product_strategy, you get the representation (string) of dominant_product_strategy column storing as integer in DB.
I think that your first working solution (self.attributes["dominant_product_strategy"]) is fine.
Domain (just simple example):
class House {
String address
String region
Long price
static searchable = { only = ['address', 'price', 'region'] }
}
I want to search by address with price selector
Search query is:
"${address} AND price: [100 TO 1000]"
But if address='500' in search result will be Houses with price 500. I need to find house for address only by House.address + House.region
If I understood, you want to use the value of ${address} to query only on the address and region attributes.
In that case, based on the "complex" query string example at http://grails.org/Searchable+Plugin+-+Searching+-+String+Queries, your query string could be:
"+(address:${address} OR region:${address}) price:[100 TO 1000]"
Matches must have a ${address} value for address or a ${address} value for region, and have a value from 100 to 1000 for price.
Note: your input variable has the same name of an entity attribute, "address", wich can cause some confusion when trying to understand query strings using them both. Though not needed, you might want to change that, for easier readable query strings.