|
27 | 27 | * Thread-safe clients
|
28 | 28 | * Typed requests and responses
|
29 | 29 |
|
30 |
| -**Migration note from v2.x to v3.x** |
| 30 | +**Migration note from v3.x to v4.x** |
31 | 31 | >
|
32 |
| -> In June 2019, we released v3 of our Java client. If you are using version 2.x of the client, read the [migration guide to version 3.x](https://www.algolia.com/doc/api-client/getting-started/upgrade-guides/java/). |
33 |
| -Version 2.x will **no longer** be under active development. |
| 32 | +> In July 2024, we released the v4 of our Java client. If you are using version 2.x or 3.x of the client, read the [migration guide to version 4.x](https://www.algolia.com/doc/api-client/getting-started/upgrade-guides/java/). |
| 33 | +Version 3.x will **no longer** be under active development. |
34 | 34 |
|
35 | 35 | ## 💡 Getting Started
|
36 | 36 |
|
37 |
| -### Install |
38 |
| - |
39 |
| -* **Maven**: add the following to your `pom.xml` file: |
40 |
| - |
41 |
| - ```xml |
42 |
| - <dependency> |
43 |
| - <groupId>com.algolia</groupId> |
44 |
| - <artifactId>algoliasearch</artifactId> |
45 |
| - <version>LATEST</version> |
46 |
| - </dependency> |
47 |
| - ``` |
48 |
| -* **Gradle**: add the following to your `build.gradle` file: |
49 |
| - ```groovy |
50 |
| - implementation "com.algolia:algoliasearch:$version" |
51 |
| - ``` |
52 |
| - |
53 |
| -### Initialize the client |
54 |
| - |
55 |
| -To start, you need to initialize the client. To do this, you need your **Application ID** and **API Key**. |
56 |
| -You can find both on [your Algolia account](https://www.algolia.com/api-keys). |
57 |
| - |
58 |
| -```java |
59 |
| -SearchClient client = new SearchClient("MY_APPLICATION_ID", "MY_API_KEY"); |
60 |
| -``` |
61 |
| - |
62 |
| -If you need to customize the configuration of the client, use |
63 |
| -`ClientOptions` when instantiating the Algolia `SearchClient` instance. |
| 37 | +To get started, add the algoliasearch-client-java dependency to your project, either with [Maven](Maven): |
64 | 38 |
|
65 | 39 | ```java
|
66 |
| -ClientOptions options = ClientOptions.builder().setLogLevel(LogLevel.BODY).build(); |
67 |
| -SearchClient client = new SearchClient("MY_APPLICATION_ID", "MY_API_KEY", options); |
| 40 | +<dependency> |
| 41 | + <groupId>com.algolia</groupId> |
| 42 | + <artifactId>algoliasearch</artifactId> |
| 43 | + <version>4.0.0-beta.36</version> |
| 44 | +</dependency> |
68 | 45 | ```
|
69 | 46 |
|
70 |
| -### Push data |
71 |
| - |
72 |
| -Without any prior configuration, you can start indexing contacts in the `contacts` index using the following code: |
| 47 | +or [Gradle](https://gradle.org/): |
73 | 48 |
|
74 | 49 | ```java
|
75 |
| -class Contact { |
76 |
| - private String firstname; |
77 |
| - private String lastname; |
78 |
| - private int followers; |
79 |
| - private String company; |
80 |
| - private String objectID; |
81 |
| - // Getters/setters ommitted |
| 50 | +dependencies { |
| 51 | + implementation 'com.algolia:algoliasearch:4.0.0-beta.36' |
82 | 52 | }
|
83 |
| - |
84 |
| -Contact contact = new Contact() |
85 |
| - .setObjectID("one") |
86 |
| - .setFirstname("Jimmie") |
87 |
| - .setLastname("Barninger") |
88 |
| - .setFollowers(93) |
89 |
| - .setCompany("California Paint"); |
90 |
| - |
91 |
| -List<Contact> records = Arrays.asList(contact); |
92 |
| -List<BatchRequest> batch = records.stream() |
93 |
| - .map(entry -> new BatchRequest().setAction(Action.ADD_OBJECT).setBody(entry)) |
94 |
| - .toList(); |
95 |
| -BatchResponse response = client.batch("contacts", new BatchWriteParams().setRequests(batch)); |
96 | 53 | ```
|
97 | 54 |
|
98 |
| -### Search |
99 |
| - |
100 |
| -You can now search for contacts by `firstname`, `lastname`, `company`, etc. (even with typos): |
| 55 | +You can now import the Algolia API client in your project and play with it. |
101 | 56 |
|
102 | 57 | ```java
|
103 |
| -SearchParams params = SearchParams.of(new SearchParamsObject().setQuery("jimmie")); |
| 58 | +import com.algolia.api.SearchClient; |
| 59 | +import com.algolia.model.search.*; |
104 | 60 |
|
105 |
| -// Synchronous search |
106 |
| -client.searchSingleIndex("contacts", params, Contact.class); |
| 61 | +SearchClient client = new SearchClient("YOUR_APP_ID", "YOUR_API_KEY"); |
107 | 62 |
|
108 |
| -// Asynchronous search |
109 |
| -client.searchSingleIndexAsync("contacts", params, Contact.class); |
110 |
| -``` |
| 63 | +// Add a new record to your Algolia index |
| 64 | +client.saveObject("<YOUR_INDEX_NAME>", Map.of("objectID", "id", "test", "val")); |
111 | 65 |
|
112 |
| -For full documentation, visit the [Algolia Java API Client's documentation](https://www.algolia.com/doc/api-client/getting-started/install/java/). |
| 66 | +// Poll the task status to know when it has been indexed |
| 67 | +client.waitForTask("<YOUR_INDEX_NAME>", response.getTaskID()); |
113 | 68 |
|
114 |
| -## 📝 Examples |
| 69 | +// Fetch search results, with typo tolerance |
| 70 | +client.search( |
| 71 | + new SearchMethodParams() |
| 72 | + .setRequests(List.of(new SearchForHits().setIndexName("<YOUR_INDEX_NAME>").setQuery("<YOUR_QUERY>").setHitsPerPage(50))), |
| 73 | + Hit.class |
| 74 | +); |
| 75 | +``` |
115 | 76 |
|
116 |
| -You can find code samples in the [Algolia's API Clients playground](https://github.com/algolia/api-clients-playground/tree/master/java/src/main/java). |
| 77 | +For full documentation, visit the **[Algolia Java API Client](https://www.algolia.com/doc/api-client/getting-started/install/java/)**. |
117 | 78 |
|
118 | 79 | ## ❓ Troubleshooting
|
119 | 80 |
|
120 |
| -Encountering an issue? Before reaching out to support, we recommend heading to our [FAQ](https://www.algolia.com/doc/api-client/troubleshooting/faq/java/) where you will find answers for the most common issues and gotchas with the client. |
| 81 | +Encountering an issue? Before reaching out to support, we recommend heading to our [FAQ](https://www.algolia.com/doc/api-client/troubleshooting/faq/java/) where you will find answers for the most common issues and gotchas with the client. You can also open [a GitHub issue](https://github.com/algolia/api-clients-automation/issues/new?assignees=&labels=&projects=&template=Bug_report.md) |
121 | 82 |
|
122 |
| -## Use the Dockerfile |
| 83 | +## Contributing |
123 | 84 |
|
124 |
| -If you want to contribute to this project without installing all its dependencies, you can use our Docker image. Please check our [dedicated guide](DOCKER_README.MD) to learn more. |
| 85 | +This repository hosts the code of the generated Algolia API client for Java, if you'd like to contribute, head over to the [main repository](https://github.com/algolia/api-clients-automation). You can also find contributing guides on [our documentation website](https://api-clients-automation.netlify.app/docs/contributing/introduction). |
125 | 86 |
|
126 | 87 | ## 📄 License
|
127 |
| -Algolia Java API Client is an open-sourced software licensed under the [MIT license](LICENSE). |
| 88 | + |
| 89 | +The Algolia Java API Client is an open-sourced software licensed under the [MIT license](LICENSE). |
0 commit comments