Delete document in ElasticSearch using Elastica library - php

I have Elastica library and implemented ElasticSearch in my project.
I want to delete document from index.
How to delete documents by ids in elasticsearch using elastica ?
I have tried multiple solution but it still not working!
TIA

For deleting documents by ids you can use deleteByQuery() function which is a function for index in elastica, so you can use it like this:
$matchPhraseQuery = new MatchPhrase("_id", /*id of document you want to delete*/);
$client = new Client(/*your client config*/);
$index = $client->getIndex("/*Your index name*/");
$index->deleteByQuery($matchPhraseQuery);

Related

Programmatically load Entity in symfony

I am trying to load in Entity classes and use within a loop in order to load content in dynamically from files into relating tables.
Is there any way i can load in all Entity files from the following
use AppBundle\Entity\aaPostcode;
use AppBundle\Entity\abPostcode;
use AppBundle\Entity\acPostcode;
use AppBundle\Entity\adPostcode;
in such a way like this?
use AppBundle\Entity\*
Not sure if this is possible in Symfony.
My next issue is using the the prefixedEntity within a loop like so -
new $entityPrefix
When i am setting $entityPrefix to the following format
$entityPrefix = str_replace([".csv"], "", $entityFilename) . "Postcode" . '()';
which returns the string of
"abPostcode()"
can anyone advise as to why calling
new $entityPrefix;
is not working
Thanks in advance for any help!
trying to call
new $entityPrefix();
returns
[Symfony\Component\Debug\Exception\ClassNotFoundException]
Attempted to load class "abPostcode" from the global namespace.
Did you forget a "use" statement for "AppBundle\Entity\abPostcode"?
even when i current am hrd coding the use stament to call
use AppBundle\Entity\abPostcode;
You can't instanciate dynamically your class without ginving the full namespace. Try that :
$namespace = "AppBundle\Entity\\";
$entityName = "YourEntity";
$namespace .= $entityName;
$class = new $namespace();
This is working for me...
I solved this by pulling out the functionality into a helper and running a switch statement based on the input - in this case a {prefix}
I kinda ran into this issue migrating data from one database type to another. IE: Oracle to MySQL. With hundreds of tables the use section would get big.
This is on Symfony 5.2.x BTW.
When I run a query using a standard repository or a repository linked to the table I can call the entity with its full namespace.
$oracle = $em2->getRepository(\App\Entity\Oracle\Appoints::class)->Appoints();
So I did not have to load "use App\Entity\Oracle\Appoints
Then I can create a new object in the same way:
$mysqlObject = new \App\Entity\OracleAppPoints();

FOSElasticaBundle: Is it possible to change "query_builder_method" in controller?

According to FOSElasticaBundle documentation it is possible to configure application to use custom query builder method like this:
user:
persistence:
elastica_to_model_transformer:
query_builder_method: createSearchQueryBuilder
But is it possible to choose QB method live, e.g. in controller action?
I'd like to be able to control what's being fetched from DB while transforming Elastica results to Doctrine entities. E.g. sometimes I'll want to do eager fetch on some relations, but can't do that by default.
Since FOSElasticaBundle documentation is not very precise, I went through its code and found it impossible to control what query builder is used on controller level.
It is possible to change whole elastica_to_model_transformer to a custom service, but still it's statically defined in configuration. Maybe with some dirty solution it would be possible going this way, but I don't think it's worth it.
I decided to just not using this feature of FOSElasticaBundle. The main problem I had was that when you use fos_elastica.index instead of fos_elastica.finder or elastica repository (in order to get plain not transformed results Elastica\Resultset), there's no findPaginated method with returns Pagerfanta paginator object, which is very helpful in my case.
Fortunately although it's not mentioned in documentation it's possible to create the Pagerfanta this way too, but a little bit more manually.
Here's a code snippet:
//generate ElaticaQuery somehow.
$browseQuery = $browseData->getBrowseQuery();
$search = $this->container->get('fos_elastica.index.indexName.typName');
//create pagerfanta's adapter manually
$adapter = new \Pagerfanta\Adapter\ElasticaAdapterElasticaAdapter($search, $browseQuery);
// now you can create the paginator too.
$pager = new Pagerfanta($adapter);
//do some paging work on it...
$pager->setMaxPerPage($browseData->getPerPage());
try {
$pager->setCurrentPage($browseData->getPage());
} catch(OutOfRangeCurrentPageException $e) {
$pager->setCurrentPage(1);
}
//and get current page results.
/** #var Result[] $elasticaResults */
$elasticaResults = $pager->getCurrentPageResults();
// we have to grab ids manyally, but it's done the same way inside FOSElasticaBundle with previous approach
$ids = array();
foreach($elasticaResults as $elasticaResult) {
$ids[] = $elasticaResult->getId();
}
//use regular Doctrine's repository to fetch Entities any way you want.
$entities = $this->getDoctrine()->getRepository(MyEntity::class)->findByIdentifiers($ids);
This actually has a few advantages. In general it gives you back control over your data and doesn't tie ElasticSearch with Doctrine. Therefore you can resign on fetching data from Doctrine if you have all needed data in ElasticSearch (if they are read only data of course). This lets you optimize your application performance but reducing amount of SQL queries.
The code above may be wrapped with some kind of service in order to prevent making mess in controllers.

MongoDB PHP library how to create database

I use MongoDB PHP library in the client object I can see the method for drop a database, how can I create a new database?
You probably have problems with the naming.
In mongodb a DB is called an Index and a tabe called a Collection.
You will find createIndex in the docs:
http://mongodb.github.io/mongo-php-library/api/namespace-MongoDB.Operation.html
https://docs.mongodb.org/v3.0/reference/method/db.collection.createIndex/
the namespace database is automatically create if it does not exist when we create collection
use MongoDB\Driver\Manager;
use MongoDB\Database;
$manager = new Manager("mongodb://localhost:27017");
$database = new Database($manager, 'newDataBase');
var_dump($database->createCollection('CollectionName'));
sorry i keep learning

Retrieving list of sub-OUs within a specific OU using ADLDAP2

I'm working on a project using PHP Adldap2 Library (https://github.com/Adldap2/Adldap2) and I need to retrieve a list of sub-OUs within a specific OU from Active Directory.
That's what I tried:
$ad->search()
->whereEquals(
ActiveDirectory::OBJECT_CATEGORY,
ActiveDirectory::ORGANIZATIONAL_UNIT_LONG
)
->whereEndsWith('dn', 'OU=myou,DC=mycompany,DC=com')
->get();
While the first filter works and retrieves all OUs, the second doesn't and returns an empty array. I also tried using 'distinguishedname' instead of 'dn' in whereEndsWith, with the same result.
How can this be done?
After doing some research and experimentation I came up with this code:
$config = Adldap::getConfiguration();
$baseDn = new Adldap\Objects\DistinguishedName($config->getBaseDn());
$departmentsDn = $baseDn->addOu('myou');
$search = Adldap::search()->setDn($departmentsDn->get());
$entries = $search->whereEquals(ActiveDirectory::OBJECT_CATEGORY, ActiveDirectory::ORGANIZATIONAL_UNIT_LONG)->get();
I'm not sure if it is the best solution but it works.
PS: I'm using Laravel adldap facade that's why adldap calls are static.

PHP and Elasticsearch include score/relevance in returned object

So I'm using PHP Symfony and the Ongr-Elasticsearch bundle, to query my documents and return matching objects. The results seem to be sorted by relevance/score, but the actual relevance/score isn't included in the objects themselves.
Is this possible to do? I'd like to include the score in what I send to the frontend to be able to do stuff with it.
Code:
$search = $this->esRepository->createSearch();
$search->setSize(30);
$queryFields = array(....);
$queryStringQuery = new QueryStringQuery($queryString, ["fields" => $queryFields]);
$search->addQuery($queryStringQuery);
$esResults = $this->esRepository->execute($search, Repository::RESULTS_ARRAY);
When you are executing query with Repository::RESULTS_ARRAY it only returns _source from hits. What you want to use is Repository::RESULTS_RAW_ITERATOR ant it will return whole hit which will include _score.

Categories