First, enable the Automcomplete Service.
Go to NHN Cloud Console and click Select Services.
Click Autocomplete.
Do as follows to check if service is enabled:
Click Search on the left menu on NHN Cloud Console.
If you can find Autocomplete, the service has been enabled.
Click Create Services.
Enter Service ID on Create Services.
Click Save.
Check the result of service creation. 1. Click Service ID which is created (test).
Do as follows to create and index files.
Create Index Files
[
{
"input": "Nike",
"weight": 3
},
{
"input": "Nike sneakers",
"weight": 2
},
{
"input": "Sneakers",
"weight": 1
}
]
How to indexing
Click Indexing.
Click Select File.
Select a file to indexing.
Press Open.
Indexing command comes as REST API.
Click Indexing.
Click Refresh.
Check indexing results.
Caution for Index
When index is requested, previous data are all deleted and replaced by new data.
REST API
Indexing API
Request
By File Uploading
curl -XPOST 'https://kr1-autocomplete.api.nhncloudservice.com/indexing/v2.0/appkeys/PyVTgcSXJpA3e5U7/serviceids/test/indexing?split=true&koreng=true&chosung=false' -H 'Accept-Language:en' -H 'Content-Type:multipart/form-data; charset=UTF-8' -F 'file=@documents.json'
By Payload
curl -i -XPOST 'https://kr1-autocomplete.api.nhncloudservice.com/indexing/v2.0/appkeys/PyVTgcSXJpA3e5U7/serviceids/test/indexing?split=true&koreng=true&chosung=false' -H 'Accept-Language:en' -H 'Content-Type:application/json; charset=UTF-8' -d '
[
{
"input": "Nike",
"weight": 3
},
{
"input": "Nike Sneakers",
"weight": 2
},
{
"input": "Sneakers",
"weight": 1
}
]'
Response
{
"id" : 1
}
Indexing Result Check API
Request
curl -i -XGET 'https://kr1-autocomplete.api.nhncloudservice.com/indexing/v2.0/appkeys/PyVTgcSXJpA3e5U7/serviceids/test/indexing_log?id=1' -H 'Accept-Language:en'
Response
{
"request_time" : "2017-10-23T12:36:43",
"file_name" : "documents.json",
"file_size" : 114,
"status" : 4
}
How to Autocomplete
Click Autocomplete.
Enter a word to search.
Specify output count.
Autocomplete REST API shows.
Result of autocomplete comes out. .
REST API
Request
curl -G -XGET 'https://kr1-autocomplete.api.nhncloudservice.com/autocomplete/v2.0/appkeys/PyVTgcSXJpA3e5U7/serviceids/test/autocomplete?count=10' -H 'Accept-Language:en' --data-urlencode query='ni'
Response
{
"collections" : [
{
"index" : 0,
"items" : [
[
"nikie"
],
[
"nike sneakers"
]
],
"title" : ""
}
],
"query" : [
"ni",
"ㅜㅑ"
],
"ver" : "2.0"
}
IPs may be restricted for equipment which may call indexing and autocomplete REST APIs. Testing on console does not require ACL setting.
How to Set ACL
Click ACL.
The example regards to indexing which is available only when the request IP is 202.179.177.21.
In the example, request for autocomplete can be made from all IPs.
Click Save.
Index
To test, index data as below:
[
{
"input": "Nike sneakers",
"weight": 2
},
{
"input": "Adidas sneakers",
"weight": 1
}
]
Select Middle-match for indexing.
Autocomplete
Enter Workout .
You can find in the middle Nike Sneakers starting with Workout
Index
To test, index data as below:
[
{
"input": "Nike",
"payload": [
"https://image.nhnent.com/images/nike.jpg",
"Brand>Sports"
],
"weight": 2
},
{
"input": "Adidas",
"payload": [
"https://image.nhnent.com/images/adidas.jpg",
"Brand>Sports"
],
"weight": 1
}
]
Enter additional information you need as output for payload.
Autocomplete
Index
To test, index data as below:
[
{
"input": "Shoes",
"output": "Sneakers",
"weight": 2
},
{
"input": "Boots",
"output": "Sneakers",
"weight": 1
}
]
Autocomplete
Enter 'sh'.
'Sneakers' comes as output.
Autocomplete results of more than two services come with one-time request for autocomplete API. For instance, the autocomplete for brand and category can be made available with one-time API request.
Creating Brand Services
Click Create Services, enter ID on a popup of Create Services, and click Create.
Brand Index
To test, index data as below:
[
{
"input": "Nike",
"weight": 2
},
{
"input": "Adidas",
"weight": 1
}
]
Creating Category Services
Click Create Services , enter ID on a popup for Create Services, and click Create.
Category Index
To test, index data as below:
[
{
"input": "Activewear",
"weight": 2
},
{
"input": "Outerwear",
"weight": 1
}
]
Autocomplete
Request
Requested with 'serviceids/brand,category' to call APIs.
curl -G -XGET 'https://kr1-autocomplete.api.nhncloudservice.com/autocomplete/v2.0/appkeys/PyVTgcSXJpA3e5U7/serviceids/brand,category/autocomplete?count=10' -H 'Accept-Language:en' --data-urlencode query='a'
Response
The API response shows autocomplete results for brand at index 0 and category at index 1.
{
"collections" : [
{
"index" : 0,
"items" : [
[
"adidas"
]
],
"title" : ""
},
{
"index" : 1,
"items" : [
[
"actiewear"
]
],
"title" : ""
}
],
"query" : [
"a",
"ㅁ"
],
"ver" : "2.0"
}
기본 색인은 입력할 수 있는 데이터 크기가 10MB로 제한되어 있습니다. 10MB를 초과하는 데이터를 입력할 때는 Full indexing API를 사용합니다.
curl -i -XPOST 'https://kr1-autocomplete.api.nhncloudservice.com/indexing/v2.0/appkeys/PyVTgcSXJpA3e5U7/serviceids/test/indexing/full/begin'
curl -XPOST 'https://kr1-autocomplete.api.nhncloudservice.com/indexing/v2.0/appkeys/PyVTgcSXJpA3e5U7/serviceids/test/indexing/full?split=true&koreng=true&chosung=true' -H 'Content-Type:multipart/form-data; charset=UTF-8' -F 'file=@documents-001.json'
curl -i -XPOST 'https://kr1-autocomplete.api.nhncloudservice.com/indexing/v2.0/appkeys/PyVTgcSXJpA3e5U7/serviceids/test/indexing/full/end'
curl -i -XPOST 'https://kr1-autocomplete.api.nhncloudservice.com/indexing/v2.0/appkeys/PyVTgcSXJpA3e5U7/serviceids/test/indexing/full/cancel'
데이터를 추가/수정/삭제할 때는 Incremental indexing API를 사용합니다.
1. 테스트를 위한 데이터 입력
curl -XPOST 'https://kr1-autocomplete.api.nhncloudservice.com/indexing/v2.0/appkeys/PyVTgcSXJpA3e5U7/serviceids/test/indexing?split=true&koreng=true&chosung=false' -H 'Accept-Language:en' -H 'Content-Type:application/json; charset=UTF-8' -d '
[
{
"id": "id-1",
"input": "나이키 신발",
"weight": 1
},
{
"id": "id-2",
"input": "아디다스 신발",
"weight": 1
}
]'
2. Incremental indexing
curl -XPOST 'https://kr1-autocomplete.api.nhncloudservice.com/indexing/v2.0/appkeys/PyVTgcSXJpA3e5U7/serviceids/test/indexing/incremental?split=true&koreng=true&chosung=false' -H 'Accept-Language:en' -H 'Content-Type:application/json; charset=UTF-8' -d '
[
{
"id": "id-1",
"action": "add",
"input": "나이키 운동화",
"weight": 1
},
{
"id": "id-2",
"action": "delete"
},
{
"id": "id-3",
"action": "add",
"input": "뉴발란스 운동화",
"weight": 1
}
]'
In the case of the following index files, when user enters 'no', the output shows in the order of 'notebook', 'notepad', 'note cards'.
[
{
"input": "notebook",
"weight": 3
},
{
"input": "notepad",
"weight": 2
},
{
"input": "note cards",
"weight": 1
}
]
ACL can be set on a page like this:
Input Formats
Following shows the file-uploading type index example codes.
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.6'
compile group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.5.6'
package com.toast.cloud.autocomplete.client;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
public class IndexingClient {
public static void main(String[] args) throws IOException {
String documents = ""
+ "[\n"
+ " {\n"
+ " \"input\": \"나이키\",\n"
+ " \"weight\": 3\n"
+ " }\n"
+ "]\n";
File tempFile = File.createTempFile("documents-",".json", new File("/tmp/"));
tempFile.deleteOnExit();
PrintWriter printWriter = new PrintWriter(tempFile);
printWriter.println(documents);
printWriter.close();
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
// build multipart upload request.
HttpEntity data = MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.addBinaryBody("file", tempFile, ContentType.DEFAULT_BINARY, tempFile.getName())
.build();
// build http request and assign multipart upload data.
HttpUriRequest request = RequestBuilder
.post("https://kr1-autocomplete.api.nhncloudservice.com/indexing/v2.0/appkeys/PyVTgcSXJpA3e5U7/serviceids/test/indexing?split=true&koreng=true&chosung=false")
.setEntity(data)
.build();
System.out.println("Executing request " + request.getRequestLine());
// Create a custom response handler.
ResponseHandler<String> responseHandler = response -> {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
};
String responseBody = httpclient.execute(request, responseHandler);
System.out.println(responseBody);
}
}
}
<?php
$documents = ""
. "[\n"
. " {\n"
. " \"input\": \"나이키\",\n"
. " \"weight\": 3\n"
. " }\n"
. "]\n";
$file = DIRECTORY_SEPARATOR.trim(sys_get_temp_dir(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.ltrim("documents.json", DIRECTORY_SEPARATOR);
file_put_contents($file, $documents);
register_shutdown_function(function() use($file) {
unlink($file);
});
$data = array(
'file' => curl_file_create($file, "application/json", basename($file))
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://kr1-autocomplete.api.nhncloudservice.com/indexing/v2.0/appkeys/PyVTgcSXJpA3e5U7/serviceids/test/indexing?split=true&koreng=true&chosung=false");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data; charset=UTF-8"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$response = curl_exec($ch) or die(curl_error($ch));
print_r($response);
curl_close($ch);
?>