1、新增索引
curl -H "Content-Type:application/json;charset=utf-8" -XPUT 'http://127.0.0.1:9200/test_0527'
2、新增mapping
type为"text"类型的字段:
text:会被分词,然后进行索引,支持模糊查询;不支持聚合(适用于全文搜索)
keyword:不进行分词,直接索引;精确查询;支持聚合(适用于关键词搜索)
curl -H "Content-Type:application/json;charset=utf-8" -XPOST 'http://127.0.0.1:9200/test_0527/doc/_mapping?' -d '{"doc":{"date_detection":false,"properties":{
"name":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
"mobile_phone":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}
}}}'
3、查询mapping
curl -H "Content-Type:application/json;charset=utf-8" -XGET 'http://127.0.0.1:9200/test_0527/_mapping?pretty'
4、插入文档
curl -H "Content-Type:application/json;charset=utf-8" -XPOST 'http://127.0.0.1:9200/test_0527/doc?' -d
'{"name":"张三","mobile_phone":"13522335566"}'
5、查询文档
// 全文查询
curl -H "Content-Type:application/json;charset=utf-8" -XPOST 'http://127.0.0.1:9200/test_0527/doc/_search?pretty'
// 全文查询返回限定字段
curl -H "Content-Type:application/json;charset=utf-8" -POST 'http://127.0.0.1:9200/test_0527/_search?pretty' -d '{"_source": ["mobile_phone"]}'
// 条件查询,短语精准匹配
curl -H "Content-Type:application/json;charset=utf-8" -XPOST 'http://127.0.0.1:9200/test_0527/_search?pretty' -d '{"query":{"match_phrase":{"name":"王五"}}}'
// 条件查询,term精准查询
curl -H "Content-Type:application/json;charset=utf-8" -XPOST 'http://127.0.0.1:9200/test_0527/doc/_search?' -d '{"query":{"term":{"mobile_phone.keyword":"13522335566"}}}'
// 条件查询,分词匹配
curl -H "Content-Type:application/json;charset=utf-8" -XPOST 'http://127.0.0.1:9200/test_0527/_search?pretty' -d '{"query":{"match":{"name":"王"}}}'
6、删除文档
curl -H "Content-Type:application/json;charset=utf-8" -XDELETE 'http://127.0.0.1:9200/test_0527/doc/zo30-GoByqlpGcLNsU9t?'
7、给索引添加别名
curl -H "Content-Type:application/json;charset=utf-8" -XPOST 'http://127.0.0.1:9200/_aliases' -d '{"actions": [{"add":{"index":"test_0527", "alias":"test"}}]}'
8、查询索引某个字段存在的文档
curl -H "Content-Type:application/json;charset=utf-8" -XPOST 'http://127.0.0.1:9200/test_0527/_search?pretty' -d '{"query":{"exists" : {"field": "mobile_phone"}}}'