本文地址:http://blog.csdn.net/gumpeng/article/details/50414028
本文主要是基于java语言,描述如果建议一个Elasticsearch索引数据的简单过程。
1.建立schema
新建一个名为test_mapping.json的文件,存放索引的schema,具体内容如下:
{
"mappings": {
"stock": {
"_id": {
"path": "id"
},
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string",
"index": "not_analyzed"
},
"companyDesc": {
"type": "string",
"analyzer": "standard"
}
}
}
}
}
然后,将schem注入到ES引擎中,具体代码如下:
public static String fileAsString(String file) throws IOException {
if (new File(file).exists()) {
CharSource mapping = Files.asCharSource(new File(file),
Charset.forName("utf-8"));
String str = mapping.read();
return str;
} else {
InputStream input = EsHelper.class.getClassLoader()
.getResourceAsStream(file);
if (input != null) {
String string = CharStreams.toString(new InputStreamReader(
input, "UTF-8"));
return string;
}