Search code examples
phpangularjscloudant

Specific search with Cloudant and AngularJS


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:[email protected]/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?


Solution

  • Try using double quotes in your search instead of single quotes, for example:

    $url = "https://user:[email protected]/db/_design/fairs/_search/by_fair?q=\"$newsearch\"&include_docs=true";
    

    Note the change to the q param:

    q=\"$newsearch\"