Workplace Search - Java APIs
A JVM client and SDK for Elastic Workplace Search
Building
To build, run:
mvn clean install
As a Dependency
To use the JVM client for Workplace Search, add:
<dependency>
<groupId>com.seanjstory.workplace.search</groupId>
<artifactId>workplace-search-client</artifactId>
<version>7.9.0-SNAPSHOT</version>
</dependency>
To use the JVM SDK for Custom API Sources for Workplace Search, add:
<dependency>
<groupId>com.seanjstory.workplace.search</groupId>
<artifactId>workplace-search-sdk</artifactId>
<version>7.9.0-SNAPSHOT</version>
</dependency>
Usage
Client
The client requires an accessToken, and most of the APIs also require a contentSourceKey. Both can be found in the Workplace Search UI after creating a Custom API source. Using the client is as easy as:
import com.seanjstory.workplace.search.client.Client;
//...
String accessToken = "3a423c597442eddb09baad64793ff342fc0aa6da357f5227888d44b3386cf722";
String contentSourceKey = "5e8f5266f74c321dae6e5548";
Client client = new Client(accessToken);
Map<String,String> document = Collections.singletonMap("id", "someId")
List<Map<String, Object>> documents = Collections.singletonList(document)
client.indexDocuments(contentSourceKey, documents)
SDK
For experiential learners, the best way to see the SDK in action might be to look at a simple example.
The Workplace Search Java SDK is intended to help develop Custom API sources. To leverage it, it's worth understanding what is in the module.
.
└── com.seanjstory.workplace.search.sdk
├── api
│ ├── DocumentBase.java # Abstract Class
│ ├── Source.java # Interface
│ ├── SourcesUtils.java # Concrete Utility
│ ├── YamlConfig.java # Concrete Utility
│ └── Yielder.java # Abstract Utility
└── run
└── Sync.java # Concrete Utility
- The
DocumentBaseclass provides the base API for "documents". Every document should have an id and a body. Most documents will have other metadata as well (URL, Title, Last Modified Date, etc). - The
Sourceinterface is merely defined as a class that provides anIterator<? extends DocumentBase>. - The
SourcesUtilsclass is a lightweight approach to dependency injection. It will look for asourcesresource in your classloader's resources, expecting to find a line-separated list ofSourceimplementation cannonical names. TheSourceUtilscan then be used to instantiate a source, given its name, and optionally a list of String arguments. - The
YamlConfigclass is a lightweight adapter for a YAML config file that can be used store your Access Token, Content Source Key, and any arguments necessary to instantiate your Sources (in conjunction withSourcesUtils). - The
Yielderis the recommended mechanism through which yourSourceimplementations might provide anIterator. Many sources may contain so many documents (and such large documents), that it becomes difficult to handle them all in a single collection. This lazy evaluator is easy to leverage, and can help you to keep only a small number of documents in memory at a given time. - Finally, the
Syncclass provides amainfunction that drive your source. It expects two arguments, a path to a YAML configuration file (seeYamlConfig) and a source name (seeSourcesUtils). From these, it will instantiate aSource, connect to Workplace Search with a JavaClient, and iterate over your documents, indexing them in batches of 100.