Friday 25 February 2022

Spring Data: ArangoDB: Create geo indexes

CollectionOperations interface provides 'ensureGeoIndex' method to create geo index.

 

Signature

IndexEntity ensureGeoIndex(Iterable<String> fields, GeoIndexOptions options) throws DataAccessException;

 

‘GeoIndexOptions’ is used to provide additional options while creating the index. Below table summarizes the possible options associated with GeoIndexOptions.

 

Option

Datatype

Description

inBackground

Boolean

create the the index in the background

name

String

name of the index

fields

Iterable<String>

list of attribute paths like a, a.b, a.b.c etc.,

type

IndexType

IndexType is an enum and can be one of

a.   primary,

b.   hash,

c.    skiplist,

d.   persistent,

e.   geo,

f.     geo1,

g.   geo2,

h.   fulltext,

i.     edge,

j.     ttl

geoJson

Boolean

If a geo-spatial index on a location is constructed and geoJson is true, then the order within the array is longitude followed by latitude. This corresponds to the format described in

 

Example
GeoIndexOptions gersistentIndexOptions = new GeoIndexOptions();
gersistentIndexOptions.name("locationIndex");

empsCollection.ensureGeoIndex(Arrays.asList("locationLongitude", "locationLatitude"),gersistentIndexOptions);

 

Find the below working application.

 

Step 1: Create new maven project ‘collection-operations-create-geo-index’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>collection-operations-create-geo-index</artifactId>
	<version>1</version>

	<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.5</version>
	</parent>


	<dependencies>
		<dependency>
			<groupId>com.arangodb</groupId>
			<artifactId>arangodb-spring-boot-starter</artifactId>
			<version>2.3.3.RELEASE</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Step 3: Define entity class.

 

Employee.java

package com.sample.app.entity;

import com.arangodb.springframework.annotation.ArangoId;
import com.arangodb.springframework.annotation.Document;
import org.springframework.data.annotation.Id;

@Document("employees")
public class Employee {

	@Id // db document field: _key
	private String key;

	@ArangoId // db document field: _id
	private String arangoId;

	private Integer id;
	private String name;
	private double locationLongitude;
	private double locationLatitude;

	public Employee(Integer id, String name, double locationLongitude, double locationLatitude) {
		super();

		this.id = id;
		this.name = name;
		this.locationLongitude = locationLongitude;
		this.locationLatitude = locationLatitude;
	}

	public Employee() {

	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getArangoId() {
		return arangoId;
	}

	public void setArangoId(String arangoId) {
		this.arangoId = arangoId;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getLocationLongitude() {
		return locationLongitude;
	}

	public void setLocationLongitude(double locationLongitude) {
		this.locationLongitude = locationLongitude;
	}

	public double getLocationLatitude() {
		return locationLatitude;
	}

	public void setLocationLatitude(double locationLatitude) {
		this.locationLatitude = locationLatitude;
	}

	@Override
	public String toString() {
		return "Employee [key=" + key + ", arangoId=" + arangoId + ", id=" + id + ", name=" + name
				+ ", locationLongitude=" + locationLongitude + ", locationLatitude=" + locationLatitude + "]";
	}

}

Step 4: Define arango db configuration class.

 

ArangoConfig.java

package com.sample.app.config;

import org.springframework.context.annotation.Configuration;

import com.arangodb.ArangoDB;
import com.arangodb.springframework.annotation.EnableArangoRepositories;
import com.arangodb.springframework.config.ArangoConfiguration;

@Configuration
@EnableArangoRepositories(basePackages = { "com.sample.app" })
public class ArangoConfig implements ArangoConfiguration {

	@Override
	public ArangoDB.Builder arango() {
		return new ArangoDB.Builder().host("localhost", 8529).user("root").password("tiger");
	}

	@Override
	public String database() {
		return "abc_org";
	}
}

Step 5: Define main application class.

 

App.java

package com.sample.app;

import java.util.Arrays;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.arangodb.entity.IndexEntity;
import com.arangodb.model.GeoIndexOptions;
import com.arangodb.springframework.core.ArangoOperations;
import com.arangodb.springframework.core.CollectionOperations;
import com.sample.app.entity.Employee;

@SpringBootApplication
public class App {

	@Autowired
	private ArangoOperations arangoTemplate;

	public static void main(String args[]) {
		SpringApplication.run(App.class, args);
	}

	@Bean
	public CommandLineRunner demo() {

		return (args) -> {
			Employee emp1 = new Employee(1, "Krishna", 29.9792, 31.1342);

			arangoTemplate.insert(emp1);

			Iterable<Employee> empsIterable = arangoTemplate.findAll(Employee.class);

			System.out.println("Documents in employees collection");
			for (Employee emp : empsIterable) {
				System.out.println(emp);
			}

			CollectionOperations empsCollection = arangoTemplate.collection(Employee.class);

			GeoIndexOptions gersistentIndexOptions = new GeoIndexOptions();
			gersistentIndexOptions.name("locationIndex");

			empsCollection.ensureGeoIndex(Arrays.asList("locationLongitude", "locationLatitude"),
					gersistentIndexOptions);

			Collection<IndexEntity> indexesCollection = empsCollection.getIndexes();

			System.out.println("\nTotal indexes : " + indexesCollection.size());

			for (IndexEntity indexEntity : indexesCollection) {
				System.out.println("\nindex details");
				System.out.println("name : " + indexEntity.getName());
				System.out.println("Index created on fields : " + indexEntity.getFields());
			}

			System.out.println("\nDropping the collection");

			empsCollection.drop();

		};
	}
}

Total project structure looks like below.





Run App.java, you will see below messages in console.

 

Documents in employees collection
Employee [key=57096, arangoId=employees/57096, id=1, name=Krishna, locationLongitude=29.9792, locationLatitude=31.1342]

Total indexes : 2

index details
name : primary
Index created on fields : [_key]

index details
name : locationIndex
Index created on fields : [locationLongitude, locationLatitude]

Dropping the collection

 

You can download complete working application from below link.

https://github.com/harikrishna553/springboot/tree/master/arangodb/collection-operations-create-geo-index

 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment