Google Custom Search API in C#: Paging - c#

This is my code for query.
String query = 'abc';
CustomsearchService customSearchService = new CustomsearchService(new Google.Apis.Services.BaseClientService.Initializer() { ApiKey = apiKey });
Google.Apis.Customsearch.v1.CseResource.ListRequest listRequest = customSearchService.Cse.List(query);
listRequest.Cx = searchEngineId;
search_results = listRequest.Execute();
How can I add options like filtering here?
I searched whole internet and github but I couldn't find any sample code or proper documentation.

I changed the code like this and it works.
CustomsearchService customSearchService = new CustomsearchService(new Google.Apis.Services.BaseClientService.Initializer() { ApiKey = apiKey });
Google.Apis.Customsearch.v1.CseResource.ListRequest listRequest = customSearchService.Cse.List(query);
listRequest.Cx = searchEngineId;
listRequest.Start = 20;
listRequest.Num = 5;
search_results = listRequest.Execute();
But it generates error if listRequest.Start = 100 or listRequest.Num = 12

If the question is like in title about paging then CseResource.ListRequest listRequest has property Start - there you can specify from which result you want to get data (keep in mind only 10 results per query are returned).
So first time you execute it without Start set, and every sub-call to execute should increase Start by 10.
If additional filters - also listRequest contains properties like i.e. FileType, DateRestrict etc.
EDIT:
I'm using this page:
https://developers.google.com/custom-search/json-api/v1/reference/cse/list

Related

how to get the index/ order of a defect in the Backlog, Rally Rest API C#

Task:
List all defects in the backlog of a project by their rank/index.
Here is my code:
var myRequest = new Request()
{
ArtifactName = "defect",
Limit = 2000,
Query = new Query("Project.OID", Query.Operator.Equals, MyDefectProjectOID),
Fetch = new List<string>() { "true" }
};
QueryResult queryMyResult = api.Query(myRequest);
Question:
1) How do I get the result set back in the order my users have organised them in Rally.
2) Is there a value on the defect item that tells me the rank/index (for example the Task item has a TaskIndex property)
1) Order by the DragAndDropRank field, ASC.
2) As long as you fetch DragAndDropRank as well that's your rank value. It's encoded as a string which is sortable in client code. The overall numeric index will be its index in your result set.
Another quick note- rather than specifying a query on Project.ObjectID to control scoping, you can just set the Project, ProjectScopeUp and ProjectScopeDown values:
Project = "/project/" + MyDefectProjectOID,
ProjectScopeUp = false,
ProjectScopeDown = false

NEST - IndexMany doesn't index my objects

I've used NEST for elasticsearch for a while now and up until now I've used the regular ElasticSearchClient.Index(...) function, but now I want to index many items in a bulk operation.
I found the IndexMany(...) function, but I must do something wrong because nothing is added to the elastic search database as it does with the regular Index(...) function?
Does anyone have any idea?
Thanks in advance!
I found the problem. I had to specifiy the index name in the call to IndexMany
var res = ElasticClient.CreateIndex("pages", i => i.Mappings(m => m.Map<ESPageViewModel>(mm => mm.AutoMap())));
var page = new ESPageViewModel
{
Id = dbPage.Id,
PageId = dbPage.PageId,
Name = dbPage.Name,
Options = pageTags,
CustomerCategoryId = saveTagOptions.CustomerCategoryId,
Link = dbPage.Link,
Price = dbPage.Price
};
var pages = new List<ESPageViewModel>() { page };
var res2 = ElasticClient.IndexManyAsync<ESPageViewModel>(pages, "pages");
This works as expected. Guess I could specify a default index name in the configuration to avoid specifying the index for the IndexMany call.
If you are using C# you should create a list of objects that you want to insert then call the IndexMany function.
Example :
List<Business> businessList = new List<Business>();
#region Fill the business list
...............................
#endregion
if (businessList.Count == 1000) // the size of the bulk.
{
EsClient.IndexMany<Business>(businessList, IndexName);
businessList.Clear();
}
And in the end check again
if (businessList.Count > 0)
{
EsClient.IndexMany<Business>(businessList, IndexName);
}

redis c# client, how do i get Subscribers count?

i need to give statistic about my publisher app
like how many subscribers are there?
i cant seen to get that information from the redis server
i already tried to find in the 'ServiceStack.Redis.RedisSubscription'
i found this:
var channel = ConfigurationManager.AppSettings["redis_channel"];
var _redisClient = new RedisClient("localhost", 6379);
var subscription = _redisClient.CreateSubscription();
//subscription.SubscribeToChannels(channel);
var subscription_count = (int)subscription.SubscriptionCount
but it returning 0 every time.
any ideas?
edit:
i found this http://redis.io/commands/client-list
but steel need some help on how to use it
thanks : )
i got it!
if anyone need that's i did:
var redis_ip = ConfigurationManager.AppSettings["redis_server_ip"];
var redis_port = ConfigurationManager.AppSettings["redis_server_port"];
int redis_port_int = 0;
if (!int.TryParse(redis_port, out redis_port_int))
{
redis_port_int = 6739;
}
RedisNativeClient rnClient = new RedisNativeClient(redis_ip, redis_port_int);
var clientlist_byte = rnClient.ClientList();
var clientlist_string = Encoding.UTF8.GetString(clientlist_byte);
var clientamount_double = clientlist_string.Split("\n".ToCharArray()).Length;
var clientlist_int = (clientamount_double/2) - 1;
return clientlist_int;
the '-1' is to remove my selt from the count,
the /2 it's because after the split i get a doubled amount

Having trouble with a very basic search -- no results

I have Elasticsearch up and running. Using Sense within Marvel, I am able to get a result, with this query:
GET _search
{
"query": {
"query_string": {
"query": "massa"
}
}
}
My c# code, trying to recreate the above:
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node).SetDefaultIndex("mediaitems");
var client = new ElasticClient(settings);
var results = client.Search<stuff>(s => s
.Query(qs => qs.QueryString(q => q.Query("massa"))));
var d = results.Documents;
But unfortunately I'm not getting any results, nothing in "results.Documents". Any suggestions? Maybe a way to see the generated json? What is the simplest way to just query everything in an index? Thanks!
Even though your search results are going to be mapped to the proper type because you are using .Search<stuff>, you still need to set the default type as part of your query.
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node).SetDefaultIndex("mediaitems");
var client = new ElasticClient(settings);
var results = client.Search<stuff>(s => s
.Type("stuff") //or .Type(typeof(stuff)) if you have decorated your stuff class correctly.
.Query(qs => qs.QueryString(q => q.Query("massa"))));
var d = results.Documents;
Additionally, your results response contains a ConnectionStatus property. You can interrogate this property to see the Request and Response to/from Elasticsearch to see if your query is being executed as you expect.
Update: You can also set a default type the index settings as well.
var settings = new ConnectionSettings(node).SetDefualtIndex("mediaitems");
settings.MapDefaultTypeIndices(d=>d.Add(typeof(stuff), "mediaitems");
You can also check nest raw client
var results = client.Raw.SearchPost("mediaitems", "stuff", new
{
query = new
{
query_string = new
{
query = "massa"
}
}
});
You can get the values of search request URL and JSON request body as under:
var requestURL = response.RequestInformation.RequestUrl;
var jsonBody = Encoding.UTF8.GetString(response.RequestInformation.Request);
You can find other useful properties in RequestInformation for debugging.

Google plus API get the Activities List in C#.net librarry

I'm Using the .net Google Plus API and i want to get the list of activities of a specified profile(page/user..).
I have this code which i get it online
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
provider.ClientIdentifier = GoogleIdentifier;
provider.ClientSecret = GoogleSecret;
ActivitiesResource.Collection collection = new ActivitiesResource.Collection();
var service = new PlusService();
service.Key = GoogleKey;
ActivitiesResource.ListRequest list = service.Activities.List(ProfileID, collection);
ActivityFeed activityFeed = list.Fetch();
int count = activityFeed.Items.Count;
on this line:
ActivityFeed activityFeed = list.Fetch();
I get the following error:
An item with the same key has already been added.
The requested keys was invalid. Just Put the right ClientSecret & ClientIdentifier.

Categories

Resources