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

ale*_*our 3 field match elasticsearch

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

query: {match: {searchable: {query:search}}}
Run Code Online (Sandbox Code Playgroud)

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

现在我想对多个字段执行搜索: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 
Run Code Online (Sandbox Code Playgroud)

我试过这个:

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
Run Code Online (Sandbox Code Playgroud)

它运行良好,但在所有情况下都会给我 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
            )
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢。

alp*_*ert 5

您可能知道,elasticsearchmatch查询会根据相关性分数返回结果。您可以尝试使用term查询而不是匹配来进行精确的术语匹配。另外我猜你的 bool 查询结构必须是这样的:

bool: {
      must: [
          { match: {
                country_id: "1"
              }},
          {match: {
            region_id: "2"
          }},
          {match: {
            appellation_id: "34"
          }},
          {match: {
            searchable: search
          }}
      ]
    }
Run Code Online (Sandbox Code Playgroud)