【问题标题】:ElasticSearch match multiple fields with different valuesElasticSearch 匹配具有不同值的多个字段
【发布时间】:2016-05-10 20:58:33
【问题描述】:

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

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 field match


    【解决方案1】:

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

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

    【讨论】:

    • Alpert,它与您的新结构配合得很好(双 {} 围绕匹配)。我也尝试使用 term 而不是 match 并获得相同的分数。那谢谢啦。我唯一的问题是可搜索字段(映射使用 ngrams):如果找到的术语不是预期的,如何停止结果?例如,搜索“beaulieu”将给出 2 个不错的结果和 98 个离这个词非常非常远的结果。得分从 11 上升到 2……但这取决于搜索的字词。
    • 我可以使用 min_score 参数,但它是任意模式。
    • 检查 minimum_should_match 是否适合您:elastic.co/guide/en/elasticsearch/guide/current/…
    • minimum_should_match 似乎不适用于 bool 查询中包含的“匹配”。
    猜你喜欢
    • 1970-01-01
    • 2018-03-07
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多