2.使用term filter搜索数据

主要知识点

根据用户ID、是否隐藏、帖子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个字段并插入4条数据,因为整个es是支持json document格式的,所以说扩展性和灵活性非常好。如果后续随着业务需求的增加,要在document中增加更多的field,那么我们可以很方便的随时添加field。和关系型数据库比较,比如mysql,我们如果先建立了一个表,之后要给表中新增一些column,那就很麻烦了,必须用复杂的修改表结构的语法去执行。而且可能对系统代码还有一定的影响。

   

2、查看mapping结构

语法:GET /forum/_mapping/article

执行结果

{

"forum": {

"mappings": {

"article": {

"properties": {

"articleID": {

"type": "text",

"fields": {

"keyword": {

"type": "keyword",

"ignore_above": 256

}

}

},

"hidden": {

"type": "boolean"

},

"postDate": {

"type": "date"

},

"userID": {

"type": "long"

}

}

}

}

}

}

   

es5.x之后的版本,如果type=text,默认会设置两个field,一个是field本身,是分词的;还有一个是field.keyword,默认不分词,会最多保留256个字符,比如本例中的articleID

   

二、根据用户ID搜索帖子

   

GET /forum/article/_search

{

"query" : {

"constant_score" : {

"filter" : {

"term" : {

"userID" : 1

}

}

}

}

}

   

term 搜索(term filter/query),是指对搜索文本不分词,直接以原搜索文本到倒排索引中匹配,原样匹配。比如说,如果对搜索文本进行分词的话,"helle world" 会分成两个单词 "hello"和"world",两个词分别去倒排索引中匹配。只要匹配一个就是结果,如果是term搜索,那么就会以"hello world" 直接去倒排索引中匹配"hello world",只有全匹配才是结果。

   

三、搜索没有隐藏的帖子

   

GET /forum/article/_search

{

"query" : {

"constant_score" : {

"filter" : {

"term" : {

"hidden" : false

}

}

}

}

}

   

四、根据发帖日期搜索帖子

   

GET /forum/article/_search

{

"query" : {

"constant_score" : {

"filter" : {

"term" : {

"postDate" : "2017-01-01"

}

}

}

}

}

   

五、根据帖子ID搜索帖子

1、还是以term搜索的方式去搜索:

GET /forum/article/_search

{

"query" : {

"constant_score" : {

"filter" : {

"term" : {

"articleID" : "XHDK-A-1293-#fJ3"

}

}

}

}

}

执行结果如下,发现并没有找到结果,

{

"took": 1,

"timed_out": false,

"_shards": {

"total": 5,

"successful": 5,

"failed": 0

},

"hits": {

"total": 0,

"max_score": null,

"hits": []

}

}

2、以.keywrod 方式去搜索:

GET /forum/article/_search

{

"query" : {

"constant_score" : {

"filter" : {

"term" : {

"articleID.keyword" : "XHDK-A-1293-#fJ3"

}

}

}

}

}

执行结果如下:发现已搜索出结果,

{

"took": 2,

"timed_out": false,

"_shards": {

"total": 5,

"successful": 5,

"failed": 0

},

"hits": {

"total": 1,

"max_score": 1,

"hits": [

{

"_index": "forum",

"_type": "article",

"_id": "1",

"_score": 1,

"_source": {

"articleID": "XHDK-A-1293-#fJ3",

"userID": 1,

"hidden": false,

"postDate": "2017-01-01"

}

}

]

}

}

通过比较发现:articleID.keyword,是es最新版本内置建立的field,就是不分词的。所以一个articleID建立的时候,会建立两次索引,一次是自己本身,是要分词的,分词后放入倒排索引;另外一次是基于articleID.keyword,不分词,最多保留256个字符后直接以一个字符串的形式放入倒排索引中。所以term filter,对text过滤,可以考虑使用内置的field.keyword来进行匹配。但是有个问题,默认就保留256个字符。所以尽可能还是自己去手动建立索引,指定not_analyzed(详情见第45小节)。在最新版本的es中,不需要指定not_analyzed也可以,将type=keyword即可。

   

3、查看分词

语法:

GET /forum/_analyze

{

"field": "articleID",

"text": "XHDK-A-1293-#fJ3"

}

执行结果:

{

"tokens": [

{

"token": "kdke",

"start_offset": 0,

"end_offset": 4,

"type": "<ALPHANUM>",

"position": 0

},

{

"token": "b",

"start_offset": 5,

"end_offset": 6,

"type": "<ALPHANUM>",

"position": 1

},

{

"token": "9947",

"start_offset": 7,

"end_offset": 11,

"type": "<NUM>",

"position": 2

},

{

"token": "kl5",

"start_offset": 13,

"end_offset": 16,

"type": "<ALPHANUM>",

"position": 3

}

]

}

默认如果是analyzedtext类型的field,建立倒排索引的时候,就会对所有的field进行分词,分词以后原本的articleID就没有了,只有分词后的各个word存在于倒排索引中。

term,是不对搜索文本分词的,XHDK-A-1293-#fJ3 对应的搜索文本就是 XHDK-A-1293-#fJ3;但是articleID建立索引的时候,XHDK-A-1293-#fJ3 已被分词为 xhdka1293fj3,所以找不到

   

4、重建索引

   

DELETE /forum

   

PUT /forum

{

"mappings": {

"article": {

"properties": {

"articleID": {

"type": "keyword"

}

}

}

}

}

5、再次插入数据

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" }

   

6、重新根据帖子ID和发帖日期进行搜索

   

GET /forum/article/_search

{

"query" : {

"constant_score" : {

"filter" : {

"term" : {

"articleID" : "XHDK-A-1293-#fJ3"

}

}

}

}

}

   

六、总结

1term filter:根据exact value进行搜索,数字、booleandate天然支持

2text需要建索引时指定为not_analyzed,才能用term query

3)相当于SQL中的单个where条件

select * from forum.article where articleID='XHDK-A-1293-#fJ3'

原文地址:https://www.cnblogs.com/liuqianli/p/8482081.html