Replace entire document in Bulk Operation Spring MongoDB - spring-data-mongodb

I am using Spring Boot Version 2.1.0 and mongo driver version 3.8.2. I am not able to replace the entire document during bulk update operation.
It was working fine before upgrading to Spring Boot version 2.1.0. I was using ops.find().replaceOne() in spring boot 1.5.9
I tried BulkOps.updateOne.. But it is throwing Invalid Bson Name _id.
I tried dbObject.remove("_id"); But still getting the error with other fields.
BulkOperations ops = mongoOperations.bulkOps(BulkOperations.BulkMode.UNORDERED, A.class);
list.forEach(item -> {
Document dbObject = new Document();
converter.write(item, dbObject);
ops.updateOne(query(where("_id").is(item.getId())),Update.fromDocument(dbObject));
});
ops.execute();
Expected -- it should replace the entire document matching the ID.

final BulkOperations bulkOperations =
mongoTemplate.bulkOps(BulkOperations.BulkMode.ORDERED,A.class, A);
final Query query = new Query();
query.addCriteria(Criteria.where("_id").is(A.getId()));
bulkOperations.replaceOne(query,A);
bulkOperations.execute();

Related

What is the proper way to test SpringBoot Database with H2?

I am trying to run Springboot test with H2 in-memory db with Mybatis.
So far I have done
configuring h2 DB in application-test.properties
adding annotation
#SpringBootTest, #TestPropertySource (locations = "TEST_APPLICATION_PROPERTIES_LOCATION")
autowiring dao and serviceImpl beans
adding seed.sql and purge.sql to the test class with
#SqlGroup({
#Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:/database/seed.sql"),
#Sql(executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, scripts = "classpath:/database/purge.sql") })
Despite the above measures, I still have two problems
I can't retrieve user that I input with the seed.sql. I made a user with id="admin", pw="admin", and was trying to retrieve with findById("admin"). But it always returns null.
I can't open h2 DB while debugging with the #test. I simply can't access h2 with localhost:8080/h2-console (the path was written explicitly in application-test.properties)
Is there any extra measure that I should take to test SpringBoot with h2?
Add spring.h2.console.enabled=true in your properties file.

How do we get the document file url using the Watson Discovery Service?

I don't see a solution to this using the available api documentation.
It is also not available on the web console.
Is it possible to get the file url using the Watson Discovery Service?
If you need to store the original source/file URL, you can include it as a field within your documents in the Discovery service, then you will be able to query that field back out when needed.
I also struggled with this request but ultimately got it working using Python bindings into Watson Discovery. The online documentation and API reference is very poor; here's what I used to get it working:
(Assume you have a Watson Discovery service and have a created collection):
# Programmatic upload and retrieval of documents and metadata with Watson Discovery
from watson_developer_cloud import DiscoveryV1
import os
import json
discovery = DiscoveryV1(
version='2017-11-07',
iam_apikey='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
url='https://gateway-syd.watsonplatform.net/discovery/api'
)
environments = discovery.list_environments().get_result()
print(json.dumps(environments, indent=2))
This gives you your environment ID. Now append to your code:
collections = discovery.list_collections('{environment-id}').get_result()
print(json.dumps(collections, indent=2))
This will show you the collection ID for uploading documents into programmatically. You should have a document to upload (in my case, an MS Word document), and its accompanying URL from your own source document system. I'll use a trivial fictitious example.
NOTE: the documentation DOES NOT tell you to append , 'rb' to the end of the open statement, but it is required when uploading a Word document, as in my example below. Raw text / HTML documents can be uploaded without the 'rb' parameter.
url = {"source_url":"http://mysite/dis030.docx"}
with open(os.path.join(os.getcwd(), '{path to your document folder with trailing / }', 'dis030.docx'), 'rb') as fileinfo:
add_doc = discovery.add_document('{environment-id}', '{collections-id}', metadata=json.dumps(url), file=fileinfo).get_result()
print(json.dumps(add_doc, indent=2))
print(add_doc["document_id"])
Note the setting up of the metadata as a JSON dictionary, and then encoding it using json.dumps within the parameters. So far I've only wanted to store the original source URL but you could extend this with other parameters as your own use case requires.
This call to Discovery gives you the document ID.
You can now query the collection and extract the metadata using something like a Discovery query:
my_query = discovery.query('{environment-id}', '{collection-id}', natural_language_query="chlorine safety")
print(json.dumps(my_query.result["results"][0]["metadata"], indent=2))
Note - I'm extracting just the stored metadata here from within the overall returned results - if you instead just had:
print(my_query) you'll get the full response from Discovery ... but ... there's a lot to go through to identify just your own custom metadata.

Solr 4.3.1 Data-Import command

I'm currently using Solr 4.3.1. i have configured dih for my solr. i would like to do a full import through command prompt. I know the url will be something like this http://localhost:8983/solr/corename/dataimport?command=full-import&clean=true&commit=true is there any method i can do this without using curl ?
Thanks
Edit
string Text = "http://localhost:8983/solr/Latest_TextBox/dataimport?command=full-import&clean=true&commit=true";
var wc = new WebClient();
var Import = wc.DownloadString(Text);
Currently using the above code
Call it like a normal REST url that's it !! I am using it in my application for importing and indexing data from my Local drive and it just works fine ! :) . Use HttpURLConnection to make a request and capture response to see whether it was successful or not . You don't need any specific API to do that . This is a sample code to make a GET request correctly in C# .Try data import handler url with this, it may work !
Console.WriteLine("Making API Call...");
using (var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }))
{
client.BaseAddress = new Uri("https://api.stackexchange.com/2.2/");
HttpResponseMessage response = client.GetAsync("answers?order=desc&sort=activity&site=stackoverflow").Result;
response.EnsureSuccessStatusCode();
string result = response.Content.ReadAsStringAsync().Result;
Console.WriteLine("Result: " + result);
}
Console.ReadLine();
}
}
}
You'll have to call the URL in some way - Solr only operates through a REST API. There is no command line API (the command line tools available just talk to the API). So use your preferred way to talk to a HTTP endpoint, that being curl, wget, GET or what's available for your programming language of choice.
The bundled solrCli application does not have any existing command for triggering a full-import as far as I were able to see (which would just talk to the REST API by calling the URL you've already referenced).

Get path to Solr instanceDir with solrj

How can I get path to an instanceDir with solrj?
I know the path to SOLR_HOME and I know the actual name of the collection but the actual instanceDir of the collection might be different.
e.g. the collection with the name "foo" maybe in the directory solr_home/foobar_1/ or in solr_home/foobar_0/
So how can I find out if the directory of this collection is called foobar_1 or foobar_0 with solrj?
Your question is how to get the InstanceDir of a solr core with solrj.
The corresponding URL to get this information is:
via Core Admin API:
http://localhost:8983/solr/admin/cores?action=STATUS&core=myCore
or via Core Overview: http://localhost:8983/solr/myCore/admin/system
This code use solrj to get the information:
SolrClient client = new HttpSolrClient("http://localhost:8983/solr/");
String core = "myCore";
// Core Admin API
CoreAdminRequest status = new CoreAdminRequest();
status.setAction(CoreAdminAction.STATUS);
status.setCoreName(core);
CoreAdminResponse rspStatus = status.process(client);
String instanceDirCore = (String) rspStatus.getCoreStatus().findRecursive(core, "instanceDir");
System.out.println(instanceDirCore);
// Core Overview
GenericSolrRequest system = new GenericSolrRequest(METHOD.GET, "/admin/system", new ModifiableSolrParams());
SimpleSolrResponse rsp = system.process(client, core);
String instanceDir = (String) rsp.getResponse().findRecursive("core", "directory", "instance");
System.out.println(instanceDir);
assert instanceDir.equals(instanceDirCore);

Spellchecking w SolrNet 400 Bad Request using colon : in spellcheck query

I'm using SolrNet and when I pass a query that has a colon in it, I receive this error:
Apache Tomcat/6.0.32 - Error report
The request sent by the client was syntactically incorrect (null).
The code sample looks like this:
var results = SolrOps.Query(request.Query, new QueryOptions
{
SpellCheck = new SolrNet.Commands.Parameters.SpellCheckingParameters
{
Collate = true,
Build = true,
Query = "test:"
}
});
Am I required to strip the colon manually, or is there some option I can set (either in SOLR or SOLRNet) to fix this?
I am thinking that spellchecking does not support special characters and you will need to filter/strip those out of the query term(s) prior to submitting the request to Solr via SolrNet.

Resources