V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
whereabouts
V2EX  ›  程序员

请教博客文章索引到 Elasticsearch 后,对多个字段搜索带空格单词的语法?

  •  
  •   whereabouts · 2018-08-30 13:33:44 +08:00 · 3302 次点击
    这是一个创建于 2064 天前的主题,其中的信息可能已经有所发展或是发生改变。
    博客文章索引到了 Elasticsearch,有标题 title、正文 body、简介 brief 三个字段,现在用户在博客上搜索带空格的词如"hello world",需求是三个字段其中一个能全文搜索出"hello world"就行了。
    语言 nodejs,用的是 ES 的 JavaScript API,搜索了 ES 文档,多个字段的语法现在写成:
    es.earch({
    index: "blog",
    type: "blog",
    body: {
    ...
    query: {
    multi_match: {
    query: "hello world",
    fields: ["title", "body", "brief"]
    }
    }
    }

    match_match 实现了对三个字段的搜索(区别于 match 只搜索一个),但是搜出来的结果是,只要有 hello 或 world 就属于结果,应该是带空格的搜索词“ hello world ”当成了 OR 的搜索,匹配任意一个。
    请教如何能在多字段的情况下做类似 AND 的搜索?需求是"hello world"要连起来才算匹配,中间只能有一个空格,只是在字段中出现过 hello 和 world 两个词的结果不算,类似于谷歌带两个引号的那种精确匹配。
    2 条回复    2018-08-30 14:19:23 +08:00
    csyjgu
        1
    csyjgu  
       2018-08-30 13:39:22 +08:00 via Android
    先分词再搜索,每个词在三个字段里都搜,每个词都能匹配上才返回,用多个 must query。
    yuan0808
        2
    yuan0808  
       2018-08-30 14:19:23 +08:00
    分词啊,不分词得用 term
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3587 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 04:55 · PVG 12:55 · LAX 21:55 · JFK 00:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.