I am using the gem amazon_product for searching the books in Amazon.
The search is perfectly fine but it gets me only a list of first 10 books.
I want to get all the search results and paginate them. How can I do this?
My code looks like this,
req = AmazonProduct["us"]
req.configure do |c|
c.key = "KEY"
c.secret = "SECRET_KEY"
c.tag = "TAG"
end
resp = req.search("Books", :power => params[:book][:search_term], :sort => "reviewrank")
#books = resp.to_hash["Items"]["Item"]
From their API page at - http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl
They have "RelatedItemPage" and "ItemPage"
You should give this a try
resp = req.search("Books", :power => params[:book][:search_term], :itemPage => 20)
Hope this helps.
Related
Trying to get the bulk percolate functionality to work for Elasticsearch-py (i.e. mpercolate), but haven't been able to find an example online. I'm able to use the percolate function, so I can get this to work:
doc = {'doc' : {'field1' : 'this is a value', 'field2' : 'this is another value'}}
res = es.percolate(index = 'my_index', doc_type = 'my_doc_type', body = doc)
The documentation I've read so far seems to imply that if I want to do a bulk submission, I need to send header and body as strings, separated by a newline. Thus I've tried:
head = {'percolate' : {'index' : 'my_index', 'type' : 'my_doc_type'}}
doc = {'doc' : {'field1' : 'this is a value', 'field2' : 'this is another value'}}
doc2 = {'doc' : {'field1' : 'values and values', 'field2' : 'billions of values'}}
query_list = [head, doc, head, doc2]
my_body = '\n'.join([str(qry) for qry in query_list])
res = es.mpercolate(body = my_body)
which gives me a generic "elasticsearch.exceptions.TransportError". Anyone have a working example I can adapt?
You don't have to serialize the data yourself, just pass in the query_list as body and it should do the right thing
I have got unique document ids (across all types) and I would like to check which document already exists in elasticsearch index. I try to search
var duplicateCheck = _elasticClient
.MultiGet(m => m.GetMany<object>(notices.Select(s => s.Id)).Fields("Id"));
but it returns wrong result - every document has set found property to false.
update
there is workaround here
var exisitngDocIds = _elasticClient.Search<ZPBase>(s => s
.AllTypes()
.Query(q => q.Ids(notices.Select(z=>z.Id)))
.Fields("Id")
.Take(notices.Count)
);
notices = notices.Where(q => !exisitngDocIds.Hits.Any(s => s.Id == q.Id)).ToList();
From the Multi Get API documentation I realized that you can use something similar to the following code to solve your problem:
var response = _elasticClient.MultiGet(m => m
.Index(MyIndex)
.Type("")
.GetMany<ZPBase>(noticeIds));
Note the empty string passed as the Type.
I have certain documents with a name: String and a version: Integer.
What I need is a list of documents of the highest version per name.
So I think I need to do the equivalent of group by in sql and then a having for max version per name.
I have no idea where to start to do this with mongoDB. If anyone could make this query for the mongo terminal that would be a great start, but an added bonus would be to give the sytnax for MongoMapper specifically.
If you are on Mongodb version 2.2+, you can do your query by using the aggregation framework with the group pipeline operator.
The documentation is here: http://docs.mongodb.org/manual/reference/aggregation/
MongoMapper doesn't have a helper for the aggregation framework but you can use the Ruby driver directly (driver version 1.7.0+ has an aggregate helper method). You would have to get an instance of Mongo::Collection and call the aggregate method on it. For example:
Model.collection.aggregate(["$group" =>
{"_id" => "$name",
"max_version" => {"$max" => "$version"}}
])
I hope that helps!
If you want to do a group by with Mongo DB, check the Aggregation Framework, it is the exact tool for the job !
Here you'll find the equivalent in Aggregation Framework for GROUP BY, HAVING, and more.
Thanks to #Simon I had a look at Map Reduce with MongoMapper. My take on it is probably not perfect, but it does what I want it to do. Here's the implementation:
class ChildTemplate
...
key :name, String
key :version, Integer, :default => 1
...
private
def self.map
<<-JS
function() {
emit(this.name, this);
}
JS
end
private
def self.reduce
<<-JS
function(key, values) {
var res = values[0];
for(var i=1; i<values.length; i++)
{
if(values[i].version > res.version)
{
res = values[i];
}
}
return res;
}
end
def self.latest_versions(opts = {})
results = []
opts[:out] = "ct_latest_versions"
ChildTemplate.collection.map_reduce(map, reduce, opts).find().each do |map_hash|
results << map_hash["value"]
end
return results
end
I'm using Facebook's fql.multiquery for a number of different things and I'm getting the results back - just as the doctor ordered. But I can't seem to wrap my head around how to join the results so I can give them back to the user in the way I want.
I'm using HTTParty to call this:
checkin_query = {
"user_checkins" => "SELECT timestamp, coords, tagged_uids, page_id, post_id, checkin_id, message FROM checkin WHERE author_uid= me()",
"location_names" => "SELECT name FROM page WHERE page_id IN (SELECT page_id FROM #user_checkins)",
"friends_with" => "SELECT uid, name, pic_square FROM user WHERE uid IN (select tagged_uids FROM #user_checkins)"
}
params = {:queries => checkin_query.to_json, :access_token => user.token, :format => :json}
#checkin_info = HTTParty.get "https://api.facebook.com/method/fql.multiquery", :query => params
Which gives me what I want... but it's in 3 separate arrays/hashes (?) and for some reason, my brain isn't coming to a conclusion on how to deal with it.
So basically if I wanted to show the user "OK dude, you were at *LOCATION_NAME* with these people: *FRIENDS_WITH*
How can I take those 3 separate result sets and match everything up? Is there a way to include something from a previous query to call back and match it so I can display everything? Some check-ins have no tagged_uids, so obviously just doing it in order won't work when it comes to the friends they're with.
Google searches have shown me SQL's JOIN (which seems like it may be a solution) - but isn't available in FQL.
I have no experience with either FQL or HTTParty so I might have this completely wrong.
I tend to format the sql as follows:
checkin_query = {
"user_checkins" => "
SELECT tagged_uids, page_id
FROM checkin
WHERE author_uid = me()
",
"location_names" => "
SELECT name
FROM page
WHERE page_id IN (
SELECT page_id
FROM #user_checkins
)
",
"friends_with" => "
SELECT name
FROM user
WHERE uid IN (
SELECT tagged_uids
FROM #user_checkins
)
"
}
location_names and friends_with are probably returned as arrays of hashes:
"friends_with" => [{:name => 'John'}, {:name => 'Sue'}]
Not sure what HTTParty returns, but if it is hashes of arrays of hashes you might try:
puts "OK dude, you were at #{ #checkin_info[:location_names].map { |e| e.values } } with these people: #{ #checkin_info[:friends_with].map { |e| e.values } }
To figure out what #checkin_info returns you could try this:
puts #checkin_info.to_yaml
I am trying to use LINQ to create a Dictionary<string, List<CustomObject>> from a List<CustomObject>. I can get this to work using "var", but I don't want to use anonymous types. Here is what I have
var x = (from CustomObject o in ListOfCustomObjects
group o by o.PropertyName into t
select t.ToList());
I have also tried using Cast<>() from the LINQ library once I have x, but I get compile problems to the effect of it being an invalid cast.
Dictionary<string, List<CustomObject>> myDictionary = ListOfCustomObjects
.GroupBy(o => o.PropertyName)
.ToDictionary(g => g.Key, g => g.ToList());
I cannot comment on #Michael Blackburn, but I guess you got the downvote because the GroupBy is not necessary in this case.
Use it like:
var lookupOfCustomObjects = listOfCustomObjects.ToLookup(o=>o.PropertyName);
var listWithAllCustomObjectsWithPropertyName = lookupOfCustomObjects[propertyName]
Additionally, I've seen this perform way better than when using GroupBy().ToDictionary().
For #atari2600, this is what the answer would look like using ToLookup in lambda syntax:
var x = listOfCustomObjects
.GroupBy(o => o.PropertyName)
.ToLookup(customObject => customObject);
Basically, it takes the IGrouping and materializes it for you into a dictionary of lists, with the values of PropertyName as the key.
This might help you if you to Get a Count of words. if you want a key and a list of items just modify the code to have the value be group.ToList()
var s1 = "the best italian resturant enjoy the best pasta";
var D1Count = s1.ToLower().Split(' ').GroupBy(e => e).Select(group => new { key = group.Key, value = group.Count() }).ToDictionary(e => e.key, z => z.value);
//show the results
Console.WriteLine(D1Count["the"]);
foreach (var item in D1Count)
{
Console.WriteLine(item.Key +" "+ item.Value);
}
The following worked for me.
var temp = ctx.Set<DbTable>()
.GroupBy(g => new { g.id })
.ToDictionary(d => d.Key.id);