首页 文章

弹性搜索:“术语”,“匹配短语”和“查询字符串”之间的区别

提问于
浏览
74

这里是弹性搜索的新手,并试图更好地理解这些查询之间的差异 . 据我所知, term 匹配单个术语(匹配工作需要小写?), match phrasequery string 匹配一串文本 .

2 回答

  • 10

    term 查询匹配单个术语:值为 not analyzed . 因此,它不必是小写的,具体取决于您索引的内容 .

    如果您在索引时提供 Bennett 并且未分析该值,则以下查询将不返回任何内容:

    {
      "query": {
        "term" : { "user" : "bennett" }
      }
    }
    

    如果为查询字段定义了分析器并查找与以下标准匹配的文档, match_phrase 查询将分析输入:

    • all the terms 必须出现在该字段中

    • 他们必须将 same order 作为输入值

    例如,如果索引以下文档(使用 standard analyzer作为字段 foo ):

    { "foo":"I just said hello world" }
    
    { "foo":"Hello world" }
    
    { "foo":"World Hello" }
    

    match_phrase 查询仅返回第一个和第二个文档:

    {
      "query": {
        "match_phrase": {
          "foo": "Hello World"
        }
      }
    }
    

    query_string 默认情况下,查询在_all字段上搜索,该字段一次包含多个文本字段的文本 . 最重要的是,它被解析并支持一些运算符(AND / OR ...),通配符等(参见related syntax) .

    作为 match_phrase 查询,根据查询字段上设置的分析器分析输入 .

    match_phrase 不同,分析后获得的术语不必具有相同的顺序,除非用户在输入周围使用了引号 .

    例如,使用与以前相同的文档,此查询将返回所有文档:

    {
      "query": {
        "query_string": {
          "query": "hello World"
        }
      }
    }
    

    但是此查询将返回与 match_phrase 查询相同的2个文档:

    {
      "query": {
        "query_string": {
          "query": "\"Hello World\""
        }
      }
    }
    

    关于这些查询的不同选项还有很多话要说,请查看相关文档:

    希望这很清楚,它会有所帮助 .

  • 154

    我认为有人肯定在寻找他们之间的差异 PARTIAL SEARCH 这是我的分析默认 ‘standard analyzer’ : -

    假设,我们有数据: -

    {“name”:“你好”}

    Now what if we want to do partial search with ell ???

    术语查询或匹配查询

    {"term":{"name": "*ell*" }
    

    不会工作,返回注意 .

    {"term":{"name": "*zz* *ell*" }
    

    不会工作,返回注意 .

    Conclusion - 术语或匹配根本无法进行部分搜索

    通配符查询: -

    {"wildcard":{"name": "*ell*" }
    

    将工作给出结果{“name”:“你好”}

    {"wildcard":{"name": "*zz* *ell*" }
    

    不会工作,返回注意 .

    Conclusion - 通配符只能使用一个令牌进行部分搜索

    Query_string: -

    {"query_string": {"default_field": "name","query": "*ell*"}
    

    将工作给出结果{“name”:“你好”}

    {"query_string": {"default_field": "name","query": "*zz* *ell*" }
    

    将工作给出结果{“name”:“Hello”} .

    Conclusion - query_string能够搜索两个令牌

    • 这里的标记是ell和zz

相关问题