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

ldioqlga  于 2023-06-21  发布在  ElasticSearch
关注(0)|答案(1)|浏览(132)

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

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

这工作得很好,我的可搜索字段在我的Map中进行了分析。
现在我想对多个字段执行搜索:1个字符串,所有其他字符串都是数字。
我的Map:

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的所有数据,最后在这组结果上执行与可搜索字段的匹配...并且不使用可搜索文本获得远离现实的结果。
谢谢

e37o9pze

e37o9pze1#

Elasticsearch match查询返回基于relevance score的结果。您可以尝试使用term query而不是match来获得精确的术语匹配。我猜你的bool查询结构应该是这样的:

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

相关问题