Mongodb Full text search Using PHP 5.6 - php

I am trying to use mongodb full text search for showing jobs list. I have done all the necessary steps to create the text indexes and enable the full text search feature on the database and everything is working fine except from the full text search using PHP 5.6.

use the below code in php for full text:
<?php
$username = 'mongodbusername';
$password = 'changeMe';
$m = new MongoClient("mongodb://myadmin1:myadmin123#localhost/dbname");
//$m = new MongoClient("mongodb://localhost", array("username" => $username, "password" => $password,"db" => "jobma_integrations"));
$db = $m->integrations; // this is your dbname
$crawlingCollection = $db->crawled_jobs; // this is your collection name
$c = $crawlingCollection->find(
['$text' => ['$search' => "sales \"ardmore\""]], // this \"ardmore\" is used for exact match and sales will be match with any where
['score'=> ['$meta'=>'textScore']]
)->sort(
['score'=> ['$meta'=>'textScore']]
);
echo "<pre>";
var_dump(iterator_to_array($c));
?>

Related

How to get length of find() results with MongoDB\Driver query in PHP?

I have a MongoDB server with a collection counting hundreds of thousands of documents.
I need to get only the number of documents matching the query filter condition using MongoDB\Driver in PHP.
In mongo-shell I would simply do the following:
db.samples.find({"location": {$geoWithin: {$geometry: ...reference_polygon... }}}).length
The below query in PHP would return a cursor with the full list of the documents (could be thousands of docs) which I don't need, I'm only looking for the number of the samples within a specified polygon:
$query = new MongoDB\Driver\Query(
["location" => ['$geoWithin' => ['$geometry' => $reference_polygon]]],
[]
);
$cursor = $dbm->executeQuery("test.samples", $query);
Even reducing projection to return a single field as _id would significantly increase the calculation time.
Is there any way to get just a scalar number of documents and avoid loading cursor results and then counting it like count($cursor->toArray())?
MongoDB Server: 4.4.1
PHP version: 7.4
OS: Ubuntu 20.04 LTS
So, finally, I found a way to do it using MongoDB\Driver\Command.
The code is below, would someone have a better proposal, please feel free to comment.
$database_name = "test";
$collection_name = "samples";
$query = new MongoDB\Driver\Query(
["location" => ['$geoWithin' => ['$geometry' => $reference_polygon]]],
[]
);
$dbc = new MongoDB\Driver\Command(
['count' => $collection_name, "query" => (object)$query]
);
$cursor = $dbm->executeCommand($database_name, $dbc);
$n = (isset($cursor->toArray()[0]->n)) ? $cursor->toArray()[0]->n : 0;

Documents are not inserting using php

I am trying to insert a document using PHP for my project but the code is not working.
for reference, I have echoed "still here" and "done".
But the code is not able to execute the insert query please help.
I have already tried all the stack overflow and other sites but non of them work properly.
<?php
require 'vendor/autoload.php';
include 'lib/JSON.php';
// connect to mongodb
$client = new MongoDB\Client("mongodb://localhost:27017");
// select a database
$db = $client->hm;
$collection = $db->sc;
$count = $collection->count();
$document = array("_id" => $count,
"device_id" => 100,
"pin" => 17,
"status" => "True");
echo "still here";
$collection->insert($document);
echo "done"
?>
I am only getting "still here" on browser.
Try to use method insertOne. Does that give some additional errors?
https://docs.mongodb.com/php-library/master/reference/method/MongoDBCollection-insertOne/

Adding a tag to existing Note?

I'm trying to update a note(s) that are returned from an Evernote search using PHP. The search part works just fine, but once I find the list of notes I can't seem to add a tag to the note. I can confirm that tag GUIDs are correct in both cases.
use EDAM\NoteStore\NoteFilter;
$client = new Client(array(
'token' => $authToken,
'sandbox' => true
));
$filter = new NoteFilter();
$filter->words = "HIGH";
$filter->tagGuids = array("ababe33d-75e6-4a50-b2ba-61889bb2b8a6");
$notes_result = $client->getNoteStore()->findNotes($filter, 0, 10);
$notes = $notes_result->notes;
foreach ($notes as $note) {
echo $note->title . "\n";
// **** ERROR HERE ****
$note->updateTag($authToken, "b3b290fa-4ca0-493e-8dd1-5cf1bff28b92");
}
The error you get is because updateTag must be called on NoteStore, not the note object. But: NoteStore.updateTag updates the tag object itself. To add tag to a note one needs NoteStore.updateNote instead. Your API token must also support write permission on notes.

Search NetSuite Custom Record Type in PHP

I have a Custom Record Type and I am having trouble searching the barcode from the item value:
I am using the NetSuite PHPToolkit_2015_2 and this stackoverflow answer https://stackoverflow.com/a/13366947/2120512 and http://burnignorance.com/netsuite-tips-and-hacks/working-with-custom-records-in-suitetalk/ to attempt to build this request:
$service = new NetSuiteService();
// Perform an AdvancedSearch for Items
// https://netsuite.custhelp.com/app/answers/detail/a_id/12203/kw/php%20search%20criteria
$service->setSearchPreferences(false, 1000, false);
$savedSearchId = 'customsearch_barcode_view'; //customsearch## from UI ID field
$searchAdvanced = new CustomRecordSearchAdvanced();
setFields($searchAdvanced, array('savedSearchScriptId'=>$savedSearchId));
$request = new SearchRequest();
$request->searchRecord = $searchAdvanced;
// PHP Toolkit 2012.2: Sample Code to Perform Search that Uses a Custom Field as Filter
// https://netsuite.custhelp.com/app/answers/detail/a_id/25066/kw/php%20custom%20field
$custSearchField = new SearchMultiSelectCustomField();
$custSearchField->value = new ListOrRecordRef();
$custSearchField->value->internalId = "custrecord_barcode_item";
$custSearchField->value->value = "00001 Beer Mug";
$searchAdvanced->customFieldList = $custSearchField;
$results = $service->search($request);
I still get all the results for the Custom Record Type and never able to figure out how to search by the item. I have made changes and still receive the entire results.
I'm pretty sure this won't immediately solve your problem, but at the first glance I can see an error in your code.
The customFieldList is of type array, so you need to change this:
$searchAdvanced->customFieldList = $custSearchField;
To this
$searchAdvanced->customFieldList = [$custSearchField];

CREA DDF, retrieve a single property By ID

I'm trying to pull one single property from the CREA Data Distribution Facility by it's listing ID. The purpose is for a wordpress plugin where the realtor will simply input his listing ID and it will pull all the data into the wordpress post.
If you've had any experience with this system, i'm using PHPRETS and am having a hell of time navigating through the documentation for querying. I've got a feed all setup and pulling random properties, but i'm looking to pull one specific one.
Thanks! Let me know if you need any more info.
If you looking for the php eneral code, please try this :
1- dowload PHRets_CREA.php file from CREA website : https://support.crea.ca/DDF#/discussion/20/crea-data-distribution-code-sample-in-php.
2- create your php file in the same folder :
Of course you must have $ListingKey, change it on the code
/* RETS Variables */
require("PHRets_CREA.php");
$RETS = new PHRets();
$RETSURL = "http://data.crea.ca/Login.svc/Login";
$RETSUsername = "Username";
$RETSPassword = "Password";
$RETS->Connect($RETSURL, $RETSUsername, $RETSPassword);
$RETS->AddHeader("RETS-Version", "RETS/1.7.2");
$RETS->AddHeader('Accept', '/');
$RETS->SetParam('compression_enabled', true);
$RETS_LimitPerQuery = 10;
$TimeBackPull = "-1 hours";
echo 'Connecting to RETS as : <span style="color:#008000;"><b>'.$RETSUsername.'</b></span><br/>';
echo "-----GETTING ALL Listings-----<br/>";
//Get Id's
$DBML = "(LastUpdated=" . date('Y-m-d', strtotime($TimeBackPull)) . ")";
$params = array("Limit" => 1, "Format" => "STANDARD-XML", "Count" => 1);
$results = $RETS->SearchQuery("Property", "Property", $DBML, $params);
$totalAvailable = $results["Count"];
echo "-----".$totalAvailable." Found-----<br/>";
$ListingKey = "ListingKey here";
$propriety = $RETS->SearchQuery("Property", "Property", "(ID=".$ListingKey.")", array("Limit" => 1, "Format" => "STANDARD-XML"));
echo "<pre>";
print_r($propriety);
echo "</pre>";

Categories