elasticsearch 搜索的时候怎么设置对搜索词不进行分词

2020-06-15 13:48:40 +08:00
 ThanksSirAlex

用了_analyze 接口,发现查询潮流的时候会这样

{
  "tokens": [
    {
      "token": "潮",
      "start_offset": 0,
      "end_offset": 1,
      "type": "<IDEOGRAPHIC>",
      "position": 0
    },
    {
      "token": "流",
      "start_offset": 1,
      "end_offset": 2,
      "type": "<IDEOGRAPHIC>",
      "position": 1
    }
  ]
}

但是我不希望他对搜索词进行分词

mapping 是这样的

mappings: {
                   product: {
                     properties: {
                       title:      { type: 'text', boost: 100, index: 'not_analyzed' }, # analyzer: 'ik_smart', index: 'not_analyzed'
                       summary:    { type: 'text', boost: 10, index: 'not_analyzed' }, # analyzer: 'ik_smart', index: 'not_analyzed' },
                       content:    { type: 'text', boost: 10, index: 'not_analyzed' }, # analyzer: 'ik_smart', index: 'not_analyzed' },
                       class_name: { type: 'string', index: "not_analyzed" },
                       tags:       { type: 'text', boost: 100, index: 'not_analyzed' }, # analyzer: 'ik_smart', index: 'not_analyzed' },
                       date:       { type: 'date',  format: "yyyy-MM-dd'T'HH:mm:ss.SSSZZ"}
                     }
                   }
                 }
2592 次点击
所在节点    问与答
9 条回复
sss495088732
2020-06-15 13:53:39 +08:00
type:"keyword",新版本好像没有 not_analyzed 这种说法了,仅 text 类型支持分词
ThanksSirAlex
2020-06-15 14:21:57 +08:00
@sss495088732 老项目了,用的还是 es5.5.3
sss495088732
2020-06-15 16:55:35 +08:00
@ThanksSirAlex 那就非常僵硬了 0.0
wupeaking
2020-06-15 17:00:09 +08:00
可以在建立 mapping 时再单独为这个字段创建一个不分词的属性
zxc12300123
2020-06-15 17:19:41 +08:00
match_phrase
ThanksSirAlex
2020-06-15 20:46:47 +08:00
@zxc12300123 用了 match_phrase 一条结果都搜不到了,有点懵。。。
optional
2020-06-15 23:29:26 +08:00
创建两个字段
misaka19000
2020-06-15 23:37:49 +08:00
看 ES 文档
JRay
2020-06-16 11:36:24 +08:00
在搜索字段的时候,在字段名称后面加上.keyword

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/681666

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX