-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into solr-cursor
- Loading branch information
Showing
9 changed files
with
155 additions
and
98 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
modules/search-solr-client/src/main/scala/io/renku/search/solr/client/RenkuEntityQuery.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2024 Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.renku.search.solr.client | ||
|
||
import io.renku.search.solr.SearchRole | ||
import io.renku.search.solr.documents.DocumentKind | ||
import io.renku.search.solr.query.SolrQuery | ||
import io.renku.search.solr.query.SolrToken | ||
import io.renku.search.solr.schema.EntityDocumentSchema | ||
import io.renku.solr.client.* | ||
import io.renku.solr.client.facet.Facet | ||
import io.renku.solr.client.facet.Facets | ||
import io.renku.solr.client.schema.FieldName | ||
|
||
/** Convert the user query into a final query that is send to SOLR. */ | ||
object RenkuEntityQuery: | ||
private val typeTerms = Facet.Terms( | ||
EntityDocumentSchema.Fields.entityType, | ||
EntityDocumentSchema.Fields.entityType | ||
) | ||
|
||
private val creatorDetails: FieldName = FieldName("creatorDetails") | ||
private val namespaceDetails: FieldName = FieldName("namespaceDetails") | ||
|
||
def apply(role: SearchRole, sq: SolrQuery, limit: Int, offset: Int): QueryData = | ||
QueryData(QueryString(sq.query.value, limit, offset)) | ||
.addFilter( | ||
SolrToken.kindIs(DocumentKind.FullEntity).value | ||
) | ||
.addFilter(constrainRole(role).map(_.value)*) | ||
.withSort(sq.sort) | ||
.withFacet(Facets(typeTerms)) | ||
.withFields(FieldName.all, FieldName.score) | ||
.addSubQuery( | ||
creatorDetails, | ||
SubQuery( | ||
"{!terms f=id v=$row.createdBy}", | ||
"{!terms f=_kind v=fullentity}", | ||
1 | ||
).withFields(FieldName.all) | ||
) | ||
.addSubQuery( | ||
namespaceDetails, | ||
SubQuery( | ||
"{!terms f=namespace v=$row.namespace}", | ||
"(_type:User OR _type:Group) AND _kind:fullentity", | ||
1 | ||
).withFields(FieldName.all) | ||
) | ||
|
||
private def constrainRole(role: SearchRole) = role match | ||
case SearchRole.Anonymous => | ||
Seq(SolrToken.publicOnly) | ||
|
||
case SearchRole.User(id) => | ||
Seq(SolrToken.forUser(id)) | ||
|
||
case SearchRole.Admin(_) => | ||
Seq.empty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
.../search-solr-client/src/test/scala/io/renku/search/solr/client/RenkuEntityQuerySpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2024 Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.renku.search.solr.client | ||
|
||
import io.renku.search.model.Id | ||
import io.renku.search.solr.SearchRole | ||
import io.renku.search.solr.documents.DocumentKind | ||
import io.renku.search.solr.query.SolrQuery | ||
import io.renku.search.solr.query.SolrToken | ||
import io.renku.solr.client.QueryData | ||
import io.renku.solr.client.SolrSort | ||
import munit.FunSuite | ||
|
||
class RenkuEntityQuerySpec extends FunSuite: | ||
val adminRole: SearchRole = SearchRole.admin(Id("admin")) | ||
|
||
def query(q: String, role: SearchRole) = | ||
RenkuEntityQuery( | ||
role, | ||
SolrQuery(SolrToken.unsafeFromString(q), SolrSort.empty), | ||
10, | ||
0 | ||
) | ||
|
||
def assertFilter(q: QueryData, fq: String, fqn: String*) = | ||
(fq +: fqn).foreach { f => | ||
assert(q.filter.exists(_ == f), s"Expected filter query not found: $f [$q]") | ||
} | ||
|
||
def assertFilterNot(q: QueryData, fq: String, fqn: String*) = | ||
(fq +: fqn).foreach { f => | ||
assert(q.filter.forall(_ != f), s"Filter query found: $f") | ||
} | ||
|
||
test("amend query with auth data"): | ||
assertFilter( | ||
query("help", SearchRole.user(Id("13"))), | ||
SolrToken.forUser(Id("13")).value | ||
) | ||
assertFilter( | ||
query("help", SearchRole.Anonymous), | ||
SolrToken.publicOnly.value | ||
) | ||
assertFilterNot( | ||
query("help", adminRole), | ||
SolrToken.publicOnly.value | ||
) | ||
|
||
test("only full entities"): | ||
assertFilter( | ||
query("bla", adminRole), | ||
SolrToken.kindIs(DocumentKind.FullEntity).value | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters