Unable to delete the existing document in lucene index - lucene.net

I am using Lucene.Net (version 2.9.4.1) to implement a simple search module. I'm trying to delete the document if it exists in the index using the following code,
var analyzer = new StandardAnalyzer(Version.LUCENE_29);
var indexWriter = new IndexWriter(
LuceneSearch._luceneDir,
analyzer,
IndexWriter.MaxFieldLength.UNLIMITED);
var searchQuery = new TermQuery(new Term("ListID", listingDoc.Get("ListID")));
indexWriter.DeleteDocuments(searchQuery);
where listingDoc is of type Document i'm trying to delete the document if it exists and then add it again to the index, the adding part works fine but the deleting part is not working that is the document is not deleted if it exists. Therefore if i search a term and it matches it is shown multiple times... Please point out what iam doing wrong here
I am using ASP.Net MVC3 and Entity Framework4. every time a record is updated i intend to update the index but instead its been duplicated. and when i search it i get the result twice or thrice depending upon the number of times i do the update.
I tried using indexWriter.UpdateDocument(args); to no avail...

When debugging deletions it can sometimes be useful to perform a search with the same parameters as the delete command, to see exactly what is going to get deleted.
If you're doing a deleteDocuments(query) you should use an IndexSearcher like this:
IndexSearcher is = new IndexSearcher(indexWriter.GetReader());
TopDocs topDocs = is.Search(query, 100);
And see what you get in the topDocs. I suspect you'll find that the query doesn't return any results.

You can do it by simply:
Query query = queryParser.parse("My Query!");
writer.deleteDocuments(query);

Related

Laravel in controller return new collection of results from a foreach loop through records

This seems like it should be obvious, but everything I find relates to returning a collection extracted from records, rather then returning a collection of new results derived from calculations on the records.
For instance, say I have records of property in my database. I can extract a collection of a subset (or the entire set) of the records. But I want to loop through this collection, calculate new values for each line item, (like marketValue-debt=netValue) and return a new collection of just those results to my view. I'm trying to keep my (much more complicated than this example) calculation in my controller and out of my view, but I'm not getting the way to stuff new values into a new collection of results for return to display in the view.
I could derive my results and stuff them into an array, but how do I pass this as a new collection for looping through in my view to show those results? Seems like there should be an Eloquent way to do this.
My project is in Laravel 6 running on Apache/Laragon, PHP 7 with MariaDb
Thanks in advance for helping me out.
Take a look at this. Hope it will be of some help.
If you want a new collection, use the map method.
$new_collection = $old_collection->map(function ($item) {
return $item->marketValue - $item->debtValue;
});

Getting translated records in CommandController

I've searched and debugged the last couple of days how to obtain the
translated version of a DomainModel object in a CommandController in Typo3 v8.7.
In Typo3 4.5/4.7 I've done the following:
- input: DomainModel in default language
- build a query that finds the record with the l10n_parent matching the
given domain model
- obtain a new domain model with the desired sys_language_uid
Unfortunately this does not work in Typo3 v8.7 any more. I always get
the domain model for the default language.
I've traced this down to the method Typo3DbBackend::doLanguageAndWorkspaceOverlay
called via Typo3DbBackend::getObjectDataByQuery
The query returns the correct (translated) row (seen in the debugger and also the mysql query log), but then the variable
$row gets overwritten in doLanguageAndWorkspaceOverlay no matter how I
set the querySettings setLanguageOverlayMode and setLanguageMode.
So what is the correct way to get a translated domain model in a
CommandController?
UPDATE:
I think I'm a step further. If I add ->setQueryLanguage(1) to the query settings, doLanguageAndWorkspaceOverlay() tries to fetch the translated record for language = 1. But in order to succeed I need to trick the FrontendGroupRestriction class by setting $GLOBALS['TSFE']->gr_list = "0,-2";.
The array returned by doLanguageAndWorkspaceOverlay() now contains all translated entries, except the uid, which is still the uid from the record in the main language. The uid of the translated record is stored in _LOCALIZED_UID.
Now my problem now is that I still get the record in the main laguage because DataMapper->mapSingleRow() (called via DataMapper->map()) has some kind of object cache and thus returns the object in the default language (because the uid is still the one of the record in the main language).
All this seems a little hackish. So again my question: what is the correct way to get a translated domain model in a CommandController?
thanks,
mika
p.s.: I've set up the second language in the backend and creating a translated record works just fine. My question is just how to I get an existing translated record in a CommandController.
alternative solution:
based on the solution above, I decided that I can do almost everything
on my own. So what I do now is
i) create an independend querybuilder for the according table:
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);`
ii) select the record with the desired l10n_parent and sys_language_uid
$query = $queryBuilder->select('*')
->from($tableName)
->where($queryBuilder->expr()->eq('sys_language_uid', $langId))
->andWhere($queryBuilder->expr()->eq('l10n_parent', $parentUid))
->execute();
iii) fetch all records into an array
$rows = $query->fetchAll();
iv) invoke the DataMapper manually to get the object
$dataMapper = $this->objectManager->get(DataMapper::class);
$translated = $dataMapper->map($className, $rows);
I know it has nothing to do with the ModelRepository any more, but it
works quite fine for now...
that's all folks
My solution to the issue described above:
To avoid invoking the DataMapper as part of the query from
Typo3DbBackend, I used a raw query (argument for ->execute()) and get
back an array, that already went through language overlay etc.
BUT: in
the array the '_LOCALIZED_UID' is still available. So I overwrite the
uid with the value from '_LOCALIZED_UID' and invoke the DataMapper
manually. Quite cumbersome and very hackish to overcome the Typo3
backend shortcomings...

javax.persistence.cache.retrieveMode and javax.persistence.cache.retrieveMode does not work with NamedQuery when used with Eclipse Link ORM

Query Hints not working in Eclipse Link 2.3.2/2.6.1 when used to fetch data from second level Cache
Used Hints,
#QueryHint(name = "javax.persistence.cache.retrieveMode", value = "USE"),
#QueryHint(name = "javax.persistence.cache.storeMode ", value = "USE")
Tried with below options.
1. Added JPA Hints to Named query itself
#NamedQuery(
name = TestEntity.FIND_BY_CODE,
query = "select t from Test t where t.code = :code",
hints = {
#QueryHint(name = "javax.persistence.cache.retrieveMode", value = "USE"),
#QueryHint(name = "javax.persistence.cache.storeMode ", value = "USE") })
2. Adding hints to the Entity Manager Itself after injecting it
em.setProperty("javax.persistence.cache.retrieveMode", CacheRetrieveMode.USE);
em.setProperty("javax.persistence.cache.storeMode", CacheRetrieveMode.USE);
3. Added JPA hints at the time of Query execution
em.createNamedQuery(TestEntity.FIND_BY_CODE,
AlertCategoryType.class).setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.USE)
.setHint("javax.persistence.cache.storeMode", CacheStoreMode.USE)
.setParameter("code", code).getSingleResult();
None of the above usage of hints worked. Then i tried debugging on three different options what i found is,
the Data Base Query which formed after setting these hints is passing hints as key/value pair below.
eclipselink.query.hints => {javax.persistence.cache.retrieveMode=USE, javax.persistence.cache.storeMode=USE}
Where eclipselink.query.hints is the key even when we have set the JPA hints. This is something we don't have control over to change this.
But when i pass Eclipse Link provided hints as below , It started working as expected and the Results are fetched from Cache and not from the DB.
eclipselink.query.hints => {eclipselink.query-results-cache.size=500, eclipselink.query-results-cache=true}
It means That When we use Eclipse Link it only recognizes Eclipse Link provided hints according to the key[ above shown] we see in Query.
Please suggest any work around to get the JPA Hints working
Environment I'm using is
Eclispe Link 2.3.2/2.6.1
Runnin fin serve Glassfish 4.1[payara]
Java8/JEE7
The query hint you state is working (eclipselink.query-results-cache) is completely unrelated - it creates a new cache for the query results so that next time you execute that same query, the results are already there, so it does not need to execute the query again. This is outside (above an beyond) the second level cache.
The settings you refer to as not working affect the second level cache. Without more information, I'm going to state they are likely working as expected. Just because your query goes to the database does not mean the cache isn't being used. Caching entities is very different than caching the results to a query. If the query results are not cached, unless you have in-memory querying enabled, most read-all type queries must go to the database to determine what entities need to be built and returned. EclipseLink will then use those results to check the cache- if the entities already exist, they are returned as is - this avoids the overhead of rebuilding entities from the data.
You can check if your entity has been cached by using a em.find() or read query that uses the ID value. The cache is indexed by ID, so it will not need to go to the database to figure out which entities you want.

Is there any way to get old record and new record within orient db hook

I'm new to Orient db and trying to read old record and the new record within orient db "onBeforeUpdate" hook. New record can get from the "doc" variable within hook. But i need to get old record within that hook to compare with the new record. Is there any method or work around to do so?
We can get old record by querying the doc.field('#rid'). But you need to specifically say "nocache" in the query. Otherwise db query will give you the updated record with old #version number.
Sample Query : SELECT FROM #46:66 NOCACHE

Manually Edit Collections

I'm relatively new to Meteor and I was wondering how I manually edit the mongoDB for Meteor Collections.
If I declare a new collection on both the client and server:
People = new Meteor.Collection("people");
Then I create an array of names on the server and insert it into the collection:
var names = ["Dan", "Bob", "Sarah"];
for(var i=0; i<names.length; i++)
{
People.insert({name: names[i]});
}
How do I add fields to the database and/or change fields in the database manually for development purpose? If I retype the names in the 'names' array and relaunch the app, it doesn't update the database on the server like I expected it would.
Thanks!
Use the javascript developer console in chrome/safari or firebug in firefox
while your app is running you can edit your names.
Your changes will be done live so you can debug and play around quite alot. Something like this might work:
People.find().fetch()
=> lists all the people
Edit one
People.update("_id value from above of the person", {$set:{name:"New Name"}})
Why the method you're using isn't working:
Meteor won't add the names again to the People collection if its already populated. So just run meteor reset to clear everything in your collection. And run meteor again to use your new updated values