ElasticSearch(Settings,Mappings)

ElasticSearch(Settings,Mappings)

1.Setting 是 针对于索引库而言
可以设置索引库的分片数量副本数量

url 的方式 设置和修改

"settings": {
        "number_of_shards": 5,
        "number_of_replicas": 1

api 前面在创建索引库的时候已经讲过了,就不重复了

2.Mappings相当于数据库中对字段的类型约束 以及 某些字段查询时指定分词器
具体解释请看官网
包含数据类型 (text,keyword,date,long,interger……………)
别的不多说了 看下我创建的mapping

"mappings": {
        "user": {//映射类型
            "properties": {//定义字段
                "title": {
                    "type": "text",//字段类型
                    "analyzer": "ik_max_word",
                    "search_analyzer": "ik_max_word"//查询分词
                },
                "name": {
                    "type": "text"
                },
                "age": {
                    "type": "long"
                }
            }
        },
        "blog": {
            "properties": {
                "title": {
                    "type": "text",
                    "analyzer": "ik_max_word",
                    "search_analyzer": "ik_max_word"
                },
                "content": {
                    "type": "text",
                    "analyzer": "ik_max_word",
                    "search_analyzer": "ik_max_word"
                }
            }
        }
    }

创建Mapping的javaApi

        // 以XContentBuilder形式
   XContentBuilder builder = XContentFactory.jsonBuilder()
             .startObject()
                .startObject("properties")
                    .startObject("name")
                        .field("type", "text")
                    .endObject()
                    .startObject("title")
                        .field("type", "text")
                        .field("analyzer", "ik_max_word")
                        .field("search_analyzer", "ik_max_word")
                    .endObject()
                .endObject()
             .endObject();
        PutMappingResponse response = client.admin().indices().preparePutMapping()
                .setIndices("dragon").setType("ccc")
                .setSource(builder).get();
        System.out.println(response.isAcknowledged());







//以json形式: 要注意要指定type  setType  
josn文件:  { "properties": {
                "title": {
                    "type": "text",
                    "analyzer": "ik_max_word",
                    "search_analyzer": "ik_max_word"
                },
                "name": {
                    "type": "text"
                },
                "age": {
                    "type": "long"
                }
            }}

  String json ="{   \"properties\": {\n" +
                "                \"title\": {\n" +
                "                    \"type\": \"text\",\n" +
                "                    \"analyzer\": \"ik_max_word\",\n" +
                "                    \"search_analyzer\": \"ik_max_word\"\n" +
                "                },\n" +
                "                \"name\": {\n" +
                "                    \"type\": \"text\"\n" +
                "                },\n" +
                "                \"age\": {\n" +
                "                    \"type\": \"long\"\n" +
                "                }\n" +
                "            }}";
        byte[] bytes = json.getBytes();
        PutMappingResponse response = client.admin().indices()
                .preparePutMapping("dragon").setType("aaa")
                .setSource(json, XContentType.JSON).get();

        System.out.println(response.isAcknowledged());
    }

我创建 一个索引库这样来:

curl -XPUT 'localhost:9200/索引库名' -d '
{
    "settings": {
        "number_of_shards": 5,
        "number_of_replicas": 1
    },
    "mappings": {
        "user": {
            "properties": {
                "title": {
                    "type": "text",
                    "analyzer": "ik_max_word",
                    "search_analyzer": "ik_max_word"
                },
                "name": {
                    "type": "text"
                },
                "age": {
                    "type": "long"
                }
            }
        },
        "blog": {
            "properties": {
                "title": {
                    "type": "text",
                    "analyzer": "ik_max_word",
                    "search_analyzer": "ik_max_word"
                },
                "content": {
                    "type": "text",
                    "analyzer": "ik_max_word",
                    "search_analyzer": "ik_max_word"
                }
            }
        }
    }
}
'  //别忘了这个
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值