Updating index in Lucene.NET - lucene.net

I am doing a POC on Search using Lucene.NET.
I fire a stored procedure which fetches around 50000 records from the database.
Thses records I put in the Lucene Index.
Now when the records in database changes, how to update the Lucene index.
Deleting the entire previous indexed and creating a new one will take a lot of time.
I want to append the new records from the database to the existing index.
How can I achieve this.
Any ideas ???
Thanks,
Aneesh

Just use lucene AddDocument method, something like this :
IndexWriter iw = new IndexWriter(folder, GetAnalyzer(), false);
try
{
Document luceneDoc = new Document();
/// add fields to the lucene document
iw.AddDocument(luceneDoc);
}
finally
{
iw.Close();
}

Related

Is there a way to quickly check every table in a mongodb database with the column "title"?

Is there a way to quickly check every table in a mongodb database with the column "title"? I need to identify every table or rather collection where there's a column with the word "title", is there a way to do this using a mongodb query?
In Mongo there is no straight forward query to check all collections and fields. Instead, you can get a list of all collections using getCollectionInfos and then query each collection to see if there exists the field that you are looking for.
db.getCollectionInfos().forEach(function(c){
result = db.getCollection(c.name).findOne({"title":{$exists:true}});
if(result != null){
print(c.name);
}
}
);
This will not look for nested documents, though.

Mongodb new data insert in top

Usually mongodb saves previous data on top. Can we save data as new at the top?
Mean i want to insert my new data on 0 no index every time and pervious data will go down as Array.unshift() method...
Not sure if I got you right.
Within a document:
This could be helpful $position
Within a collection:
You could just sort by _id descending when accessing the data to get new documents first.

UpdateOneModel replaces existing document in mongodb java-driver

List<WriteModel<Document>> updateList =
new ArrayList<WriteModel<Document>>(documents.size());
documents.stream().forEach((document) -> {
updateList.add(new UpdateOneModel<Document>(
new Document().append("accountNum",
document.get("accountNum")),
new Document().append("$set", document)));
});
BulkWriteResult result = securitiesCollection.bulkWrite(updateList,
MongoDbConstants.ORDERED_OPTION_FALSE);
In above code, Im trying to update subset of attributes in a document. After update I see whole document is replaced with just the subset. Is there a way to update a subset of attributes using bulkwrite operations using mongo-java-driver.
If you want to update some field only, don't set the whole object:
new Document().append("$set", document)));
Instead, set fields you need only:
new Document().append("$set", new BasicDBObject("field1",document.getField1()).append("field2", document.getField2());
UpdateOneModel updates as expected, I was infact populating null values to other attributes, thats the reason other attributes were getting updated to null.

Delete all documents in a collection with Springdata mongo Query

I want to implement a delete method which deletes all the documents in a collection. I am using mongo db with Spring Data.This could be done using db.myCollection.remove({}) in Mongo shell. But I want to write a method in my data access layer to do this.I am not using MongodbTemplate in my Dao class. I want to know how can I do this with Query.
Query query = new Query();
Could anybody please tell me how can I do it.
You can use MongoTemplate directly
mongoTemplate.remove(new Query(), collectionName);
Use MongoRepository's deleteAll(). Utilizes mongoTemplate behind the scene to call the remove method.
From calling method someRepository.deleteAll()
Drop collection may be efficient as other answer has noted. For that you will need to use MongoTemplate directly and call dropCollection with entity class or collection name.
You can drop the collection and create it again:
mongoTemplate.dropCollection("collectionName");
mongoTemplate.createCollection("collectionName");
You'd better drop the entire collection (if possible) than deleting all documents. For performance and allocation sake.
You can try something such as:
MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 27017));
MongoDatabase db = mongoClient.getDatabase("test");
MongoCollection collection = db.getCollection("test_collection");
collection.drop();
If you do want to delete all (I think), instead of:
collection.drop();
use:
Bson filter = new Document();
collection.deleteMany(filter);
Query is part of spring-data-mongodb, if you can't use a MongoTemplate, probably Query is irrelevant as well.
If you have considerable changes in schema, better is to drop the collection.
MongoTemplate.dropCollection(collectionName).
But the requirement is to delete all documents you have use
MongoTemplate.remove(new Query(), collectionName)or
CrudRepository.deleteAll()
If all collections are to be deleted, this could also be done:
mongoTemplate.collectionNames
.forEach { mongoTemplate.dropCollection(it) }

Which is the best way to insert data in mongodb

While writing data to mongodb, we are checking if the data is present get the _id and using save update it else using insert add the data. Read save is the best way if you are providing _id in the query while saving it will update/insert based on if the _id is present in the db. Is the save the best method or is there any other way.
If you have all data available to save, just run update() each time but use the upsert functionality. Only one query required:
db.collection.update(
['_id' => $id],
$data,
['upsert' => true]
);
If your _id is generated by mongo you always know there is a record in the database and update is the one to use, but then again you could also save().
If you generated your id's (and thus don't know if it comes from the collection), this will always work without having to run an extra query.
From the documentation
db.collection.save()
Updates an existing document or inserts a new document, depending on its document parameter.
db.collection.insert()
Inserts a document or documents into a collection.
If you use db.collection.insert() in your case you will get duplication key error since it will try to insert new document which has same _id with an existing document. But instead of using save you should use the update method.