How to configure type name or filter by type query with Elasticsearch integration? - hibernate-search

I'm using Hibernate Search with Elasticsearch integration with the versions 5.8.2 and 5.6 respectively.
Let's say I have 2 applications which use the same database and I want to search for Person entity. Both of the applications will update indexes but only one of them will read.
Since, by default Hibernate Search uses fully qualified class names for types, I end up with 2 different types for the same entity like:
Person -> com.example.x.Person
Person -> com.example.y.Person
In the application I'm performing the search, while constructing the search query, Hibernate Search automatically add filter query for type like:
"filter": {
"type": {"value": "com.example.x.Person"}
}
thus search results don't contain the records from com.example.y.Person type.
Is there any way that I can customize type names?
Or, is there any way to disable/customize added filter query?

You can't do it for now.
Funnily enough, we just had a very similar question a few days ago: Override Elasticsearch _type in Hibernate Search .
My advice is the following: if it's the exact same entity, you should extract it in a common jar and use the same entity in both of your applications (if you cannot extract it, you can still use the same package but I would recommend to extract it so that you're sure it's exactly the same).

Related

How to fetch distinct list from SpringData without using query annotation?

I am using 2 different properties to fetch distinct list eg
findDistinctBy<propertyName>And<propertyName>In(List<String> list).
My actual Spring jpa statement is
List<PojoClass> findAllByTpIdInAndDistinctMobile(List<String> edgeIds);
where TpId & Mobile are 2 different properties in PojoClass. I need to implement this without using Query annotation. Any suggestions of queryDsl will also do.
The question needs to be more clear.What exactly do you intend to achieve when the input is a list of Ids?
There is no option to provide a list to findAll unless it is an Id.
A simple way would be to loop the the input list in service and append the result to a result list.
for(String edgeId:edgeIds){
resultList.addAll(findByEdgeId(edgeId));
}
Updated code after #Tejas comment
for(Pojo pojo:Pojos){
resultList.addAll(findDistinctPojoByPropert1OrPropert2(String pojo.getProperty1(),String pojo.getProperty2());
}

Select * for Github GraphQL Search

One of the advantage of Github Search v4 (GraphQL) over v3 is that it can selectively pick the fields that we want, instead of always getting them all. However, the problem I'm facing now is how to get certain fields.
I tried the online help but it is more convolution to me than helpful. Till now, I'm still unable to find the fields for size, score and open issues for the returned repository(ies).
That's why I'm wondering if there is a way to get them all, like Select * in SQL. Thx.
GraphQL requires that when requesting a field that you also request a selection set for that field (one or more fields belonging to that field's type), unless the field resolves to a scalar like a string or number. That means unfortunately there is no syntax for "get all available fields" -- you always have to specify the fields you want the server to return.
Outside of perusing the docs, there's two additional ways you can get a better picture of the fields that are available. One is the GraphQL API Explorer, which lets you try out queries in real time. It's just a GraphiQL interface, which means when you're composing the query, you can trigger the autocomplete feature by pressing Shift+Space or Alt+Space to see a list of available fields.
If you want to look up the fields for a specific type, you can also just ask GraphQL :)
query{
__type(name:"Repository") {
fields {
name
description
type {
kind
name
description
}
args {
name
description
type {
kind
name
description
}
defaultValue
}
}
}
}
Short Answer: No, by design.
GraphQL was designed to have the client explicitly define the data required, leading to one of the primary benefits of GraphQL, which is preventing over fetching.
Technically you can use GraphQL fragments somewhere in your application for every field type, but if you don't know which fields you are trying to get it wouldn't help you.

Sulu CMS: how to search/filter for content of a specific type with specific values for specifc attributes?

Short description of the situation:
We're running a forked version of Sulu 1.5.2, PHP 7.1, Windows server environment, db connection with PostgreSQL
We have a website structure/tree where we have house templates at the top level; each house has one house_rooms and one house_occupants template; each house_rooms template has N house_rooms_room templates, and each house_occupants template has N house_occupants_occupant templates. This represents an actual House that has N Rooms and N Occupants.
Now I'd like to know if there is a way to specifically get, for instance, all the house_occupants_occupant content that follows a certain pattern of attributes (for instance: their gender attribute having value 'female' and their date_of_birth parameter being >= 1990/01/01), without having to load each house, then find its house_occupantspage among the children, and then loop over that template's house_occupants_occupant children and filter the thus begotten content according to their gender and date of birth attributes.
I already found that there is a ContentRepository class that can ::findAll() and ::findByUuids(), but there doesn't seem to be a way to filter on specific attributes (like template type, template attributes, ...). So I took a roundabout way of creating my own "repository" that does direct PDO queries on the phpcr_nodes table in the database, to specifically scan the props attribute for the occurence of a certain template name:
$this->pdo->query("SELECT identifier, props FROM phpcr_nodes WHERE props LIKE '%>house_occupants_occupant<%'");
I can see that the propscontains a string value representing an XML document that somehow translates into the entire template with attribute-value pairs, however it is obscured regarding tag-levels and how certain attributes relate to certain values. So in theory I could use a specific XML parser to turn this into something human-readable, so that for my house_occupants_occupant data I could get something like:
// what I would get after putting the props through a certain XML parser:
$xmlHumanReadableData = [
'<the_uuid_of_occupant_1>' => [
...
'gender' => 'female',
'date_of_birth' => '1992-05-18T00:00:00.000+00:00',
...
],
... //etcetera etcetera
];
When I would have that, I could filter the readable data to ascertain which content I want to keep, add the node-uuid to some $theUuids variable, and then retrieve the actual content using Sulu's ContentRepository::findByUuids($theUuids) method. That would "only" require 2 queries and some PHP array filtering in between, which is a great deal better than looping over all the children content starting from a certain parent and doing this until you've traversed all the parents and all their children... (Certainly, the overhead would increase if you'd want to search for, for instance, all house nodes where at least one of its house_occupants_occupant nodes represents a child less than 10 years old, since you'd need extra queries to "set up" the filterdata used in the final query. But still: a great deal better than looping everything... ;-) )
So my question is sort-of twofold:
What is the Sulu-specific XML parser I can use to turn the XML string value in this props column into something human-readable, with proper attribute-value pairs?
And/or, hopefully: is there a way I can avoid all this nonsense and just use a less low-level way of retrieving content of a specific template type with specific values for specific attributes ?
The ContentRepository you've found is already an abstraction to some of our requirements for pages. Your requirements are already quite specific, so you should write your own query using SQL-2, the query language for PHPCR.
This should enable you to write a query which matches your requirements.

Algolia Tags vs Facets Use Cases

New to Algolia, and having a bit of trouble deciphering the difference (suggested use) of tags vs. facets -- they seem to be functionally equivalent.
The Algolia documentation gives one example of a tag with a user ID -- e.g. "user_1234", which could then be used for filtering.
However that seems functionally equivalent to simply having this in your JSON:
"user": "1234"
and then declaring "user" as a faceted field.
What's the difference / purpose? Why have both tags and facets?
You're indeed correct that both can give you the same filtering functionality.
The main difference comes from facet counts that are computed at indexing time, which takes time.
That's why you can now add in your attributesForFaceting setting an onlyFilter modifier to your attribute, like so:
{
attributesForFaceting: [
'onlyFilter(user)'
]
}
This will tell the engine that the user attribute should be considered as a tag or tag list (this syntax is currently undocumented, but should soon be).
The same logic can be applied to numeric attributes. By default, the Algolia engine creates data structures for all numbers indexed in order to quickly answer to queries like nb_views>10000.
This is also computation-heavy, which is why you can add the equalOnly modifier in the numericAttributesToIndex.

Writing a query with ORMLite

How can I write a query with ormlite instead of using .create or any other thing like that? Can you please show me how for this simple example :
SELECT name FROM client
EDIT since I can't answer myself :
I guess I had to search a little more , anyway I found how to do it with the QueryBuilder like this :
newDao.query(newDao.queryBuilder().where.eq("name",valueofname)
If someone knows how to write the full query that would be great , otherwise , I'll stick with this solution
How can I write a query with ormlite instead of using .create or any other thing like that?
Goodness, there are tons of documentation about how to do this on the ORMLite site. Here's the section on the query builder.
I'm not sure what you mean by "full query" but your example will work with some tweaks:
List<...> results = newDao.queryBuilder().where().eq("name",valueofname).query();
It does not make sense to just return the name since the Dao hierarchy is designed to return the specific Client object. If you just want the name the you can specify the name column only to return:
... clientDao.queryBuilder().selectColumns("name").where()...
That will return a list of Client objects with just the name field (and the id field if it exists) extracted from the database.
If you just want the name strings then you can use the RawResults feature.