问题
I was experimenting with Neo4j embedded-db in the past few days for a DEMO and was thoroughly impressed with the Native Java API's for indexing, lucene queries and even managed to do fuzzy search. I then decided to take this POC to production with Spring Data Neo4j 4.0 but ran into issues with Cypher queries and fuzzy search.
My domain class "Team" looks like this:
@NodeEntity public class Team {
@GraphId Long nodeId;
/** The team name. */
@Indexed(indexType = IndexType.FULLTEXT,indexName = "teamName")
private String teamName;
public Team(){};
public Team(String name){
this.teamName = name;
}
public void setTeamName(String name){
this.teamName = name;
}
public String getTeamName(){
return this.teamName;
}
}
I am populating my database as follows:
Team lakers = new Team("Los Angeles Lakers");
Team clippers = new Team("Los Angeles Clippers of Anaheim");
Team warriors = new Team("Golden State Warriors");
Team slappers = new Team("Los Angeles Slappers of Anaheim");
Team slippers = new Team("Los Angeles Slippers of Anaheim");
Transaction tx = graphDatabase.beginTx();
try{
teamRepository.save(lakers);
teamRepository.save(clippers);
teamRepository.save(warriors);
teamRepository.save(slappers);
teamRepository.save(slippers);
}
My TeamRepository interface looks like this:
public interface TeamRepository extends CrudRepository<Team, String>
{
@Query("MATCH (team:Team) WHERE team.teamName=~{0} RETURN team")
List<Team> findByTeamName(String query);
}
My query looks like:
List<Team> teams = teamRepository.findByTeamName("The Los Angeles Will be Playing in a state of Golden");
The above CYPHER query DOES NOT return anything.
I'd like to be able to do a Native Java API type query in Spring like the one below and get the following result.(teamIndex was a full text search index I had created on team names)
IndexHits<Node> found = teamIndex.query("Team-Names",queryString+"~0.5");
.
Native JAVA API found:
- Los Angeles Lakers
- Los Angeles Clippers of Anaheim
- Golden State Warriors
- Los Angeles Slappers of Anaheim
- Los Angeles Slippers of Anaheim
回答1:
What you are looking for is in SDN3
SDN4 doesn't support @Indexed.
There is an IndexRepository with some additional query methods but you can also use cypher like this:
public interface TeamRepository extends GraphRepository<Team>
{
@Query("start team=node:teamName({0}) RETURN team")
List<Team> findByTeamName(String query);
}
Which will send the index-query to Lucene.
来源:https://stackoverflow.com/questions/31350710/neo4j-native-java-apior-equivalent-cypher-query-in-spring-data-neo4j