I am using Spring data elasticsearch to query in my elastic documents.
My Elasticsearch entity class:
#Document(indexName="keywords")
#Getter
#Setter
public class ESKeywords {
#Id
private Long id;
#Field(type = FieldType.Text, name = "text")
private String text;
}
Indexed row in elastic search:
{
"id": 118390,
"text": "top 20 tweets of",
}
For Example searching for "top 20 tweets of ABC".
It gives different results when I Use directly elastic search port it gives:
And with the code(ElasticsearchRepository.findByText()) it gives empty result.
thanks in advance
Probably you just don't have one.
keyword field type should not be used for full text search - instead use text
Read also:
https://www.elastic.co/guide/en/elasticsearch/reference/current/keyword.html
Related
I am new to MongoDB and have trouble. I would be very grateful if someone could help me with it.
I have the entity like:
class FileRecord {
private ObjectId id;
private String fileId;
private EnumStatus status;
private Long userId;
private Integer year;
etc.
> }
A file can have many records but only one in the year.
I need to get all the last documents by criteria.
I don't understand how to make it right in Spring, but in the mongo query too if to be honest)
I understand how to make criteria for it:
var match = match(Criteria.where("userId").in(userIds).andOperator(Criteria.where("status").in(EnumStatus.availableStatues())));
And group by max:
var group = group("fileId", "year").max("year").as("max_year");
But how to make it to query in one that will return for me Colletion<FileRecord> after aggregation?
If I try it make it this way:
var aggregation = newAggregation(filter, group);
AggregationResults<FileRecord> aggregateResult =
mongoOperations.aggregate(aggregation, FileRecord.class, FileRecord.class);
Collection<FileRecord> records = aggregateResult.getMappedResults()
I get an exception:
readObjectId can only be called when CurrentBSONType is OBJECT_ID, not when CurrentBSONType is DOCUMENT.;
If someone has an idea I'm interested
Thank you in advance
I found the answer)
var group = group("fileId", "year").max("year").as("max_year").first("_id").as("enityId");
I needed to create the projection and after again make request to the DB and get the data that I needed by id. I don't have many of them, so the solution worked for me. But, as I understand, you can add a lot "first", and add the fields you need. And get all by one request
I have a query that would be fairly simple using SQL databases. However, in MongoDB, I'm not sure on how to do it. I am building a notification system. I have a collection that stores notifications, and another that creates a document when the user has seen the notification, as such:
notifications collection:
_id: 1
content: "some content"
targetGroup: "somegroup"
seen-notification collection
_id: 1
notificationId: 1
userName: "johndoe"
I'm building an endpoint that should return all notifications in a specific group, that the user has not already seen. In a SQL pseudo-code, I'm trying to do:
SELECT
*
FROM
notifications AS n
INNER JOIN
seen-notification AS sn
ON n._id = sn.notificationId
WHERE
sn.notificationId IS NULL
AND n.targetGroup = "somegroup"
So, basically, a right join with a where clause. I'm using spring data with a MongoRepository. Right now, i'm making two different selects and iterating through them to remove the already seen. I've looked into mongo's $lookup and several SO questions regarding it, but I just couldn't get it work with spring data and MongoRepository. I'm fairly new to mongo with spring data, so maybe there's a way to accomplish this with $lookup and I just didn't figure out how.
try
https://mongoplayground.net/p/-dZHmP9yj-c
expects collection names of notifications and seenNotifications
#Aggregation(pipeline = {
"{$match: { targetGroup: ?0 } }",
"{$lookup: { from: \"seenNotifications\", localField: \"_id\", foreignField: \"notificationId\", as: \"seenNotificationsDocs\"}}",
"{$match: { seenNotificationsDocs: { $size: 0 } } }",
"{$project: { seenNotificationsDocs: 0 } }",
})
List<Notifications> findUnseenNotificationByTargetGroup(String targetGroup);
#Document("notifications")
public class Notifications {
#Id
private Integer id;
private String content;
private String targetGroup;
//getter & setter
}
I am trying to retrieve some data I stored before from Solr. This is my model.
#SolrDocument(solrCoreName = "images")
public class Imagen {
#Id
#Field
private String id;
#Field
public String origin_url;
#Field
public String[] predominantColor;
#Field
public double predominantColor_hue;
If I want to get all the "Imagen" where predominantColor_hue is near of the value I enter, how can I make a customized query for that? Because I just tried to use the
public interface ImagenRepository extends SolrCrudRepository<Imagen, String> {
Imagen[] findByPredominantColor_hue(double predominantColor_hue);
}
Also tried this
Imagen[] findByPredominantColor_hueIsLessThanAndPredominantColor_hueIsGreaterThan(double predominantColor_hue1, double predominantColor_hue2);
but I get this error:
No property hue found for type String! Traversed path: Imagen.predominantColor.
Maybe if I can make a custom query I can say to Solr that I want the Imagen which have a predominantColor_hue close to the one I want to compare. Maybe +5 and -5. But, how can I make that query? Already googled a lot and nothing found. Thanks and SORRY for my English!
Couple of suggestions,
Rename your predominantColor_hue field to predominantColorHue, because _ has a special meaning in spring-data queries.
Your Query should be like this
List<Imagen> findByPredominantColorHueGreaterThanAndPredominantColorHueLessThan(double predominantColor_hue1, double predominantColor_hue2);
If you want to get values near x, Now you can use this method like this
findByPredominantColorHueGreaterThanAndPredominantColorHueLessThan(x-5, x+5)
I have a kind named Posts and the key is postId.
The postId format is : YYMMDDXXX. Where XXX is 3 digits sequence number.
For example : 150703001, 150704001, 150704002
How do I get the sequence number from my entities in datastore? I want to convert this SQL Select nvl(max(substring(postId, 7, 3)), 0) from posts where substring(postId, 1, 6) = '150704' to Objectivy filter.
Please help, thanks a lot!
You will have to re-design your Kinds/Entities into a different structure to accomplish this. The datastore can not search by substring.
A possible approach could be this layout :
#Entity
class PostGroup {
#Id
String id; // this is will hold the formatted date YYMMDD
}
#Entity
class Post {
#Id
Long id; // auto generated ID
#Index
Long sequence; // the sequence number
#Parent
Key<PostGroup> group;
}
Now you can set the PostGroup has the ancestor for your Objectify query, sort by Post.sequence desc limit 1 to find the latest post.
String yymmdd = "150704";
Key<PostGroup> postGroupKey = Key.create(PostGroup.class, yymmdd);
Query<Post> query = ofy().type(Post.class).ancestor(postGroupKey).order("-sequence");
Post latestPost = query.first().now();
Note: you don't have to actually persist the PostGroups to use them as ancestors.
I am trying to create a hibernate full text search using hibernate-search-4.3.0.Final.jar
There is no errors in this application, but my Lucene query unsing the query DSL doesn't return any results.
I mean It doesn't return any of rows in the table. can any one please help me.
This is my function:
OgmConfiguration cfgogm=new OgmConfiguration();
cfgogm.configure("hibernate.cfg.xml");
serviceregistry=new ServiceRegistryBuilder().applySettings(cfgogm.getProperties()).buildServiceRegistry();
sessionfactory=cfgogm.buildSessionFactory(serviceregistry);
Session session= sessionfactory.openSession();
FullTextSession fulltextsession= Search.getFullTextSession(session);
QueryBuilder querybuilder=fulltextsession.getSearchFactory().buildQueryBuilder().forEntity(User.class).get();
org.apache.lucene.search.Query lucenequery=querybuilder.keyword().onField("IdU").matching("96645").createQuery();
org.hibernate.search.FullTextQuery fulltextquery=fulltextsession.createFullTextQuery(lucenequery, User.class);
List result=fulltextquery.list();
System.out.println(result.toString());
and this is my POJO class:
#Entity
#Table(name="Users")
#Indexed
public class User {
#Id
#GeneratedValue(generator="mongodb_uuidgg")
#Field(index = Index.YES,analyze = Analyze.NO,store = Store.NO)
private String _id;
#Column(name="City")
#Field(index = Index.YES,analyze = Analyze.NO,store = Store.NO)
private String city;
#Column(name="UserID")
#Field(index = Index.YES,analyze = Analyze.NO,store = Store.NO)
private int IdU;
...
I would use Luke to verify that your queries actually return what you want from the index.
[edit...]
If Luke shows that the index is empty, you will need to look at your indexing setup.
Most likely, the configuration for the id field is incorrect. It should be:
#Id
#GeneratedValue(generator="mongodb_uuidgg")
#DocumentId
private String _id;
(i.e. #DocumentId instead of #Field).
Have you actually indexed the data? You need to create the initial index, if you start with an existing database. You can use the programmatic index API and the mass indexer to create the initial index. Once you have the initial index and you are you use incremental indexing the index will stay in sync with database changes (provided changes are made via the Hibernate/JPA API).
Other than that have look at the lg file. Is there anything in there? If you still have the issue please post the code you use to index the data.