Skip to content

Commit

Permalink
DOC-2808 (#2843)
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardSmedley authored Aug 13, 2018
1 parent 4bec730 commit 9df114b
Showing 1 changed file with 257 additions and 0 deletions.
257 changes: 257 additions & 0 deletions content/sdk/php/full-text-searching-with-sdk.dita
Original file line number Diff line number Diff line change
Expand Up @@ -237,5 +237,262 @@ var_dump($result->facets);</codeblock>
}
}</screen>
</section>



<section><title>Detailed Examples</title>

<p>
The code example below demonstrates the PHP SDK Full Text Search
API. The example assumes that Couchbase Server is running, and that
the username <codeph>Administrator</codeph> and
the password <codeph>password</codeph> provide authorization for performing the searches.
It also assumes that the <codeph>travel-sample</codeph> bucket
has been installed.
For information on creating users and managing roles, see
<xref href="../../security/security-authorization.dita" scope="local" format="dita">Authorization</xref>.
For information on installing sample buckets, see
<xref href="../../settings/settings.dita" scope="local" format="dita">Settings</xref>.
</p>

<p>
The example also assumes the existence of three specific Full Text Indexes, defined on the
<codeph>travel-sample</codeph> bucket. These are:
</p>

<ul>
<li>
<codeph>travel-sample-index-unstored</codeph>: Uses only the default
settings.
</li>

<li>
<codeph>travel-sample-index-stored</codeph>: Uses default settings, with one exception:
dynamic fields are <i>stored</i>, for the whole index.
</li>

<li>
<codeph>travel-sample-index-hotel-description</codeph>: Indexes only the <codeph>description</codeph>
fields of <codeph>hotel</codeph>
documents, and disables the <codeph>default</codeph> type mapping. The index
has a custom analyzer named <codeph>myUnicodeAnalyzer</codeph> defined on it: the analyzer's
main characteristic is that it uses the <b>unicode</b> tokenizer.
</li>
</ul>

<p>
See
<xref href="../../fts/fts-creating-indexes.dita" scope="local" format="dita">Creating Indexes</xref>
for details on how to create these indexes: they can be created interactively, by
means of the Couchbase Web Console; however, there may be greater efficiency
in using the Couchbase REST
API, as described
in the section
<xref href="../../fts/fts-creating-indexes.dita#topic_ksl_wwk_1v/index-creation-with-the-rest-api" scope="local" format="dita">Index Creation with the REST API</xref>.
The JSON objects that constitute index-definitions (for
inclusion as bodies to the index-creation REST calls), are provided in
<xref href="../../fts/fts-demonstration-indexes.dita" scope="local" format="dita">Demonstration Indexes</xref>.
</p>
<codeblock outputclass="language-php">&lt;?php

use \Couchbase\Cluster;
use \Couchbase\Bucket;
use \Couchbase\SearchQuery;

function printResult($label, $resultObject) {
echo("\n");
echo("= = = = = = = = = = = = = = = = = = = = = = =\n");
echo("= = = = = = = = = = = = = = = = = = = = = = =\n");
echo("\n");
echo($label);
echo(' (total hits: ' . $resultObject->metrics['total_hits'] . ')');
echo("\n");

foreach ($resultObject->hits as $row) {
echo("id=" . $row->id . ", score=" . $row->score);
if (property_exists($row, 'fields')) {
echo(", fields=" . json_encode($row->fields));
}
if (property_exists($row, 'locations')) {
echo(", locations=" . json_encode($row->locations));
}
if (property_exists($row, 'fragments')) {
echo(", fragments=" . json_encode($row->fragments));
}
echo("\n");
}
}

// Simple Text Query on a single word, targeting an index with dynamic fields
// unstored.
function simpleTextQuery(Bucket $bucket) {
$indexName = "travel-sample-index-unstored";
$query = new SearchQuery($indexName,
SearchQuery::match("swanky"));
$query->limit(10);
$result = $bucket->query($query);
printResult("Simple Text Query", $result);
}

// Simple Text Query on Stored Field, specifying the field to be searched;
// targeting an index with dynamic fields stored, to ensure that field-content
// is included in the return object.
function simpleTextQueryOnStoredField(Bucket $bucket) {
$indexName = "travel-sample-index-stored";
$query = new SearchQuery($indexName,
SearchQuery::match("MDG")->field("destinationairport"));
$query->limit(10)->highlight(SearchQuery::HIGHLIGHT_HTML);
$result = $bucket->query($query);
printResult("Simple Text Query on Stored Field", $result);
}

// Simple Text Query on Non-Default Index, specifying an index that consists
// only of content derived from a specific field from a specific document-type.
function simpleTextQueryOnNonDefaultIndex(Bucket $bucket) {
$indexName = "travel-sample-index-hotel-description";
$query = new SearchQuery($indexName,
SearchQuery::match("swanky"));
$query->limit(10);
$result = $bucket->query($query);
printResult("Simple Text Query on Non-Default Index", $result);
}

// Match Query with Facet, showing how query-results can be displayed either by
// row or by hits; and demonstrating use of a facet, which provides
// aggregation-data.
function textQueryOnStoredFieldWithFacet(Bucket $bucket) {
$indexName = "travel-sample-index-stored";
$query = new SearchQuery($indexName,
SearchQuery::match("La Rue Saint Denis!!")->field("reviews.content"));
$query->limit(10)->highlight(SearchQuery::HIGHLIGHT_HTML);
$query->addFacet('Countries Referenced', SearchQuery::termFacet('country', 5));
$result = $bucket->query($query);
printResult("Match Query with Facet, Result by Row", $result);

echo("Match Query with Facet, Result by facet:\n");
echo(json_encode($result->facets) . "\n");
}

// DocId Query, showing results of a query on two document IDs.
function docIdQueryMethod(Bucket $bucket) {
$indexName = "travel-sample-index-unstored";
$query = new SearchQuery($indexName,
SearchQuery::docId("hotel_26223", "hotel_28960"));
$result = $bucket->query($query);
printResult("DocId Query", $result);
}

// Unanalyzed Term Query with Fuzziness Level, demonstrating how to query on a
// term with no analysis. Zero fuzziness is specified, to ensure that matches
// are exact. With a fuzziness factor of 2, allowing partial matches to be made.
function unAnalyzedTermQuery(Bucket $bucket, int $fuzzinessLevel) {
$indexName = "travel-sample-index-stored";
$query = new SearchQuery($indexName,
SearchQuery::term("sushi")->field("reviews.content")->fuzziness($fuzzinessLevel));
$query->limit(50)->highlight(SearchQuery::HIGHLIGHT_HTML);
$result = $bucket->query($query);
printResult("Unanalyzed Term Query with Fuzziness Level of " . $fuzzinessLevel . ":", $result);
}

// Match Phrase Query, using Analysis, for searching on a phrase.
function matchPhraseQueryOnStoredField(Bucket $bucket) {
$indexName = "travel-sample-index-stored";
$query = new SearchQuery($indexName,
SearchQuery::matchPhrase("Eiffel Tower")->field("description"));
$query->limit(10)->highlight(SearchQuery::HIGHLIGHT_HTML);
$result = $bucket->query($query);
printResult("Match Phrase Query, using Analysis", $result);
}

// Phrase Query, without Analysis, for searching on a phrase without analysis supported.
function unAnalyzedPhraseQuery(Bucket $bucket) {
$indexName = "travel-sample-index-stored";
$query = new SearchQuery($indexName,
SearchQuery::phrase("dorm", "rooms")->field("description"));
$query->limit(10)->highlight(SearchQuery::HIGHLIGHT_HTML);
$result = $bucket->query($query);
printResult("Phrase Query, without Analysis", $result);
}

// Conjunction Query, whereby two separate queries are defined and then run as
// part of the search, with only the matches returned by both included in the
// result-object.
function conjunctionQuery(Bucket $bucket) {
$indexName = "travel-sample-index-stored";
$firstQuery = SearchQuery::match("La Rue Saint Denis!!")->field("reviews.content");
$secondQuery = SearchQuery::match("boutique")->field("description");
$query = new SearchQuery($indexName,
SearchQuery::conjuncts($firstQuery, $secondQuery));
$query->limit(10)->highlight(SearchQuery::HIGHLIGHT_HTML);
$result = $bucket->query($query);
printResult("Conjunction Query", $result);
}

// Query String Query, showing how a query string is specified as search-input.
function queryStringQuery(Bucket $bucket) {
$indexName = "travel-sample-index-unstored";
$query = new SearchQuery($indexName,
SearchQuery::queryString("description: Imperial"));
$query->limit(10);
$result = $bucket->query($query);
printResult("Query String Query", $result);
}

// Wild Card Query, whereby a wildcard is used in the string submitted for the
// search.
function wildCardQuery(Bucket $bucket) {
$indexName = "travel-sample-index-stored";
$query = new SearchQuery($indexName,
SearchQuery::wildcard("bouti*ue")->field("description"));
$query->limit(10)->highlight(SearchQuery::HIGHLIGHT_HTML);
$result = $bucket->query($query);
printResult("Wild Card Query", $result);
}

// Numeric Range Query, whereby minimum and maximum numbers are specified, and
// matches within the range returned.
function numericRangeQuery(Bucket $bucket) {
$indexName = "travel-sample-index-unstored";
$query = new SearchQuery($indexName,
SearchQuery::numericRange()->min(10100)->max(10200)->field("id"));
$query->limit(10);
$result = $bucket->query($query);
printResult("Numeric Range Query", $result);
}

// Regexp Query, whereby a regular expression is submitted, to generate the
// conditions for successful matches.
function regexpQuery(Bucket $bucket) {
$indexName = "travel-sample-index-stored";
$query = new SearchQuery($indexName,
SearchQuery::regexp("[a-z]")->field("description"));
$query->limit(10)->highlight(SearchQuery::HIGHLIGHT_HTML);
$result = $bucket->query($query);
printResult("Regexp Query", $result);
}

$cluster = new Cluster('couchbase://localhost');
$cluster->authenticateAs('Administrator', 'password');
$bucket = $cluster->openBucket('travel-sample');

simpleTextQuery($bucket);
simpleTextQueryOnStoredField($bucket);
simpleTextQueryOnNonDefaultIndex($bucket);
textQueryOnStoredFieldWithFacet($bucket);
docIdQueryMethod($bucket);
unAnalyzedTermQuery($bucket, 0);
unAnalyzedTermQuery($bucket, 2);
matchPhraseQueryOnStoredField($bucket);
unAnalyzedPhraseQuery($bucket);
conjunctionQuery($bucket);
queryStringQuery($bucket);
wildCardQuery($bucket);
numericRangeQuery($bucket);
regexpQuery($bucket);
</codeblock>

</section>


</body>
</topic>

0 comments on commit 9df114b

Please sign in to comment.