ElaticSearch使用term filter来搜索数据
语法
{action:{metadata}}\n
{request body}\n
{action:{metadata}}\n
{request body}\n
.....
action行为 | 解释 |
---|---|
create | 当文档不存在时进行创建 |
index | 创建新文档或替换已有文档 |
update | 局部更新文档 |
delete | 删除一个文档 |
例如:
{"delete":{"_index":"library","_type":"books","_id":"1"}}
POST /forum/article/_bulk
{ "index": { "_id": 1 }}
{ "articleID" : "XHDK-A-1293-#fJ3", "userID" : 1, "hidden": false, "postDate": "2017-01-01" }
{ "index": { "_id": 2 }}
{ "articleID" : "KDKE-B-9947-#kL5", "userID" : 1, "hidden": false, "postDate": "2017-01-02" }
{ "index": { "_id": 3 }}
{ "articleID" : "JODL-X-1937-#pV7", "userID" : 2, "hidden": false, "postDate": "2017-01-01" }
{ "index": { "_id": 4 }}
{ "articleID" : "QQPX-R-3956-#aD8", "userID" : 2, "hidden": true, "postDate": "2017-01-02" }
注意:最后一行也要进行换行,不然会报错
初步来说,就先搞4个字段,因为整个es是支持json document格式的,所以说扩展性和灵活性非常之好。如果后续随着业务需求的增加,要在document中增加更多的field,那么我们可以很方便的随时添加field。
GET /forum/article/_search
{
"query" : {
"constant_score" : {
"filter" : {
"term" : {
"userID" : 1
}
}
}
}
}
注意点:所以就会产生一个问题,比如我们在demo(索引)下新增这4个文档
当我们使用term去搜索"spotless future"时无法搜索到结果
原因在于:分词器将"spotless future"分析成"spotless"和"future"作为关键词,生成到倒排索引的检索目录中
GET /demo/_analyze
{
"field": "text",
"text": "Whatever your past has been, you have a spotless future"
}
通过term,将"spotless future"作为一个关键词去倒排索引中匹配,当然是无法匹配的
替代的查询方法:match_phrase
GET /forum/article/_search
{
"query" : {
"constant_score" : {
"filter" : {
"term" : {
"hidden" : false
}
}
}
}
}
GET /forum/article/_search
{
"query" : {
"constant_score" : {
"filter" : {
"term" : {
"postDate" : "2017-01-01"
}
}
}
}
}
GET /forum/article/_search
{
"query" : {
"constant_score" : {
"filter" : {
"term" : {
"articleID" : "XHDK-A-1293-#fJ3"
}
}
}
}
}
使用field.keyword进行匹配
GET /forum/article/_search
{
"query" : {
"constant_score" : {
"filter" : {
"term" : {
"articleID.keyword" : "XHDK-A-1293-#fJ3"
}
}
}
}
}
当我们插入的数据是text类型时,例如插入"articleID":“XHDK-A-1293-#fJ3”,在mappings中,除了原本的articleID字段,在articleID字段下还有一个keyword字段
验证articleID的分词
GET /forum/_analyze
{
"field": "articleID",
"text": "XHDK-A-1293-#fJ3"
}
分词器将 "XHDK-A-1293-#fJ3"分成"xhdk","a","1293","fj3"放入倒排索引中(字段为articleID)
验证articleID.keyword的分词
GET /forum/_analyze
{
"field": "articleID.keyword",
"text": "XHDK-A-1293-#fJ3"
}
直接将"XHDK-A-1293-#fJ3"扔到倒排索引中(字段为articleID.keyword)
GET /forum/_analyze
{
"field": "articleID",
"text": "XHDK-A-1293-#fJ3"
}
DELETE /forum
PUT /forum
{
"mappings": {
"article": {
"properties": {
"articleID": {
"type": "keyword"
}
}
}
}
}
POST /forum/article/_bulk
{ "index": { "_id": 1 }}
{ "articleID" : "XHDK-A-1293-#fJ3", "userID" : 1, "hidden": false, "postDate": "2017-01-01" }
{ "index": { "_id": 2 }}
{ "articleID" : "KDKE-B-9947-#kL5", "userID" : 1, "hidden": false, "postDate": "2017-01-02" }
{ "index": { "_id": 3 }}
{ "articleID" : "JODL-X-1937-#pV7", "userID" : 2, "hidden": false, "postDate": "2017-01-01" }
{ "index": { "_id": 4 }}
{ "articleID" : "QQPX-R-3956-#aD8", "userID" : 2, "hidden": true, "postDate": "2017-01-02" }
当articleID的类型指定为keyword,就不会进行分词了
{
"query" : {
"constant_score" : {
"filter" : {
"term" : {
"articleID" : "XHDK-A-1293-#fJ3"
}
}
}
}
}
select *
from forum.article
where articleID='XHDK-A-1293-#fJ3'