Full text index is used while executing full text queries.
CollectionOperations provide 'ensureFulltextIndex' method to create a full text index.
Signature
IndexEntity ensureFulltextIndex(Iterable fields, FulltextIndexOptions options) throws DataAccessException;
Creates a fulltext index for the collection, if it does not already exist.
'FulltextIndexOptions' provides additional options which are used at the time of index creation. Below table summarizes possible options associated with FulltextIndexOptions.
Option | Data type | Description |
inBackground | Boolean | create the the index in the background |
name | String | name of the index |
fields | Iterable | 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 |
minLength | Integer | Minimum character length of words to index. Will default to a server-defined value if unspecified. It is thus recommended to set this value explicitly when creating the index. |
Example
CollectionOperations empsCollection = arangoTemplate.collection(Employee.class);
FulltextIndexOptions fulltextIndexOptions = new FulltextIndexOptions();
fulltextIndexOptions.name("aboutMeIndex");
fulltextIndexOptions.minLength(10);
empsCollection.ensureFulltextIndex(Arrays.asList("aboutMe"), fulltextIndexOptions);
Find the below working application.
Step 1: Create new maven project ‘collection-operations-create-fulltext-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.0modelVersion>
groupId>com.sample.appgroupId>
artifactId>collection-operations-create-fulltext-indexartifactId>
version>1version>
parent>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-parentartifactId>
version>2.4.5version>
parent>
dependencies>
dependency>
groupId>com.arangodbgroupId>
artifactId>arangodb-spring-boot-starterartifactId>
version>2.3.3.RELEASEversion>
dependency>
dependencies>
build>
plugins>
plugin>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
Step 3: Define Employee entity.
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 firstName;
private String lastName;
private Integer age;
private String aboutMe;
public Employee(Integer id, String firstName, String lastName, Integer age, String aboutMe) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.aboutMe = aboutMe;
}
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 getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAboutMe() {
return aboutMe;
}
public void setAboutMe(String aboutMe) {
this.aboutMe = aboutMe;
}
@Override
public String toString() {
return "Employee [key=" + key + ", arangoId=" + arangoId + ", id=" + id + ", firstName=" + firstName
+ ", lastName=" + lastName + ", age=" + age + ", aboutMe=" + aboutMe + "]";
}
}
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.FulltextIndexOptions;
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", "Gurram", 33, "Blogger, cricketer");
Employee emp2 = new Employee(2, "Lahari", "G", 20, "I love trekking, singing and classical dancer");
Employee emp3 = new Employee(3, "Thulasi", "G", 19, "Ethical hacker");
arangoTemplate.insert(Arrays.asList(emp1, emp2, emp3), Employee.class);
Iterable 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);
FulltextIndexOptions fulltextIndexOptions = new FulltextIndexOptions();
fulltextIndexOptions.name("aboutMeIndex");
fulltextIndexOptions.minLength(10);
empsCollection.ensureFulltextIndex(Arrays.asList("aboutMe"), fulltextIndexOptions);
Collection 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=55413, arangoId=employees/55413, id=1, firstName=Krishna, lastName=Gurram, age=33, aboutMe=Blogger, cricketer]
Employee [key=55414, arangoId=employees/55414, id=2, firstName=Lahari, lastName=G, age=20, aboutMe=I love trekking, singing and classical dancer]
Employee [key=55415, arangoId=employees/55415, id=3, firstName=Thulasi, lastName=G, age=19, aboutMe=Ethical hacker]
Total indexes : 2
index details
name : primary
Index created on fields : [_key]
index details
name : aboutMeIndex
Index created on fields : [aboutMe]
Dropping the collection
You can download complete working application from below link.
https://github.com/harikrishna553/springboot/tree/master/arangodb/collection-operations-create-fulltext-index
This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here