Skip to content

Commit 7b5e1f2

Browse files
committed
Fixed retrieve query
1 parent e91fd60 commit 7b5e1f2

File tree

3 files changed

+30
-51
lines changed

3 files changed

+30
-51
lines changed

src/main/scala/de/upb/cs/swt/delphi/webapi/MavenIdentifier.scala

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import de.upb.cs.swt.delphi.webapi.artifacts.Identifier
2323

2424
case class MavenIdentifier(val repository: Option[String], val groupId: String, val artifactId: String, val version: Option[String]) extends Identifier {
2525

26-
def toUniqueString = {
27-
repository + ":" + groupId + ":" + artifactId + ":" + version
26+
def toUniqueString: String = {
27+
repository.getOrElse(MavenIdentifier.DefaultRepository) + ":" + groupId + ":" + artifactId + ":" + version.getOrElse("")
2828
}
2929

3030
override val toString: String = groupId + ":" + artifactId + ":" + version
@@ -38,7 +38,7 @@ case class MavenIdentifier(val repository: Option[String], val groupId: String,
3838
}
3939

4040
private def constructArtifactBaseUri(): URI =
41-
new URI(repository.getOrElse(""))
41+
new URI(repository.getOrElse(MavenIdentifier.DefaultRepository))
4242
.resolve(encode(groupId).replace('.', '/') + "/")
4343
.resolve(encode(artifactId) + "/")
4444
.resolve(encode(version.getOrElse("")) + "/")
@@ -48,16 +48,15 @@ case class MavenIdentifier(val repository: Option[String], val groupId: String,
4848
}
4949

5050
object MavenIdentifier {
51+
private val DefaultRepository = "http://repo1.maven.org/maven2/"
52+
5153
private implicit def wrapOption[A](value : A) : Option[A] = Some(value)
54+
5255
def apply(s: String): Option[MavenIdentifier] = {
5356
val splitString: Array[String] = s.split(':')
5457
if (splitString.length < 2 || splitString.length > 3) return None
55-
//if (splitString(0).startsWith("http")) {
56-
// if (splitString.length < 3) return None
57-
// MavenIdentifier(splitString(0), splitString(1), splitString(2), if (splitString.length < 4) None else splitString(3))
58-
//} else {
59-
//if (splitString.length > 3) return None
60-
MavenIdentifier(None, splitString(0), splitString(1), if (splitString.length < 3) None else splitString(2))
61-
//}
58+
59+
MavenIdentifier(None, splitString(0), splitString(1), if (splitString.length < 3) None else splitString(2))
60+
6261
}
6362
}

src/main/scala/de/upb/cs/swt/delphi/webapi/RetrieveQuery.scala

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,65 +16,40 @@
1616

1717
package de.upb.cs.swt.delphi.webapi
1818

19-
import com.sksamuel.elastic4s.http.ElasticDsl.{termQuery, _}
20-
import com.sksamuel.elastic4s.http.search.SearchResponse
19+
import com.sksamuel.elastic4s.http.ElasticDsl._
20+
import com.sksamuel.elastic4s.http.get.GetResponse
2121
import com.sksamuel.elastic4s.http.{ElasticClient, RequestSuccess, Response}
22-
import com.sksamuel.elastic4s.searches.queries.Query
23-
import com.sksamuel.elastic4s.searches.queries.term.TermQuery
24-
import de.upb.cs.swt.delphi.webapi.artifacts.ArtifactTransformer
22+
import de.upb.cs.swt.delphi.webapi.artifacts.{Artifact, ArtifactTransformer}
2523

2624
object RetrieveQuery {
27-
def retrieve(identifier: String)(implicit configuration: Configuration) = {
25+
26+
def retrieve(identifier: String)(implicit configuration: Configuration): Option[List[Artifact]] = {
2827
val client = ElasticClient(configuration.elasticsearchClientUri)
2928

3029
val parsedIdentifier = MavenIdentifier(identifier)
3130

32-
3331
parsedIdentifier match {
3432
case None => None
3533
case Some(m) => {
36-
val response: Response[SearchResponse] = client.execute {
37-
searchWithType(configuration.esProjectIndex) query {
38-
bool {
39-
must(
40-
constructIdQuery(m)
41-
)
42-
}
43-
}
34+
35+
val response: Response[GetResponse] = client.execute {
36+
get(configuration.esProjectIndex.index, configuration.esProjectIndex.`type`, m.toUniqueString)
4437
}.await
4538

4639
response match {
47-
case RequestSuccess(_, body, _, results: SearchResponse) => {
48-
results.totalHits match {
49-
case 0L => None
40+
case RequestSuccess(_, body, _, results: GetResponse) => {
41+
results.found match {
42+
case false => None
5043
case _ => {
51-
Some(ArtifactTransformer.transformResults(results.hits))
44+
Some(List(ArtifactTransformer.transformResult(results.id, results.sourceAsMap)))
5245
}
5346
}
5447
}
5548
case _ => None
5649
}
5750
}
5851
}
59-
60-
6152
}
62-
63-
private def constructIdQuery(m: MavenIdentifier): Iterable[Query] = {
64-
val baseQuery: List[TermQuery] = List(
65-
termQuery("identifier.groupId", m.groupId),
66-
termQuery("identifier.artifactId", m.artifactId)
67-
)
68-
m.version.isDefined match {
69-
case true => {
70-
baseQuery.+:(termQuery("identifier.version", m.version.getOrElse("")))
71-
}
72-
case false => {
73-
baseQuery
74-
}
75-
}
76-
}
77-
7853
}
7954

8055

src/main/scala/de/upb/cs/swt/delphi/webapi/artifacts/ArtifactTransformer.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,17 @@ object ArtifactTransformer {
3838
identifier("groupId"), identifier("artifactId"), identifier("version"))
3939
}
4040

41-
private def transformResult(h: SearchHit) = {
42-
val sourceMap = h.sourceAsMap
43-
Artifact(h.id, getMetadata(sourceMap), getHermesResults(sourceMap))
41+
def transformResult(id : String, sourceMap : Map[String, AnyRef]) :Artifact = {
42+
Artifact(id, getMetadata(sourceMap), getHermesResults(sourceMap))
4443
}
4544

46-
def transformResults(hits: SearchHits) = {
45+
46+
private def transformResult(h: SearchHit): Artifact = {
47+
transformResult(h.id, h.sourceAsMap)
48+
}
49+
50+
51+
def transformResults(hits: SearchHits): Array[Artifact] = {
4752
hits.hits.map(h => transformResult(h))
4853
}
4954

0 commit comments

Comments
 (0)