mirror of
https://github.com/simon987/music-graph-api.git
synced 2025-04-16 08:26:43 +00:00
Search tag
This commit is contained in:
parent
093e3fcd6c
commit
fa1e1a5515
@ -30,6 +30,7 @@ public class Main {
|
||||
rc.registerClasses(Index.class);
|
||||
rc.registerClasses(ArtistController.class);
|
||||
rc.registerClasses(CoverController.class);
|
||||
rc.registerClasses(TagController.class);
|
||||
rc.registerClasses(ReleaseController.class);
|
||||
rc.registerClasses(ChartController.class);
|
||||
rc.registerClasses(JacksonFeature.class);
|
||||
|
@ -0,0 +1,7 @@
|
||||
package net.simon987.musicgraph.entities;
|
||||
|
||||
|
||||
public class TagSearchResult extends SearchResult {
|
||||
|
||||
public Tag tag;
|
||||
}
|
@ -142,7 +142,7 @@ public class MusicDatabase extends AbstractBinder {
|
||||
// Only match artists with > 0 releases
|
||||
"MATCH (b)-[:CREDITED_FOR]->(:Release)\n" +
|
||||
"WHERE r.weight > 0.25\n" +
|
||||
"RETURN a as artist, a {rels: collect(DISTINCT r), nodes: collect(DISTINCT b)} as rank1\n" +
|
||||
"RETURN a as artist, {rels: collect(DISTINCT r), nodes: collect(DISTINCT b)} as rank1\n" +
|
||||
"LIMIT 1",
|
||||
params);
|
||||
|
||||
@ -156,14 +156,50 @@ public class MusicDatabase extends AbstractBinder {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public TagSearchResult getRelatedByTag(long id) {
|
||||
|
||||
try (Session session = driver.session()) {
|
||||
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("tag_id", id);
|
||||
|
||||
StatementResult result = query(session,
|
||||
"MATCH (t:Tag)-[r:IS_TAGGED]-(a:Artist)\n" +
|
||||
"WHERE ID(t) = $tag_id\n" +
|
||||
"RETURN t, {rels: collect(DISTINCT r), nodes: collect(DISTINCT a)} as rank1\n" +
|
||||
"LIMIT 1",
|
||||
params);
|
||||
|
||||
TagSearchResult out = new TagSearchResult();
|
||||
|
||||
if (result.hasNext()) {
|
||||
Record row = result.next();
|
||||
out.tag = makeTag(row.get(0).asNode());
|
||||
artistsFromRelMap(out, row.get(1).asMap());
|
||||
}
|
||||
|
||||
return out;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void parseRelatedResult(StatementResult result, SearchResult out) {
|
||||
long start = System.nanoTime();
|
||||
while (result.hasNext()) {
|
||||
if (result.hasNext()) {
|
||||
Record row = result.next();
|
||||
out.artists.add(makeArtist(row.get(0).asNode()));
|
||||
|
||||
var rank1 = row.get(1).asMap();
|
||||
artistsFromRelMap(out, row.get(1).asMap());
|
||||
}
|
||||
long end = System.nanoTime();
|
||||
long took = (end - start) / 1000000;
|
||||
logger.info(String.format("Fetched search result (Took %dms)", took));
|
||||
}
|
||||
|
||||
private void artistsFromRelMap(SearchResult out, Map<String, Object> rank1) {
|
||||
out.relations.addAll(((List<Relationship>) rank1.get("rels"))
|
||||
.stream()
|
||||
.map(MusicDatabase::makeRelation)
|
||||
@ -174,11 +210,6 @@ public class MusicDatabase extends AbstractBinder {
|
||||
.map(MusicDatabase::makeArtist)
|
||||
.collect(Collectors.toList()
|
||||
));
|
||||
|
||||
}
|
||||
long end = System.nanoTime();
|
||||
long took = (end - start) / 1000000;
|
||||
logger.info(String.format("Fetched search result (Took %dms)", took));
|
||||
}
|
||||
|
||||
private static Artist makeArtist(Node node) {
|
||||
@ -194,6 +225,14 @@ public class MusicDatabase extends AbstractBinder {
|
||||
return artist;
|
||||
}
|
||||
|
||||
private static Tag makeTag(Node node) {
|
||||
return new Tag(
|
||||
node.id(),
|
||||
node.get("name").asString(),
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
private static Relation makeRelation(Relationship rel) {
|
||||
Relation relation = new Relation();
|
||||
|
||||
|
@ -0,0 +1,34 @@
|
||||
package net.simon987.musicgraph.webapi;
|
||||
|
||||
import net.simon987.musicgraph.entities.SearchResult;
|
||||
import net.simon987.musicgraph.io.MusicDatabase;
|
||||
import net.simon987.musicgraph.logging.LogManager;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@Path("/tag")
|
||||
public class TagController {
|
||||
|
||||
private static Logger logger = LogManager.getLogger();
|
||||
|
||||
@Inject
|
||||
private MusicDatabase db;
|
||||
|
||||
public TagController() {
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("related/{id}")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public SearchResult getRelated(@PathParam("id") long id) {
|
||||
|
||||
logger.info(String.format("Related tag for %d", id));
|
||||
return db.getRelatedByTag(id);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user