How to handle results when make a search - javascript

I've learning how to play with search results using Google Image API at this URL http://code.google.com/apis/imagesearch/v1/reference.html#resultobject. I need to get "tbUrl" & "unescapedUrl" values as described in section Handling search results: Result properties but didn't know how to and can't find any example to get this working. Also I'm trying play with search results in this page http://reyner.subdivx.com/prueba.php. Can any help me to get this values?
Cheers and thanks in advance

Modify your bind_event callback function to have the following signature:
function bind_event(sc, searcher){}
Then, you can access the properties by calling:
searcher.results[0].tbUrl;
searcher.results[0].unescapedurl;
Substituting 0 for whatever iterative number you're on when you loop the searcher.results collection.
http://code.google.com/apis/websearch/docs/

Related

How to retrieve the value of the first field in an object without its name?

I am using the REST Countries API to retrieve data from different countries, the point is to generate a HTML code to insert, depending on the country data.
But as I'm trying to point the language data in my HTML code, it of course returns me an object.
Here are two examples of the data from the API:
languages: {cat: 'Catalan'}
languages: {fra: 'French'}
The line of code in my HTML:
<p class="country__row"><span>🗣️</span>${data.languages}</p>
I would like to retrieve the values of the first field in those objects (Catalan or French) without having to, of course, write ${data.languages.fra} in my HTML code.
Thank you for your help.
I tried ${data.languages[0]} and ${data.languages[0].value}, doesn't work.
You can use the Object.keys() function to get the list of keys from an object.
There are other similar functions as well that you can use such as Object.values() or Object.entries().
As far as your use case is concerned try any one of the solutions mentioned below:
${data.languages[Object.keys(data.languages)[0]]}
// The `0` index can be replaced with i if using a loop.
${Object.values(data.languages)[0]}
You can write as Object.values(data.languages) which would give you values of object.

Algolia Search api returns maximum 1000 records while my total records are around 50000

My index in Algolia contains around 50k records. While using javascript api for retrieving records it only returns maximum 1000 records, i am using search() function.
Any suggestions?
search() function by default returns only 1000 records. If in case you need to retrieve more than 1000 records you should use browse() function instead. However search function is more optimised then browse function.
Always prefer search() over browse unless you really want to retrieve all your records(for backup purpose).
Reference to API doc: Browse All Index Content
Actually, Algolia doesn't return more than 1000 result. However, the response contains a cursor that you can use to access the next elements with the "browseFrom" function. You probably want to use the browseAll function instead which lets you access all the elements sequentially.
The search() function is, indeed, only able to retrieve you 1000 results at max. Most of the time, you don't need to retrieve so many results because you probably just want to display a few of them.
However, if you really need to access all your records, for backup or anything else, you should use the browse() function which does exactly that. :)
More informations here on this FAQ page

Google Custom Search API javascript

I'm writing an AngularJS application which should perform image search through entire web using google custom search api.
I've used the query as follows:
'https://www.googleapis.com/customsearch/v1?key='+ googleApiKey + '&cx=014766414461901118935:knbecyxzx4u&searchType=image&startIndex=41&imgType=photo&q=' + query;
As a result I get an array which consists of 10 elements. The documentation says that it can return up to the first 100 results. How can I load all the rest of results beside those 10 which come from the query above?
Note: Besides the array of images I also get the query object which has the nextPage object which holds the metadata describing the query to use for the next page of results, but apparently I have no idea how to use it as the startIndex value is always 11
I believe that you use &start instead of &startIndex

List of autocomplete items in Dojo FilteringSelect

I have a typical FilteringSelect that works when I type in it and I can retrieve the value from the box, which is good. However, I'm trying to determine if I can access to that list of items that it returns.
For instance, if I have the following items in my store:
apple
axe
bananna
And I type in 'a' I want to get the an array that gets me 'apple' and 'axe'.
I'm assuming this will go somewhere in the onKeyPress: function , I'm just not familiar enough with the documentation. I've looked into dijit.byId('selectId') but from there I just don't know the API/documentation well enough
You should look at the API Documentation. There you can see a summary of all events possible. When you read it, you should come to the event called onSearch which returns 3 parameters:
the query
the results
some options
So what you want is the onSearch event and read the results parameter. I made a JSFiddle to show you an example.
Another possible solution is to query the store directly, which can be useful if you don't need the FilteringSelect, but if you just want to get a list of items based on a query. You can also see how that works in my JSFiddle.
EDIT: I Just noticed that you can't access the API documentation. You should really try another browser then, since the API documentation contains a lot of interesting things and is usually the reference for events/methods and properties.
I would have a look at the following properties from the Dojo API page:
https://dojotoolkit.org/api/ (click on dijit/form/FilteringSelect)
query
Defined by dijit/form/_SearchMixin
A query that can be passed to store to initially filter the items. ComboBox overwrites any reference to the searchAttr and sets it to the queryExpr with the user's input substituted.
queryExpr
Defined by dijit/form/_SearchMixin
This specifies what query is sent to the data store, based on what the user has typed. Changing this expression will modify whether the results are only exact matches, a "starting with" match, etc. dojo.data query expression pattern. ${0} will be substituted for the user text. * is used for wildcards. ${0}* means "starts with", ${0} means "contains", ${0} means "is"
searchAttr
Defined by dijit/form/_SearchMixin
Search for items in the data store where this attribute (in the item) matches what the user typed
For example (Haven't tried this so not sure if it will work or not):
* Use attr to retrieve or set dojo properties.
var srchItems = dijit.byId('resistForm').attr("searchAttr","a");

Auto Complete does not filter results when I'm typing

I have very simple form that gets values in JSON format from searchAlbum.php. It works when I start typing something in, but it does not filter results, for example it shows 123 as available even though I typed ab.
This is what my saerchAlbum.php is returning
["123","abc"]
This is my Java Script code
$(document).ready(function(){
$('.albumName').autocomplete({
source: 'searchAlbum.php'
});
});
You might say that it should not filter my resoulds and I need to pass my input as a paramater but why then this examle on jquery-ui page does that for me?
The documentation isn't clear about it, but the only time the autocompleter does the filtering for you is when your code isn't getting called at all (e.g., you've given it an array as source). When your code is getting called (either client-side code because you've given a function for source, or server-side code because you've given a URL), your code is expected to do the filtering.
You might say that it should not filter my resoulds and I need to pass my input as a paramater but why then this examle on jquery-ui page does that for me?
Because the search.php page that the example calls filters the results based on the term parameter the autocompleter passes it. Compare the results you get from these:
http://jqueryui.com/demos/autocomplete/search.php?term=ti
http://jqueryui.com/demos/autocomplete/search.php?term=ro
You can see that it's filtering server-side.
The js sends a query string parameter named "term", your php code needs to return data by filtering existing data that match the "term" parameter.
This isn't a Javascript or jQuery problem, but a PHP problem. As mentioned in the linked jQuery-UI page, the source script must process the "term" property via a GET request.

Categories

Resources