ElasticSearch 匹配具有不同值的多个字段

标签 elasticsearch field match

我实际上可以用这个执行简单的匹配搜索:

query: {match: {searchable: {query:search}}}

这很有效,我的可搜索字段在我的映射中进行了分析。

现在我想对多个字段进行搜索:1个字符串,其他都是数字。

我的映射:

mappings dynamic: 'false' do
        indexes :searchable, analyzer: "custom_index_analyzer", search_analyzer: "custom_search_analyzer"
        indexes :year, type: "integer"
        indexes :country_id, type: "integer"
        indexes :region_id, type: "integer"
        indexes :appellation_id, type: "integer"
        indexes :category_id, type: "integer"
      end

def as_indexed_json(options={}) 
      as_json(
        only: [:searchable, :year, :country_id, :region_id, :appellation_id, :category_id]
      ) 
    end 

我试过这个:

query: {
                    filtered: {
                      query: {
                        match: {
                          searchable: search
                        }
                      },
                      filter: {
                        term: {
                          country_id: "1"
                        },
                        term: {
                          region_id: "2"
                        },
                        term: {
                          appellation_id: "34"
                        }
                      }
                    }
                  },
                  sort: {
                    _score: {
                      order: :desc
                    }, 
                    year: {
                      order: :desc, 
                      ignore_unmapped: true
                    }
                  },
                  size:100

它运行良好,但它会在所有情况下为我提供 100 个结果,来自发送的 appellation_id (34),即使可搜索字段与文本搜索相距甚远。

我也试过 BOOL 查询:

  self.search(query: {
                bool: {
                  must: [
                    {
                      match: {
                            country_id: "1"
                          },
                          match: {
                            region_id: "2"
                          },
                          match: {
                            appellation_id: "34"
                          },
                          match: {
                            searchable: search
                          }
                    }
                  ]
                }
              },
              sort: {
                _score: {
                  order: :desc
                }, 
                year: {
                  order: :desc, 
                  ignore_unmapped: true
                }
              },
              size:100
            )

但它会给我所有匹配可搜索字段的结果,并且不会处理想要的 appellation_id。

我的目标是获得最好的结果和性能,并要求 ES 给我来自 country_id=X、region_id=Y、appellation_id=Z 的所有数据,最后对这组结果与可搜索字段进行匹配...并且不要使用可搜索的文本获得与现实相去甚远的结果。

谢谢。

最佳答案

您可能知道 elasticsearch match 查询返回基于 relevance score 的结果.您可以尝试使用 term 查询而不是 match 来进行精确的术语匹配。另外我猜你的 bool 查询结构一定是这样的:

bool: {
      must: [
          { match: {
                country_id: "1"
              }},
          {match: {
            region_id: "2"
          }},
          {match: {
            appellation_id: "34"
          }},
          {match: {
            searchable: search
          }}
      ]
    }

关于ElasticSearch 匹配具有不同值的多个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37138840/

相关文章:

java - 在 Java 中自动填充字段 WebView

JavaScript:使用 .exec() 查找连续匹配项

MySQL Match() 没有找到最佳匹配

elasticsearch - Elasticsearch嵌套过滤器包含与不包含

elasticsearch - 在elasticsearch中,如何查询日期字段为2020年1月1日太平洋时间的任何时间的所有文档

ElasticSearch bool should_not 过滤器

excel - 索引和匹配矩阵公式

elasticsearch - “aggs”查询的输出结果不正确

Mysql 通过 append 同一行中的另一个字段文本来更新字段文本

c# - 为什么希望在 C# 中使用所有属性而不是公共(public)实例变量?