好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

ElasticSearch如何设置某个字段不分词浅析

先说结论:字段类型更改为 'keyword'

elasticSearch官方文档中创建index代码如下

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

PUT /my_store

{

     "mappings" : {

         "products" : {

             "properties" : {

                 "productID" : {

                     "type" : "string" ,

                     "index" : "not_analyzed"

                 }

             }

         }

     }

 

}

由于es官方文档版本基于2.x编写,而本人安装版本为6.6 在执行如上代码过程中出现如下错误

No handler for type [string] declared on field [productID]

这里报错是因为ElasticSearch5.x以上版本没有string类型了,换成了text和keyword作为字符串类型。

字符串 - text:用于全文索引,该类型的字段将通过分词器进行分词,最终用于构建索引

字符串 - keyword:不分词,只能搜索该字段的完整的值,只用于 filtering

此时我们将文档中代码更改为如下

?

1

2

3

4

5

6

7

8

9

10

11

12

13

PUT /my_store

{

     "mappings" : {

         "products" : {

             "properties" : {

                 "productID" : {

                     "type" : "keyword" ,

                     "index" : true

                 }

             }

         }

     }

}

创建成功,此时我们进行查询试试看

?

1

2

3

4

5

6

7

8

9

10

11

12

GET /my_store/products/_search

{

     "query" : {

         "constant_score" : {

             "filter" : {

                 "term" : {

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

                 }

             }

         }

     }

}

总结

到此这篇关于ElasticSearch如何设置某个字段不分词的文章就介绍到这了,更多相关ElasticSearch设置字段不分词内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://HdhCmsTestcnblogs测试数据/cfas/p/16142883.html

查看更多关于ElasticSearch如何设置某个字段不分词浅析的详细内容...

  阅读:22次