Skip to content

Instantly share code, notes, and snippets.

@horitaku1124
Last active May 15, 2017 13:13
Show Gist options
  • Save horitaku1124/d953570872948863928d24ad80753878 to your computer and use it in GitHub Desktop.
Save horitaku1124/d953570872948863928d24ad80753878 to your computer and use it in GitHub Desktop.
cd solr-6.5.0
mkdir -p server/solr/mycore1/conf
cp -r server/solr/configsets/basic_configs/conf/* server/solr/mycore1/conf
curl -s 'http://localhost:8984/solr/admin/cores?action=CREATE&name=mycore1&instanceDir=mycore1&wt=json&indent=4'

managed-schema

  <fields>
    <field name="name"       type="text_ja" indexed="true" stored="true"/>
    <field name="file_path"       type="text_ja" indexed="true" stored="true"/>
    <field name="content"    type="text_ja" indexed="true" stored="true"/>
  </fields>
bin/solr restart -p 8984
echo '[{"id":1, "path_id": 1, "name": "abc", "content": "aaaabi"}]' > data.json
curl 'http://localhost:8984/solr/mycore1/update/json?commit=true' --data-binary @data.json -H 'Content-type:application/json;charset:utf-8'

参考 http://vimtaku.github.io/blog/2014/02/13/solr-sample/

import okhttp3.*;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
/**
* Created by tak on 4/26/17.
*/
public class Test1 {
private static Pattern fileTarget = Pattern.compile("\\.c$");
public static void main(String[] args) throws IOException {
final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
if(args.length == 0) {
System.err.println("FileDir is required.");
System.exit(1);
}
String startDir = args[0];
List<String[]> foundFiles = new ArrayList<>();
search(new File(startDir), foundFiles);
System.out.println(foundFiles.size());
OkHttpClient client = new OkHttpClient();
for(int i = 0;i < 10;i++) {
String fileName = foundFiles.get(i)[0];
String fullPath = foundFiles.get(i)[1];
String content = fileRead(fullPath);
content = content.replaceAll("\"", "\\\\\"");
String json = String.format(
"[{\"id\": %d, \"name\": \"%s\", \"file_path\": \"%s\", \"content\": \"%s\"}]",
i, fileName, fullPath, content
);
System.out.println(json);
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url("http://localhost:8984/solr/mycore1/update/json?commit=true")
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
System.out.println(response.body().string());
}
}
}
private static void search(File dir, List<String[]> foundFile) {
File[] children = dir.listFiles();
if(children == null) return;
for(File file: children) {
if(file.isFile()) {
String fileName = file.getName();
if (fileTarget.matcher(fileName).find() && file.length() < 1000) {
foundFile.add(new String[]{fileName, file.getAbsoluteFile().toString()});
}
} else {
search(file, foundFile);
}
}
}
private static String fileRead(String filePath) throws IOException {
File file = new File(filePath);
FileReader filereader = new FileReader(file);
char[] buf = new char[1000000];
StringBuilder sb = new StringBuilder();
while(true) {
int len = filereader.read(buf);
if (len <= 0) {
break;
}
sb.append(new String(buf, 0, len));
}
return sb.toString();
}
}
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import okhttp3.*;
import java.io.IOException;
/**
* Created by tak on 4/27/17.
*/
public class Test2 {
public static void main(String[] args) {
String url = "http://localhost:8984/solr/mycore1/select?indent=on&q=*:*&wt=json";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String json = response.body().string();
JsonObject jsonObj = new Gson().fromJson(json, JsonObject.class);
JsonObject responseO = jsonObj.getAsJsonObject("response");
JsonArray docs = responseO.getAsJsonArray("docs");
for (int i = 0; i < docs.size(); i++) {
JsonObject obj = docs.get(i).getAsJsonObject();
String name = obj.get("name").getAsString();
System.out.println("Name = " + name);
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment