MongoDB Spring Data Hashmap query search for numeric key - spring

The following json data is a part of a document:
"ahashMap" : {
"1" : {
"type" : "STOCK"
}
}
To query above document in MongoDB shell, simply execute the script below:
db.COLS.findOne({'aHashMap.1.type':'STOCK'})
Moreover, to query using Spring Data, it also works for the below codes:
Query q = new Query(Criteria.where("aHashMap.1.type").is("STOCK"));
Col c = mongoOperation.findOne(q, Col.class);
However, it only works for older version of Spring Data, such as 1.7.0. Then I tried to compare the source code with old and new version. For the newest version such as 1.7.2, Spring Data removes the .digit from the query so no result would be found. It would treat as the below query for MongoDB:
db.COLS.findOne({'aHashMap.type':'STOCK'})
I feel confused for this kind of interpretation for Spring Data. Anyone would explain the reason behind and how would I process the query without restructure the document. Thank you.

This bug is fixed in Spring Data MongoDB 1.8.0

Related

How to perform a Join query on Elastic Search via springboot/java?

I have a springboot application that interacts with elastic search (or as it know now OpenSearch). It can perform basic operations such as search, index etc. I used this as my base (although I replaced high level client since it is deprecated) and to perform queries, I am using #Query annotation mostly (as described in section 2.2 here, although I also used QueryBuilders).
Now, I have an interesting use case - I would like to perform 2 queries at the same time. First query would find a file in elastic search that would contain 3 ids. These 3 ids are ids of other files in the same elastic search. The 2nd query would look for these 3 files and finally return them to me. Now, I can easily do it in 2 steps:
Have a query to find a file containing 3 ids and return it
Have a second query (multisearch query can do bulk search as I understand) to search
for 3 files using info from the first query.
However, I need them to happen within the same query - so within the same query I need to search for a file containing the 3 ids and then perform a search for these 3 files.
So currently my files in elastic search look like so:
{
"docId": "docId57",
"relatedDocs": [
{
"relatedId": "docId1",
"type": "apple"
},
{
"relatedId": "docId2",
"type": "orange"
},
{
"relatedId": "docId3",
"type": "banana"
}
]
}
and my goal is to have a query that will accept docId57 as an arg (so a method findFilesViaJoin(docId57) or something) and return a list of 3 files: file for docId1, file docId2 and file for docId3.
I know it is possible either via nested queries, child/parent queries or good old SQL queries (via jpa/hibarnate).
I attempted to use all of these and was unsuccessful for reasons described below.
Child/parent queries
So for child/parent queries, I attempted to use DSL with #Query but couldn't quite get it since I don't have a solid documentation to refer to (the one that actually helps with java not curls). After some time I found this and this articles - I maybe can figure out how to make it work with child/parent but neither explain how to do mapping. If this approach can do what I want, my question is: how to set up & map parent/child in springboot.
Using SQL queries
So for this one, I need to change my set up to use hibarnate. I used this as my base. It works, the only problem I have is that my SQL queries get ignored. Instead, the search is done based of a method's name, not the content of #Query. So it is as if I don't have an annotation used at all. So using the structure mentioned above, the following method in my app:
#Query("select t from MyModel t where t.docId = ?1")
findByRelatedDocsRelatedId(String id)
will return files that has a relatedId that matches the id passed via method ard id (as oppose to reading query from #Query that tells method to search all docs based on docId). Now, I don't mind using method name as a query to search for something. But then why would I use #Query for? (not to mention how do I create a name that does join). It might be possible that my hibernate is set up wrong (never used it before this week). So question here is, does anybody have a nice complete example of hibarnate being used with elastic search that does join query?
Nested queries
For these queries, I assume that I just need to figure out what to put inside the #Query but due to limited documentation about how to compose nested query I didn't manage to make it even remotely to work. Any concreate documentation on how to create DSL nested query would be appreciated.
Any of the ways I described will work for me. I think child/parent seems the best choice (seeing as they kind created for this purpose) but any will do.

using queries generated by name with Spring Boot

I am trying to write a query in Spring Boot, I have the query written like this
, but when I search for the vehicle objects by year by doing http://localhost:8080/vehicles?year=(year) it returns to me all of the vehicle objects instead of only ones that match the year.
Try this
http://localhost:8080/vehicles?year=2001
If still error try your query in sql command, i think any mistake in your query or check again in your controller file

Does Elasticsearch deletes all documents using delete_by_query, in any case, except query says match all?

{
"query": {
"terms": {
"MyId": [123 ]
}
}
}
Is there, any chance, to delete all documents in elastic search index. by executing this query. Please suggest. Currently all indexed documents have been deleted. Which i could not able to trace/ find reason. your concern is much valuable at this moment. Thank you in advance.
my code :
var deleteQueryResponse = ClientES.Client.LowLevel.DeleteByQuery(databaseName, datatableName, postData);
where postData is above query.
I found
https://www.elastic.co/guide/en/elasticsearch/reference/6.1/breaking_60_rest_changes.html
Under Secton, Delete by query requires explicit query.
I am using elastic search version 5.5.1. Delete_by_query was working as expected. But suprisingly today, its deleting all the documents(which is the default case). how come this changes is effective today. I need reason.
Elasticsearch query built using c# string, was incorrect JSON. But _delete_by_query didn't throw any exception instead by default it deleted all the records.
I found https://www.elastic.co/guide/en/elasticsearch/reference/6.1/breaking_60_rest_changes.html Under Secton, Delete by query requires explicit query.

How do I combine Facet and FilterQueries using Spring data Solr?

Is it possible to combine a facet and field query in spring data solr? Something that would build a query like this:
> http://localhost:8983/solr/myCore/select?q=lastName%3AHarris*&fq=filterQueryField%3Ared&wt=json&indent=true&facet=true&facet.field=state
In other words, how do I add FilterParameters to a SimpleFacetQuery?
Any/all replies welcome, thanks in advance,
-- Griff
I assume you're using Spring Data Solr, from your reference to SimpleFacetQuery. Based on your sample query, the code would look something like this:
// creates query with facet
SimpleFacetQuery query = new SimpleFacetQuery(
new Criteria("lastName").startsWith("Harris"))
.setFacetOptions(new FacetOptions()
.addFacetOnField("state"));
// filter parameters on query
query.addFilterQuery(new SimpleFilterQuery(
Criteria.where("filterQueryField").is("red")));
// using query with solrTemplate
solrTemplate.queryForFacetPage(query, YourDocumentClass.class)

mongo subtract two fields

I have a mongo collection which has documents with two fields "fieldA" and "fieldB", which are timestamps and I need to get all the documents which are "(fieldB-fieldA)/6000 > 2"... So I was looking for some function in order to do this...
I saw in some posts the function "$subtract" but that seems is in mongo 2.1.x and currently I'm using 2.0.x (it's the stable), any idea how to do this with 2.0.x? or Do I need to create a new field? or Can I only do it in the application side?
btw, I'm using the ruby mongo driver...
You can use the $where operator to specify query expressions as Javascript. In the shell:
db.myCollection.find( { $where: "(this.fieldB - this.fieldA)/6000 > 2" } );
You should note, however, that Javascript execution can be very slow. If this is a one time query, $where may be an ok solution. Otherwise, storing the result of the equation in a separate field is the best way to ensure that queries are timely.
Additional information on the $where operator:
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-JavascriptExpressionsand%7B%7B%24where%7D%7D

Resources