Elasticsearch 高手之路
  • Introduction
  • First Chapter
  • 1.ElasticSearch 5.x 安装
    • 1.1.Window 环境
    • 1.2.Linux 环境
  • 2.基础学前班
  • 3.基础
    • 3.1.配置文件
    • 3.2.Mapping
      • 3.2.1.字段的数据类型
        • 3.2.1.1.核心数据类型
        • 3.2.1.2.复合数据类型
        • 3.2.1.3.Geo地理数据类型
        • 3.2.1.4.特定数据类型
      • 3.2.2.Meta-Fields(元字段)
        • _index,_uid,_type,_id 元字段
        • _source,_size 元字段
        • _all, _field_names元字段
        • _parent,_routing 元字段
        • _meta 元字段
      • 3.2.3.Mapping parameters(映射参数)
        • analyzer(分析器)
        • normalizer(归一化)
        • boost(提升)权重
        • Coerce(强制类型转换)
        • copy_to(合并参数)
        • doc_values(文档值)
        • dynamic(动态设置)
        • enabled(开启字段)
        • fielddata(字段数据)
        • format (日期格式)
        • ignore_above(忽略超越限制的字段)
        • ignore_malformed(忽略格式不对的数据)
        • include_in_all(_all 查询包含字段)
        • index_options(索引设置)
        • index (索引)
        • fields(字段)
        • norms (标准信息)
        • null_value(空值)
        • position_increment_gap(短语位置间隙)
        • properties (属性)
        • search_analyzer (搜索分析器)
        • similarity (相似度模型)
        • store(存储)
        • term_vectors(词根信息)
      • 3.2.4.Dynamic Mapping(动态映射)
        • _default_ mapping(mapping中的_default_)
        • Dynamic field mapping(动态字段映射)
        • Dynamic templates(动态模板)
        • Override default template(覆盖默认模板)
    • 3.3. Analysis(分析)
      • 3.3.1.Anatomy of an analyzer(分析器的分析)
      • 3.3.2.Testing analyzers(测试分析器)
      • 3.3.3.Analyzers(分析器)
        • Configuring built-in analyzers(配置内置分析器)
        • Standard Analyzer(标准分析器)
        • Simple Analyzer(简单分析器)
        • Whitespace Analyzer(空格分析器)
        • Stop Analyzer(停止词分词器)
        • Keyword Analyzer(关键词分析器)
        • Pattern Analyzer(模式分析器)
        • Language Analyzers(语言分析器)
        • Fingerprint Analyzer(指纹分析器)
        • Custom Analyzer(自定义分析器)
      • 3.3.4. Tokenizers(分词器)
        • Standard Tokenizer(标准分词器)
        • Letter Tokenizer
        • Lowercase Tokenizer (小写分词器)
        • Whitespace Tokenizerr (空格分词器)
        • UAX URL Email Tokenizer
        • Classic Tokenizer
        • Thai Tokenizer(泰语分词器)
        • NGram Tokenizer
        • Edge NGram Tokenizer
        • Keyword Tokenizer(关键词分词器)
        • Pattern Tokenizer(模式分词器)
        • Path Hierarchy Tokenizer(路径层次分词器)
      • 3.3.5.Token Filters(词语过滤器)
      • 3.3.5.补充1:Token Filters(词语过滤器)
      • 3.3.5.补充2:Token Filters(词语过滤器)
      • 3.3.6.Character Filters(字符过滤器)
        • HTML Strip Character Filter(HTML标签过滤)
        • Mapping Character Filter(字符替换映射)
        • Pattern Replace Character Filter(正则替换字符)
    • 3.4. APIs
      • 3.4.1.索引 APIs (Indices APIs)
        • 创建/删除/获取->索引
        • 启动关闭/缩小/滚动->索引
        • 提交/获取/获取字段->映射
        • 索引->别名/是否存在/类型存在
        • 更新索引/获取->设置(未完成)
        • 分析器、索引模板(未完成)
        • Shadow replica indices 卷影副本索引
        • 索引->统计信息/段
        • 索引->恢复/分片存储
        • 清理缓存/刷新/同步刷新
        • 重新加载/强制合并
      • 3.4.2.文档 APIs (Document APIs)
        • 读写文档(Reading and Writing documents)
        • 索引文档 API
        • 获取/批量获取->文档
        • 删除/根据查询API删除
        • 更新/根据查询API更新
        • Bulk API(批量操作)
        • Reindex API(复制索引)
        • Term Vectors(词条向量)/Multi termvectors API
        • ?refresh
      • 3.4.3.搜索 APIs (Search APIs)
        • Search / URI Search
        • Request Body Search(未完成)
          • Query / From / Size
          • Sort / Source filtering
          • Fields / Script Fields / Doc value Fields
          • Post filter
          • Highlighting
          • Rescoring / Search Type
          • Scroll
          • Preference / Explain
          • Version / Index Boost
          • min_score / Named Queries
          • Inner hits / Search After
          • Field Collapsing 字段折叠
        • Search 模板/Multi Search 模板
        • Search Shards API
        • Suggesters
          • Term suggester
          • Phrase Suggester
          • Completion Suggester
          • Context Suggester
          • 返回suggester的类型
        • Multi Search API
        • Count API
        • Validate API
        • Explain API
        • Profile API
          • Profiling Queries
          • Profiling Aggregations
          • Profiling Considerations
        • Percolator / Field stats API
        • Field Capabilities API
    • 3.5.Query DSL(DSL方式查询)
      • 3.5.1.查询和过滤上下文
      • 3.5.2.Match All 查询
      • 3.5.3.全文搜索(Full Text Search)
        • 匹配查询(Match Query)
        • 短语匹配查询(Match Phrase Query)
        • 短语前缀匹配查询(Match Phrase Prefix Query)
        • 多字段查询(Multi Match Query)
        • 常用术语查询(Common Terms Query)
        • (Query String Query) 未完成
      • 3.5.4.Term级别查询(Term level queries)
        • Term 查询
        • Terms 查询
        • Range 查询(范围查询)
        • Exists 查询(非空值查询)
        • Prefix 查询(前缀查询)
        • Wildcard 查询(通配符查询)
        • Regexp 查询(正则表达式查询)
        • Fuzzy 查询(模糊查询)
        • Type Query(类型查询)
        • Ids Query(ID 查询)
      • 3.5.5.复合查询(Compound queries)
        • Constant Score 查询
        • Bool 查询
        • Dis Max 查询
        • Function Score 查询
        • Boosting 查询
        • Indices 查询
      • 3.5.6.Joining 查询(连接查询)
        • Nested Query(嵌套查询)
        • Has Child Query
        • Has Parent Query
        • Parent Id Query
      • 3.5.7.地理位置查询 (Geo queries)
        • GeoShape Query(地理形状查询)
        • Geo Bounding Box Query(地理边框查询)
        • Geo Distance Query(地理距离查询)
        • Geo Distance Range Query(地理距离范围查询)
        • Geo Polygon Query(地理多边形查询)
      • 3.5.8.专业查询(Specialized queries)
      • 3.5.9.Span 查询
        • Span Term 查询
        • Span Multi Term 查询
        • Span First 查询
        • Span Near 查询
        • Span Or 查询
        • Span Not 查询
        • Span Containing 查询
        • Span Within 查询
        • Span Field Masking 查询
    • 3.6.Aggregations(聚合分析)
      • 3.6.1.量度聚合(Metric Aggregations)
        • 平均值聚合(Avg Aggregation)
        • 基数聚合(Cardinality Aggregation)
        • 扩展统计聚合( Extended Stats Aggregation)
        • 地理边界聚合(Geo Bounds Aggregation)
        • 地理重心聚合(Geo Centroid Aggregation)
        • 最大值聚合(Max Aggregation)
        • 最小值聚合(Min Aggregation)
        • Percentiles Aggregation
        • Percentile Ranks Aggregation
        • Scripted Metric Aggregation
        • Stats Aggregation
        • 总和聚合(Sum Aggregation)
        • Top hits Aggregation
        • Value Count Aggregation
      • 3.6.2.桶聚合(Bucket Aggregations)
        • 邻接矩阵聚合(Adjacency Matrix Aggregation)
        • Children Aggregation
        • 日期直方图聚合(Date Histogram Aggregation)
        • 日期范围聚合(Date Range Aggregation)
        • 多元化的采样器聚集(Diversified Sampler Aggregation)
        • 过滤器聚合(Filter Aggregation)
        • 多过滤器聚合(Filters Aggregation)
        • 地理距离聚合(Geo Distance Aggregation)
        • GeoHash网格聚合(GeoHash grid Aggregation)
        • 全局聚合(Global Aggregation)
        • 直方图聚合(Histogram Aggregation)
        • IP范围聚合(IP Range Aggregation)
        • 丢失字段聚合(Missing Aggregation)
        • 嵌套聚合(Nested Aggregation)
        • 范围聚合(Range Aggregation)
        • Reverse nested Aggregation
        • 采样聚合(Sampler Aggregation)
        • Significant Terms Aggregation
      • 3.6.3.管道聚合(Pipeline Aggregations)
        • 平均值桶聚合( Avg Bucket Aggregation)
        • 导数聚合(Derivative Aggregation)
        • 最大值桶聚合(Max Bucket Aggregation)
        • 最小值桶聚合(Min Bucket Aggregation)
        • 总和桶聚合(Sum Bucket Aggregation)
        • 统计桶聚合(Stats Bucket Aggregation)
        • 扩展信息桶聚合(Extended Stats Bucket Aggregation)
        • 百分数桶聚合(Percentiles Bucket Aggregation)
        • Moving Average Aggregation
        • 累积汇总聚合(Cumulative Sum Aggregation)
        • 桶脚本聚合(Bucket Script Aggregation)
        • 桶选择器聚合(Bucket Selector Aggregation)
        • 串行差异聚合(Serial Differencing Aggregation)
      • 3.6.4.矩阵聚合(Matrix Aggregations)
        • 矩阵统计(Matrix Stats)
      • 3.6.5.缓存频繁聚合(Caching heavy aggregations)
      • 3.6.6.仅返回需要聚合的结果(Returning only aggregation results)
      • 3.6.7.聚合元数据(Aggregation Metadata)
      • 3.6.8.返回聚合的类型(Returning the type of the aggregation)
    • Glossary of terms (词汇表)
    • 未完成的任务
  • 4.基础补充总结
    • 3.2.Mapping
    • 3.3.分析器与定义自己的分析器(Analyzer)
  • 原理
  • 实战
    • 结构化搜索
    • 聚合分析
    • 数据建模
    • 应用场景
  • PHP API
    • 安装与快速入门
    • 配置
    • 实战
  • 资料
  • 笔记
    • 1.x 升级 2.x
Powered by GitBook
On this page
  • 多字段查询
  • 多字段查询的类型
  • best_fields
  • most_fields
  • phrase和phrase_prefix
  • cross_fields
  • cross_field 和 analysis
  • tie_breaker

Was this helpful?

  1. 3.基础
  2. 3.5.Query DSL(DSL方式查询)
  3. 3.5.3.全文搜索(Full Text Search)

多字段查询(Multi Match Query)

多字段查询

multi_match查询基于匹配查询且允许多字段查询构建的:

GET /_search
{
  "query": {
    "multi_match" : {
      "query":    "this is a test", # 1
      "fields": [ "subject", "message" ] #2
    }
  }
}

1

查询字符串

2

要查询的字段

字段盒每个字段的重点都可以用通配符来指定,比如:

GET /_search
{
  "query": {
    "multi_match" : {
      "query":    "Will Smith",
      "fields": [ "title", "*_name" ]  # 1
    }
  }
}

1

查询title、first_name 盒 last_name字段

可以使用插入符号(^)表示法来增强单个字段

GET /_search
{
  "query": {
    "multi_match" : {
      "query" : "this is a test",
      "fields" : [ "subject^3", "message" ] (1)
    }
  }
}

1

主题字段的重要性是消息字段的三倍

多字段查询的类型

内部执行multi_match查询的方式取决于type参数,可以将其设置为:

best_fields

best_fields类型是非常有用的,当您搜索在同一字段中要找多个字词时。 例如,单个字段中的“棕狐”比一个字段中的“棕色”和另一个字段中的“狐狸”更有意义。

GET /_search
{
  "query": {
    "multi_match" : {
      "query":      "brown fox",
      "type":       "best_fields",
      "fields":     [ "subject", "message" ],
      "tie_breaker": 0.3
    }
  }
}

等价于执行:

GET /_search
{
  "query": {
    "dis_max": {
      "queries": [
        { "match": { "subject": "brown fox" }},
        { "match": { "message": "brown fox" }}
      ],
      "tie_breaker": 0.3
    }
  }

通常,best_fields类型使用单个最佳匹配字段的权重,但如果指定了tie_breaker,则计算分数如下:

  • 从最佳匹配字段得权重

  • 用于所有其他匹配字段加上tie_breaker * _score

此外,如match查询中所述,接受analyzer,boost,operator,minimum_should_match,fuzziness,lenient,prefix_length,max_expansions,rewrite,zero_terms_query和cutoff_frequency。

运算符和minimum_should_match

best_fields和most_fields类型是以字段为中心的 - 它们为每个字段生成一个匹配查询。 这意味着运算符和minimum_should_match参数分别应用于每个字段,这可能不是您想要的

以此查询为例:

GET /_search
{
  "query": {
    "multi_match" : {
      "query":      "Will Smith",
      "type":       "best_fields",
      "fields":     [ "first_name", "last_name" ],
      "operator":   "and" #1
    }
  }
}

1

所有查询条件必须存在

这个查询可以理解为:

  (+first_name:will +first_name:smith)
| (+last_name:will  +last_name:smith)

换句话说,所有术语必须存在于单个字段中以供文档匹配。

most_fields

当以不同方式查询包含相同文本的多个字段时,most_fields类型最有用。 例如,主字段可以包含同义词,词干和没有变音符号的术语。 第二字段可以包含原始术语,并且第三字段可以包含带状疱疹。 通过组合所有三个字段的权重,我们可以将尽可能多的文档与主字段匹配,但使用第二和第三字段将最相似的结果推送到列表的顶部

查询如下:

GET /_search
{
  "query": {
    "multi_match" : {
      "query":      "quick brown fox",
      "type":       "most_fields",
      "fields":     [ "title", "title.original", "title.shingles" ]
    }
  }
}

等价于执行:

GET /_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "title":          "quick brown fox" }},
        { "match": { "title.original": "quick brown fox" }},
        { "match": { "title.shingles": "quick brown fox" }}
      ]
    }
  }
}

每个匹配子句的权重分加在一起,然后除以匹配子句的数量。

此外,如match查询中所述,接受analyzer,boost,operator,minimum_should_match,fuzziness,lenient,prefix_length,max_expansions,rewrite,zero_terms_query和cutoff_frequency,但请参阅operator和minimum_should_match。

phrase和phrase_prefix

phrase和phrase_prefix类型的行为与best_fields类似,但是它们使用match_phrase或match_phrase_prefix查询,而不是匹配查询。

如下查询:

GET /_search
{
  "query": {
    "multi_match" : {
      "query":      "quick brown f",
      "type":       "phrase_prefix",
      "fields":     [ "subject", "message" ]
    }
  }
}

等价于执行:

GET /_search
{
  "query": {
    "dis_max": {
      "queries": [
        { "match_phrase_prefix": { "subject": "quick brown f" }},
        { "match_phrase_prefix": { "message": "quick brown f" }}
      ]
    }
  }
}

此外,如match查询中所述,接受analyzer,boost,operator,minimum_should_match,fuzziness,lenient,prefix_length,max_expansions,rewrite,zero_terms_query和cutoff_frequency,但请参阅operator和minimum_should_match。

重要:

phrase、phrase_prefix和fuzziness

fuzziness参数不能与phrase或phrase_prefix一起使用

cross_fields

cross_fields类型对于多个字段应匹配的结构化文档特别有用。 例如,当查询“Will Smith”的first_name和last_name字段时,最佳匹配可能在一个字段中具有“Will”,而在另一个字段中具有“Smith”

这听起来像是most_fields的工作,但这种方法有两个问题。 第一个问题是,对每个字段应用operator和minimum_should_match,而不是per-term(参见上面的解释)。

第二个问题是关于相关性:first_name和last_name字段中不同的术语频率可能会产生意外的结果。

例如,假设我们有两个人:“Will Smith”和“Smith Jones”。 “Smith”作为姓氏是非常普遍的(因此具有低重要性),但是“Smith”作为名字是非常罕见的(因此是非常重要的)。

如果我们搜索“Will Smith”,“Smith Jones”文档可能会出现在更匹配的“Will Smith”上面,因为first_name:smith的得分胜过了first_name:will加上last_name:smith的组合分数

处理这些类型的查询的一种方法是简单地将first_name和last_name字段索引到单个full_name字段中。 当然,这只能在索引时完成。

cross_field类型试图通过采用以术语为中心的方法在查询时解决这些问题。 它首先将查询字符串分析为单个术语,然后在任何字段中查找每个术语,就好像它们是一个大字段。

例如如下查询:

GET /_search
{
  "query": {
    "multi_match" : {
      "query":      "Will Smith",
      "type":       "cross_fields",
      "fields":     [ "first_name", "last_name" ],
      "operator":   "and"
    }
  }
}

执行等价与:

+(first_name:will  last_name:will)
+(first_name:smith last_name:smith)

换句话说,所有术语必须存在于至少一个字段中以供文档匹配。 (与best_fields和most_fields的逻辑进行比较。)

这解决了两个问题之一。 不同项频率的问题通过混合所有字段频率来解决,以便平衡差异。

在实践中,first_name:smith将被视为具有与last_name:smith相同的频率,加一。 这将使得first_name和last_name上的匹配具有可比的分数,对last_name具有很小的优势,因为它是包含smith的最可能的字段。

注意,cross_fields通常只对所有的boost字段都为1的短字符串字段有用。否则boosts,term freqs和length标准化以这样一种方式促成分数,使得术语统计的混合不再有意义了。

+blended("will",  fields: [first_name, last_name])
+blended("smith", fields: [first_name, last_name])

此外,如match查询中所述,接受analyzer,boost,operator,minimum_should_match,fuzziness,lenient,prefix_length,max_expansions,rewrite,zero_terms_query和cutoff_frequency,但请参阅operator和minimum_should_match。

cross_field 和 analysis

cross_field类型只能在具有相同分析器的字段上以term-centric模式工作。 具有相同分析器的字段在上面的示例中被分组在一起。 如果有多个组,它们将与bool查询结合使用。

例如,如果我们有具有相同分析器的第一和最后一个字段,加上first.edge和last.edge,它们都使用edge_ngram分析器,

查询如下:

GET /_search
{
 "query": {
    "multi_match" : {
      "query":      "Jon",
      "type":       "cross_fields",
      "fields":     [
        "first", "first.edge",
        "last",  "last.edge"
      ]
    }
  }
}

等价与执行:

    blended("jon", fields: [first, last])
| (
    blended("j",   fields: [first.edge, last.edge])
    blended("jo",  fields: [first.edge, last.edge])
    blended("jon", fields: [first.edge, last.edge])
)

换句话说,第一个和最后一个将被分组在一起并被视为单个字段,first.edge和last.edge将被分组在一起并被视为单个字段。

拥有多个组是很好的,但是当与operator或minimum_should_match相结合时,它可能会遇到与most_fields或best_fields相同的问题。

您可以轻松地将此查询重新编写为两个单独的cross_fields查询以及bool查询,并将minimum_should_match参数应用于其中一个:

GET /_search
{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match" : {
            "query":      "Will Smith",
            "type":       "cross_fields",
            "fields":     [ "first", "last" ],
            "minimum_should_match": "50%"  # 1
          }
        },
        {
          "multi_match" : {
            "query":      "Will Smith",
            "type":       "cross_fields",
            "fields":     [ "*.edge" ]
          }
        }
      ]
    }
  }
}

1

在第一个或最后一个字段中必须存在一个will或smith

您可以通过在查询中指定分析器参数将所有字段强制设置到同一组中:

GET /_search
{
  "query": {
   "multi_match" : {
      "query":      "Jon",
      "type":       "cross_fields",
      "analyzer":   "standard",  # 1
      "fields":     [ "first", "last", "*.edge" ]
    }
  }
}

1

对所有字段使用标准分析仪

等价与执行:

blended("will",  fields: [first, first.edge, last.edge, last])
blended("smith", fields: [first, first.edge, last.edge, last])

tie_breaker

默认情况下,每个词汇混合查询将使用组中任何字段返回的最佳分数,然后将这些分数加在一起以给出最终分数。 tie_breaker参数可以更改每个期间混合查询的默认行为。 它接受:

0.0 取出单个最佳分数(例如)first_name:will和last_name:will(default)

1.0 将(例如)first_name:will和last_name:will的分数加在一起

0.0 <n <1.0 取单个最佳分数加上tie_breaker乘以来自其他匹配字段的每个分数。

cross_field 和 fuzziness

fuzziness字段不能和cross_fields类型一起使用

Previous短语前缀匹配查询(Match Phrase Prefix Query)Next常用术语查询(Common Terms Query)

Last updated 5 years ago

Was this helpful?

best_fields: (默认) 查找与任何字段匹配的文档,使用最佳字段中的权重。 详情参见:

most_fields: 查找与任何字段匹配的文档,并组合每个字段的权重。详情参见:.

cross_fields: 使用相同的分析仪处理字段,就像它们是一个大字段。 在任何字段中查找每个字词,详情参见:.

phrase: 对每个字段运行match_phrase查询,并合并每个字段的权重,详情参见:.

phrase_prefix:对每个字段运行match_phrase_prefix查询,并合并每个字段的权重,详情参见:

有关更好的解决方案,请参阅

如果您通过运行上述查询,则返回以下解释:

best_fields
most_fields
cross_fields
phrase and phrase_prefix
phrase and phrase_prefix
cross_fields
Validate API