V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
whereabouts
V2EX  ›  程序员

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

  •  
  •   whereabouts · Aug 30, 2018 · 4044 views
    This topic created in 2801 days ago, the information mentioned may be changed or developed.
    博客文章索引到了 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 replies    2018-08-30 14:19:23 +08:00
    csyjgu
        1
    csyjgu  
       Aug 30, 2018 via Android
    先分词再搜索,每个词在三个字段里都搜,每个词都能匹配上才返回,用多个 must query。
    yuan0808
        2
    yuan0808  
       Aug 30, 2018
    分词啊,不分词得用 term
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   811 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 197ms · UTC 22:30 · PVG 06:30 · LAX 15:30 · JFK 18:30
    ♥ Do have faith in what you're doing.