mirror of
https://github.com/simon987/music-graph-api.git
synced 2025-04-19 09:56:43 +00:00
Autocomplete endpoint
This commit is contained in:
parent
c048c351cf
commit
0e83b37672
@ -3,6 +3,8 @@ package net.simon987.musicgraph.io;
|
|||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import net.simon987.musicgraph.entities.*;
|
import net.simon987.musicgraph.entities.*;
|
||||||
import net.simon987.musicgraph.logging.LogManager;
|
import net.simon987.musicgraph.logging.LogManager;
|
||||||
|
import net.simon987.musicgraph.webapi.ArtistOverview;
|
||||||
|
import net.simon987.musicgraph.webapi.AutoCompleteData;
|
||||||
import org.glassfish.jersey.internal.inject.AbstractBinder;
|
import org.glassfish.jersey.internal.inject.AbstractBinder;
|
||||||
import org.neo4j.driver.v1.*;
|
import org.neo4j.driver.v1.*;
|
||||||
import org.neo4j.driver.v1.types.Node;
|
import org.neo4j.driver.v1.types.Node;
|
||||||
@ -152,35 +154,6 @@ public class MusicDatabase extends AbstractBinder {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public SearchResult getRelatedByName(String name) {
|
|
||||||
|
|
||||||
try (Session session = driver.session()) {
|
|
||||||
|
|
||||||
Map<String, Object> params = new HashMap<>();
|
|
||||||
params.put("name", name);
|
|
||||||
|
|
||||||
StatementResult result = query(session,
|
|
||||||
"MATCH (a:Artist)-[r:IS_RELATED_TO]-(b)\n" +
|
|
||||||
"WHERE a.name = $name\n" +
|
|
||||||
// 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" +
|
|
||||||
"LIMIT 1",
|
|
||||||
params);
|
|
||||||
|
|
||||||
SearchResult out = new SearchResult();
|
|
||||||
|
|
||||||
parseRelatedResult(result, out);
|
|
||||||
|
|
||||||
return out;
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void parseRelatedResult(StatementResult result, SearchResult out) {
|
private void parseRelatedResult(StatementResult result, SearchResult out) {
|
||||||
long start = System.nanoTime();
|
long start = System.nanoTime();
|
||||||
while (result.hasNext()) {
|
while (result.hasNext()) {
|
||||||
@ -277,4 +250,36 @@ public class MusicDatabase extends AbstractBinder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AutoCompleteData autoComplete(String prefix) {
|
||||||
|
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
params.put("prefix", prefix);
|
||||||
|
|
||||||
|
try (Session session = driver.session()) {
|
||||||
|
|
||||||
|
StatementResult result = query(session,
|
||||||
|
"MATCH (a:Artist) " +
|
||||||
|
"WHERE a.sortname STARTS WITH $prefix " +
|
||||||
|
"RETURN a ORDER BY a.listeners DESC " +
|
||||||
|
"LIMIT 30",
|
||||||
|
params);
|
||||||
|
|
||||||
|
AutoCompleteData data = new AutoCompleteData();
|
||||||
|
|
||||||
|
while (result.hasNext()) {
|
||||||
|
Map<String, Object> map = result.next().get("a").asMap();
|
||||||
|
|
||||||
|
ArtistOverview a = new ArtistOverview();
|
||||||
|
a.name = (String) map.get("name");
|
||||||
|
a.comment = (String) map.get("comment");
|
||||||
|
a.year = (long) map.get("year");
|
||||||
|
a.mbid = (String) map.get("id");
|
||||||
|
|
||||||
|
data.artists.add(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import net.simon987.musicgraph.entities.ArtistDetails;
|
|||||||
import net.simon987.musicgraph.io.MusicDatabase;
|
import net.simon987.musicgraph.io.MusicDatabase;
|
||||||
import net.simon987.musicgraph.entities.SearchResult;
|
import net.simon987.musicgraph.entities.SearchResult;
|
||||||
import net.simon987.musicgraph.logging.LogManager;
|
import net.simon987.musicgraph.logging.LogManager;
|
||||||
|
import org.checkerframework.checker.units.qual.A;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.ws.rs.*;
|
import javax.ws.rs.*;
|
||||||
@ -39,16 +40,6 @@ public class ArtistController {
|
|||||||
return db.getArtistMembers(mbid);
|
return db.getArtistMembers(mbid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
|
||||||
@Path("related_by_name/{name}")
|
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
|
||||||
public SearchResult getRelatedByName(@PathParam("name") String name) {
|
|
||||||
|
|
||||||
name = name.replace('+', ' ');
|
|
||||||
logger.info(String.format("Related for %s", name));
|
|
||||||
return db.getRelatedByName(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("details/{mbid}")
|
@Path("details/{mbid}")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
@ -57,4 +48,15 @@ public class ArtistController {
|
|||||||
logger.info(String.format("Details for %s", mbid));
|
logger.info(String.format("Details for %s", mbid));
|
||||||
return db.getArtistDetails(mbid);
|
return db.getArtistDetails(mbid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Path("autocomplete/{prefix}")
|
||||||
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
public AutoCompleteData autoComplete(@PathParam("prefix") String prefix) {
|
||||||
|
|
||||||
|
prefix = prefix.replace('+', ' ');
|
||||||
|
logger.info(String.format("Autocomplete for '%s'", prefix));
|
||||||
|
|
||||||
|
return db.autoComplete(prefix);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,8 @@
|
|||||||
|
package net.simon987.musicgraph.webapi;
|
||||||
|
|
||||||
|
public class ArtistOverview {
|
||||||
|
public String name;
|
||||||
|
public String mbid;
|
||||||
|
public String comment;
|
||||||
|
public long year;
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package net.simon987.musicgraph.webapi;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class AutoCompleteData {
|
||||||
|
public List<ArtistOverview> artists;
|
||||||
|
|
||||||
|
public AutoCompleteData() {
|
||||||
|
artists = new ArrayList<>(30);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user