Specific search with Cloudant and AngularJS - php

I have a search index on my Cloudant that I query using AngularJS and PHP.
So far I'm not getting specific enough results.
For instance, on a search with fair:'Fair 2017', I'm getting all the results that include Fair, including Fair 2016 and so on.
I've tried different search types (simple, standard, classic), and it happens with all of them.
A typical object:
doc:Object
exhibitortype:"Project Space"
fair:"Fair 2017"
...
Here's my AngularJS code:
$scope.loadexhibitors = function(fair){
$scope.searchindex = fair.doc.fairname;
var $promisefairexh=$http({
url: 'databaseconnect/getexhibitors.php',
method: "GET",
params: {search: $scope.searchindex}
});
...
The PHP bit looks like this:
<?php
$search = $_GET["search"];
$newsearch = str_replace(' ', '+', $search);
$url = "https://user:pass.#user.cloudant.com/db/_design/fairs/_search/by_fair?q='$newsearch'&include_docs=true";
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
?>
And my Cloudant search function:
function (doc) {
index("default", doc.fair);
}
On the other hand, on the Cloudant User Interface, when I test the search index and include double quotes on the search input (for example: "Fair 2016" instead of Fair 2016), I get the desired results.
Any tips?

Try using double quotes in your search instead of single quotes, for example:
$url = "https://user:pass.#user.cloudant.com/db/_design/fairs/_search/by_fair?q=\"$newsearch\"&include_docs=true";
Note the change to the q param:
q=\"$newsearch\"

Related

Php Elasticsearch with query parameters

I have a host for Elasticsearch set up like this 199.55.56.289:9998.
What i need is to make a post with Json body but to also add several custom search query's on that host URL so it looks something like this:
199.55.56.289:9998/archive/blog?param1=0&param2=150&param3=0
Is there a way to add those 3 query params in json body when making a post request?
in this case i know that
$query =
[
index: archive,
type: blog
...
]
but what can i do with those uri params?
Elasticsearch supports uri search, So you can use it like this
<?php
$query = 'event:played AND a:b AND #timestamp:c';
$finalUrl = '199.55.56.289:9998/archive/blog?q='.urlencode($query);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$finalUrl);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$output=curl_exec($ch);
curl_close($ch);
echo $output.PHP_EOL;
Check Here: https://www.elastic.co/guide/en/elasticsearch/reference/7.9/search-search.html#search-api-query-params-q

How to use Discogs API to display similarly named artists?

I'm making a website where I'd like the user to be able to start typing in a band name (for example, "Rad") and have Discogs API display 10 most similar suggestions to them (for example, "Radical Face", "Radiohead", etc). These suggestions could be sorted either alphabetically or, ideally, by popularity.
The problem is that I don't know how to make such a request to the Discogs API. Here's the code I'm working with now, which retrieves the content of http://api.discogs.com/releases/1 and parses it.
Any insight would be appreciated. Thank you.
<?php
$url = "http://api.discogs.com/releases/1"; // add the resource info to the url. Ex. releases/1
//initialize the session
$ch = curl_init();
//Set the User-Agent Identifier
curl_setopt($ch, CURLOPT_USERAGENT, 'SiteName/0.1 +http://your-site-here.com');
//Set the URL of the page or file to download.
curl_setopt($ch, CURLOPT_URL, $url);
//Ask cURL to return the contents in a variable instead of simply echoing them
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Execute the curl session
$output = curl_exec($ch);
//close the session
curl_close ($ch);
function textParser($text, $css_block_name){
$end_pattern = '], "';
switch($css_block_name){
# Add your pattern here to grab any specific block of text
case 'description';
$end_pattern = '", "';
break;
}
# Name of the block to find
$needle = "\"{$css_block_name}\":";
# Find start position to grab text
$start_position = stripos($text, $needle) + strlen($needle);
$text_portion = substr($text, $start_position, stripos($text, $end_pattern, $start_position) - $start_position + 1);
$text_portion = str_ireplace("[", "", $text_portion);
$text_portion = str_ireplace("]", "", $text_portion);
return $text_portion;
}
$blockStyle = textParser($output, 'styles');
echo $blockStyle. '<br/>';
$blockDescription = textParser($output, 'description');
echo $blockDescription. '<br/>';
?>
With the discogs API you can easily execute a search. I think you have already viewed the documentation: https://www.discogs.com/developers/#page:database,header:database-search
There you can even say that you only want to search for artists. When you retrieve the results you must either sort them alphabetically by yourself or must relay on the order of the results. I think that order is already some kind of popularity by discogs as far as I can see from the documentation. And it is the same implementation as in the website integrated search.
You should keep in mind that the result set can be very large. So sorting by alphabet wouldn't be the best idea as you have to retrieve all result pages. Here you should increase the page_size parameter to the maximum of 100 items per page.

Formatting long structured text string from Jira Atlassian issue url in php

Im wondering if there is an easy way to access the information in a huge string i have, the string is structured, for the purpose of people reviewing it i put line breaks and space but this is just one huge single line of text that's returned:
First this is how i access the Jira API:
$username = 'xxx';
$password = 'xxx';
$url = 'https://xxx.atlassian.net/rest/api/2/Issue/Bug-5555';
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
$issue_list = (curl_exec($curl));
echo $issue_list;
Now that returns a huge string which when broken down looks like this:
{"expand":
"renderedFields,names,schema,transitions,operations,editmeta,changelog",
"id":"16935",
"self":"https://xxx.atlassian.net/rest/api/2/issue/16935",
"key":"xx-5555",
"fields":
{"summary":"Dialog boxes shouldn't be on top.",
"progress":
{"progress":0,
"total":0
},
"timetracking":{},
"issuetype":
{"self":"https://xxx.atlassian.net/rest/api/2/issuetype/1",
"id":"1",
"description":"A problem which impairs or prevents the functions of the product.",
"iconUrl":"https://xxx.atlassian.net/images/icons/bug.gif",
"name":"Bug",
"subtask":false
},
"timespent":null,
"reporter":
{"self":"https://xxx.atlassian.net/rest/api/2/user?username=xxx%40xxx.com",
"name":"xxx#xx.com",
"emailAddress":"xxx#xxx.com",
"avatarUrls":{"16x16":"https://xxx.atlassian.net/secure/useravatar?size=small&avatarId=10122",
"48x48":"https://xxx.atlassian.net/secure/useravatar?avatarId=10122"},
"displayName":"xxx",
"active":true
},
"created":"2012-08-25T18:39:27.760-0600",
"updated":"2012-08-31T16:47:38.761-0600",
"priority":
{"self":"https://xxx.atlassian.net/rest/api/2/priority/6",
"iconUrl":"http://dl.dropbox.com/u/xxx/3-green.png",
"name":"3 - Medium Priority",
"id":"6"
},
"description":"\"loading \" dialog is always on top, so is the \"Updating database\" dialog.\r\n\r\n\r\nThis is annoying. It shouldn't be on top and/or you should be able to easily minimize the window.",
"issuelinks":[], etc etc etc
Now im a basic php user so please try and keep the replies simplistic if possible, before i go down the route of parsing the whole document which will be difficult for me as im not familiar with parsing i was wondering if there was an easy way to access the values.
what im thinking is something like this:
foreach($issue_list->issues as $issue) {
echo "summary" . $issue->summary;
echo "updated" . $issue->updated;
echo "created" . $issue->created;
echo "description" . $issue->description;
}
Now this might be wishful thinking but i seen an article where i guy done something similar but i cant figure it out, here is the article:
http://www.lornajane.net/posts/2012/using-jiras-rest-api-to-create-a-dashboard
Also if it is possible, how would i access the reporter > displayName value since that's 2 indents deep, would it be $issue->reporter->displayName;
Finally one quick other question, if im echoing the description, how do i get it to obey the /r/r/r/r/r/n and /" so it prints it out with line breaks and removes those special characters?
This looks like a JSON (JavaScript Object Notation - more info here) string, you could probably use json_decode (documented here) to convert it into a PHP object and then easily index it.
I don't have your full string but you can probably try something along the lines of:
$jiraIssue = json_decode($theString);
echo $jiraIssue["id"];
Now, since objects are contained inside of objects, you'll probably have to go through "fields" before you can access "summary".
You can pass true as a second parameter if you'd prefer to deal with arrays instead of objects.

CURL return data handling

I've written a PHP function to get plus ones count for a URL
function makeApiCall($destinationUrl, $stringOfParams){
$curl = curl_init();
echo $destinationUrl.$stringOfParams."<br>";
curl_setopt($curl, CURLOPT_URL, $destinationUrl.$stringOfParams);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
echo $result;
}
While inputting https://plusone.google.com/u/0/_/+1/fastbutton as destination URL and inputting the correct string of params, the result I'm receiving in $result is HTML.
The problem is that I would like using PHP to get the count and not using JavaScript.
How can I do that?
Using preg_match, you can achieve it.
Assuming you are calling a url like that :
https://plusone.google.com/_/+1/fastbutton?bsv=pr&url=http://www.google.com
You are looking for:
<div id="aggregateCount" class="t1">118k</div>
or
<div id="aggregateCount" class="t1">12</div>
So you can perform:
preg_match('/\<div id=\"aggregateCount\" class=\"t1\"\>\>?([0-9]*k?)\<\/div\>/i', $result, $matches);
And $matches will be:
Array
(
[0] => <div id="aggregateCount" class="t1">118k</div>
[1] => 118k
)
edit:
After running the example, it seems that Google return a different number when using curl, for example, on http://www.google.com, it returns:
<div id="aggregateCount" class="t1">>9999</div>
So I've updated the regex to handle the >.

website query, php

How can I query a particular website with some fields and get the results to my webpage using php?
let say website xyz.com will give you the name of the city if you give them the zipcode. How can I acehive this easliy in php? any code snap shot will be great.
If I understand what you mean (You want to submit a query to a site and get the result back for processing and such?), you can use cURL.
Here is an example:
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
?>
You can grab the Lat/Long from this site with some regexp like this:
if ( preg_match_all( "#<td>\s+-?(\d+\.\d+)\s+</td>#", $output, $coords ) ) {
list( $lat, $long ) = $coords[1];
echo "Latitude: $lat\nLongitude: $long\n";
}
Just put that after the curl_close() function.
That will return something like this (numbers changed):
Latitude: 53.5100
Longitude: 60.2200
You can use file_get_contents (and other similar fopen-class functions) to do this:
$result = file_get_contents("http://other-site.com/query?variable=value");
Do you mean something like:
include 'http://www.google.com?q=myquery'; ? or which fields do you want to get?
Can you be a bit more specific pls :)
If you want to import the html to your page and analyze it, you probably want to use cURL.
You have to have the extensions loaded to your page (it's usually part of PHP _ I think it has to be compiled in? The manual can answer that)
Here is a curl function. Set up your url like
$param='fribby';
$param2='snips';
$url="www.example.com?data=$param&data2=$param2";
function curl_page($url)
{
$response =false;
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_FAILONERROR,true);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_TIMEOUT,30);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$page_data=curl_page($url);
Then, you can get data out of the page using the DOM parsing or grep/sed/awk type stuff.

Categories