`

Neo4j: Index and Lables with SDN

 
阅读更多

In model, in order to index some property, you should anotate it with @Index.    SDN now adopt schema-based index policy, which means the Entity class name will be used as lable.

 

@NodeEntity
@XmlRootElement
@JsonAutoDetect
@JsonIgnoreProperties(ignoreUnknown = true)
public class User {
    @Indexed
    private String name;

    @Indexed(unique = true)
    private String userId;

    @Indexed
    private String phoneNumber;
....

 

 In the above codes, SDN will produce tow labels for this modle  User, _User.

If we want multiply lables for a modle, we could do this by following

    @Labels
    private Set<String> lables;

    public Set<String> getLables() {
	return lables;
    }

    public void setLables(Set<String> lables) {
	this.lables = lables;
    }

    public void addLable(String lable) {
	if (this.lables == null)
	    this.lables = new HashSet<String>();

	lables.add(lable);
    }

    public void delLable(String lable){
	 if(this.lables!=null & this.lables.contains(lable))
	     lables.remove(lable);
    }

 

We can add lables for a node in extention

@Path("/addLable/active/{userName}")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response activeUser(final @PathParam("userName") String userName) throws JSONException {
	try (Transaction tx = template.getGraphDatabaseService().beginTx()) {
	    User user = userRepo.getUserByName(userName);
	    if(user != null){
		user.addLable(FIFOLabels.ActiveUser.name());
		userRepo.saveOnly(user);
	    }else{
		return Response.status(Status.NOT_FOUND).entity("User doesn't exist").build();
	    }
	    
	    tx.success();
	}
	return Response.status(Status.OK).entity(OK_MSG).build();
    }

----------------------------------------------------------------------

But a failure comes in other modle using  @Lables,

java.lang.NullPointerException
        at org.springframework.data.neo4j.fieldaccess.
LabelFieldAccessorFactory$LabelFieldAccessor.setValue
(LabelFieldAccessorFactory.java:73)
        at org.springframework.data.neo4j.fieldaccess.
DefaultEntityState.setValue(DefaultEntityState.java:113)

 

Model Cods

  @Labels
    private Set<String> lables;

    public Set<String> getLables() {
	return lables;
    }

    public void setLables(Set<String> lables) {
	this.lables = lables;
    }

    public void addLable(String lable) {
	if (this.lables == null)
	    this.lables = new HashSet<String>();

	lables.add(lable);
    }

    public void delLable(String lable) {
	if (this.lables != null & this.lables.contains(lable))
	    lables.remove(lable);
    }

    @Indexed(indexType = IndexType.POINT, indexName = "TaskLocation")
    String wkt;

    public void setLocation(float lon, float lat) {
	this.wkt = String.format("POINT( %.2f %.2f )", lon, lat);
    }

    public Task() {

    }

    public Task(String taskId, String title, String description) {
	this.taskId = taskId;
	this.title = title;
	this.description = description; 
    }

 

 The reasons I guess is that I used legency Index type .

 

@Indexed(indexType = IndexType.POINT, indexName = "TaskLocation")
    String wkt;

But,when I save a Task node by set it's lable, there is no error.

 

 @Path("/addTask")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response addTask(Task task) {
	logger.info("############# add new task ########################");
	logger.info("task:" + task.toString());
        task.addLable("NewTask");
	taskRepo.saveOnly(task);
	return Response.status(Status.OK).entity(OK_MSG).build();
    }

 When comment this add lable command, error comes

//task.addLable("NewTask");

 Why? 

 

Labels Set not initialized when using @Labels annotation

 https://jira.spring.io/browse/DATAGRAPH-541

 

It turns out that this is a bug in SDN.

Good news is that I solved this problem by manually initilize  @Labels filed in constuctor

 public Task() {
	this.lables = new HashSet<String>();
	this.lables.add("Task");
	this.lables.add("_Task");
    }

 

 

 

 

 

 

 

 

 

References

http://graphaware.com/neo4j/2015/01/16/neo4j-graph-model-design-labels-versus-indexed-properties.html

http://stackoverflow.com/questions/22075802/spring-data-neo4j-3-0-0-labels-support

http://stackoverflow.com/questions/28272953/how-to-use-labels-spring-data-neo4j

http://stackoverflow.com/questions/28475023/how-to-add-labels-to-nodes-using-spring-data-neo4j

https://github.com/spring-projects/spring-data-neo4j/blob/15cc8418ed4d429a952b86f788717add8a93f76c/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/Person.java

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics