I'm using Morphia 1.5.2 (Java 8) as a driver for MongoDB (V4.x), trying to use Search for a phrase, so my code looks like :
datastore.find(myEntity).disableValidation().search("\\\"" + textToFilter + "\\\"");
Debug looks good, but in running time the query is being sent with the three backslashes instead of just one, and the query return 0 results.
What am I missing? thanks!
actual generated query: "$text" : { "$search" : "\\\"filteredText\\\"" }
try this:
datastore.find(myEntity).disableValidation().search("\"" + textToFilter + "\"");
Copied and pasted from the official github issue tracker at https://github.com/MorphiaOrg/morphia/issues/1453 . I would have proposed this as an edit to a previous answer, as a reasonable person would have, but the moderators decided to delete the answer instead. Hope you weren't too delayed in getting your answer.
datastore.find(myEntity).disableValidation().search("\"" + textToFilter + "\"");
Thanks #evanchooly !
Related
I am trying to do a fairly simple Ebean query:
Long articleId = Long.parseLong(dataFetchingEnvironment.getArgument("id"));
System.out.println("Article ID");
System.out.println(articleId);
Article article = DB.find(Article.class).where().eq("id", articleId).findOne();
System.out.println("Article");
System.out.println(article);
Article ID prints to the console, articleId prints to the console
However everything after the query does not. Absolutely no logging at all.
I've tried to enable a logback.xml on all of the queries, I'm not seeing any errors.
What could be going on here? Clearly there's some problem going on that is not being made available for me to see - how do I debug this?
Apparently something like this works:
Article article = DB.find(Article.class)
.select("id, title, body").where().eq("id", articleId)
.findOne();
System.out.println("Article");
System.out.println(article);
Not entirely sure what the difference is, as the Ebeans documentation is a little sparse, but this works for me
see your log , io.ebean.SQL - txn[txnNum] -> your query , io.ebean.SUM - txn[txNum] -> find bean or not
if find -> row[1] :
io.ebean.SUM - txn[txNum] FindBean type[beanType] origin[DrFzdL.A.A] exeMicros[num] rows[1] bind[beanId, ]
if not find -> row[0] :
io.ebean.SUM - txn[txNum] FindBean type[beanType] origin[DrFzdL.A.A] exeMicros[num] rows[0] bind[beanId, ]
I am having trouble while trying to implement where condition with " punchDate like :date1 " in sql query using hibernate. Can any one please tell me what is the correct syntax to implement it.
String sql=select * from PunchHistory whered punchDate like :date1;
String date="2017-10-23";
List<PunchHistory> results =session.createQuery(StackperksConstants.sql)
.setDate("date1", java.sql.Date.valueOf(date))
.list();
Could anyone please help me.
Thanks in advance
Like is used with Strings, instead you have to use = with dates :
String sql="select * from PunchHistory where punchDate = :date1"
//-----------------------------------------------------^
List<PunchHistory> results =
session.createQuery(StackperksConstants.sql)
.setParameter("date1", java.sql.Date.valueOf(date), TemporalType.TIMESTAM)
//Instead you can use -----^
.list();
The better way is using the setParameter() method, for example:
session.createQuery(sql,Class.class).setPrameter(0,date,TemporalType.TIMESTAMP).list();
I am currently using spring-data-jpa version 1.9.4.
I have a MySql table with columns project(integer), summary(varchar), and description(varchar).
I have a regex that I would like to use to search the summary and/or description field meaning that if it finds it in summary does not need to apply regex to description.
The repository method I am attempting to use is:
List<Issue> findByProjectAndSummaryOrDescriptionRegex(long project, String regex)
The error I am receiving is:
java.lang.IllegalArgumentException: Unsupported keyword REGEX (1):
[MatchesRegex, Matches, Regex]
It is difficult in my company environment to update/upgrade versions, so if the issue is NOT my syntax but rather the then if someone knows which version now supports 'Regex' for query derivation or where I could find that specific information I would be grateful. I have looked at the Changelog and it appears that 1.9.4 should support but it appears not.
Thanks for your help!
JD
EDIT 1: I am aware of the #Query annotation but have been asked by my lead to only use that as a last resort if I cannot find the correct version which supports keyword REGEX [MatchesRegex, Matches, Regex]
I would recommend using native query (with #Query annotation) if the Spring data syntax does not work, e.g.:
#Query(nativeQuery=true, value="SELECT * FROM table WHERE project = ?1 AND (summary regexp ?2 OR description regexp ?2)")
List<Issue> findByProjectAndSummaryOrDescription(long project, String regex);
Update
If native query is not an option then (a) could you try it with single column and see if that works and (b) could you try by appending regex to both the columns, e.g.:
List<Issue> findByProjectAndDescriptionRegex(long project, String regex);
List<Issue> findByProjectAndSummaryRegexOrDescriptionRegex(long project, String regex, String regex);
In a followup, I discovered by doing some digging that the authoratative list will reside in the org.springframework.data.jpa.repository.query.JpaQueryCreator class. So for future folks that want to know which keywords from the 'Documented' list are ACTUALLY implemented, look inside JpaQueryCreator and you will the keywords supported as case arguments inside a switch!
Hope this helps!
PS - as suspected, REGEX was not supported in my version
try tu use #Query with param nativeQuery = true inside You can use database regexp_like function :
#Query(value = "select t.* from TABLE_NAME t where regexp_like(t.column, ?1)", nativeQuery = true)
Documentation :
https://www.techonthenet.com/oracle/regexp_like.php
I have mongodb aggregation query and it works perfectly in shell.
How can i rewrite this query to use with morphia ?
org.mongodb.morphia.aggregation.Group.addToSet(String field) accepts only one field name but i need to add object to the set.
Query:
......aggregate([
{$group:
{"_id":"$subjectHash",
"authors":{$addToSet:"$fromAddress.address"},
---->> "messageDataSet":{$addToSet:{"sentDate":"$sentDate","messageId":"$_id"}},
"messageCount":{$sum:1}}},
{$sort:{....}},
{$limit:10},
{$skip:0}
])
Java code:
AggregationPipeline aggregationPipeline = myDatastore.createAggregation(Message.class)
.group("subjectHash",
grouping("authors", addToSet("fromAddress.address")),
--------??????------>> grouping("messageDataSet", ???????),
grouping("messageCount", new Accumulator("$sum", 1))
).sort(...)).limit(...).skip(...);
That's currently not supported but if you'll file an issue I'd be happy to include that in an upcoming release.
Thanks for your answer, I can guess that according to source code. :(
I don't want to use spring-data or java-driver directly (for this project) so I changed my document representation.
Added messageDataSet object which contains sentDate and messageId (and some other nested objects) (these values become duplicated in a document which is a bad design).
Aggregation becomes : "messageDataSet":{$addToSet:"$messageDataSet"},
and Java code is: grouping("messageDataSet", addToSet("messageDataSet")),
This works with moprhia. Thanks.
I'm doing a website (an auction website) using java. I have one page to show the product in auction and I want to show 10 similar products.
To perform the search I'm using elasticsearch (by using the elasticsearch java implementation dadoonet).
One requirement I have is to show only the 10 similar documents that has date > now.
I say the elasticsearch documentation and I found the query "More like this" but first I'm not getting this to work using:
new MoreLikeThisRequest("auction").searchSize(size).id(productId + "").fields(new String[] { "name", "description", "brand" }).type("string");
Because is always showing the error:
org.elasticsearch.index.engine.DocumentMissingException: [_na][_na] [string][2]: document missing
And I'm not find the way to filter the date.
Someone can point me on the right way to do this?
thks
My best bet would be that you have the wrong id and I also see that you are missing the type. To use more like this, you have to provide the document to use. This is defined by the combination of index,type and id. If you do not specify the document right, elasticsearch cannot find the document and that is most probably why you get the document missing message.
In java I would do something like this:
FilteredQueryBuilder queryBuilder =
new FilteredQueryBuilder(
QueryBuilders.matchAllQuery(),
FilterBuilders.rangeFilter("datefield").lte("now")
);
SearchSourceBuilder query = SearchSourceBuilder.searchSource().query(queryBuilder);
client.prepareMoreLikeThis("index","type","id")
.setField("field1","field2")
.setSearchSource(query)
.execute().actionGet();
So after strugling a little bit I found someone with the same problem. So his suggestion was to set the min_term_freq to 1.
So the code now looks like this:
FilteredQueryBuilder queryBuilder = new FilteredQueryBuilder(QueryBuilders.matchAllQuery(), FilterBuilders.rangeFilter("finish_date").lt("now"));
SearchSourceBuilder query = SearchSourceBuilder.searchSource().query(queryBuilder);
SearchResponse response = esClient.prepareMoreLikeThis("auction", "product", productId + "").setField("name.name", "description", "brand").setPercentTermsToMatch(0.3f)
.setMinTermFreq(1).setSearchSource(query).execute().actionGet();
But I dont know what this MinTermFreq does and if the value 1 is the right value. Someone know what is this field?
Thks for all the help!
Once again, Thank you for all the help and sorry for all the trouble!