方面麻烦与弹性搜索查询 [英] Facet Troubles with Elasticsearch on Query

查看:102
本文介绍了方面麻烦与弹性搜索查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我添加一个术语到我的查询而不是一个过滤器,我得到0个面。 FYI我正在使用Ruby的轮胎宝石。



这是我的模型代码及其映射:

  class Property< ActiveRecord :: Base 
包括Tire :: Model :: Search
include Tire :: Model :: Callbacks

has_and_belongs_to_many:tags

mapping do
索引:id,type:'integer'
索引:状态
索引:refno,类型:'整数'
索引:名称,分析器=> 'snowball',:boost => 100
索引:描述
索引:标签,类型:'对象',
属性:{
名称:{type:'multi_field',
fields:{
name:{type:'string',analyzer:'snowball'},
exact:{type:'string',index:'not_analyzed'}
}
}

end

def to_indexed_json
to_json(include:{
tags:{only:[:name]},
})
end

然后这里是搜索方法

  def self.search(params = {})
tire.search(页面:params [:page],per_page:2,load:true)do
查询do
boole一个do
必须{string params [:name],default_operator:AND}如果params [:name] .present?
必须{term:status,'live'}
must {term:refno,params [:refno]}如果params [:refno] .present?
#must {term:tag,params [:tag]}如果params [:tag] .present? ##不工作
must {term'tags.name.exact',params [:tag]}如果params [:tag] .present?
end
end
facettagsdo
terms'tags.name.exact'
end
raise to_json
#raise to_curl
end
end

我得到0 Facets。

  def self.search (params = {})
tire.search(页面:params [:page],per_page:2,load:true)do
query do
boolean do
must {string params [:name],default_operator:AND}如果params [:name] .present?
必须{term:status,'live'}
must {term:refno,params [:refno]}如果params [:refno] .present?
end
end
过滤器:term,'tags.name.exact'=> params [:tag] if params [:tag] .present?
facettagsdo
terms'tags.name.exact'
end
raise to_json
#raise to_curl
end
end

虽然没关系,但不要,当点击一个facet过滤器时,我想从我的facet过滤器并更新新的面数。



如果这里有帮助,查询的json是有效的,没有。

  ## No Factes 
{
query:{
bool:{
must [
{
query_string:{
query:England,
default_operator:AND
}
},
{
term:{
status:live
}
},
{
term:{
tags.name.exact:[
宠物友好
]
}
}
]
}
},
facets:{
tags:{
条款:{
field:tags.name.exact,
size:10,
all_terms:false
}
}
},
size:2
}

## facets working
{
query:{
bool:{
must:[
{
query_string:{
query:England,
default_operator
}
},
{
term:{
status:live
}
}
]
}
},
facets:{
tags:{
terms:{
field:tags。 name.exact,
size:10,
all_terms:false
}
}
},
filter:{
term:{
tags.name.exact:[
Pet Friendly
]
}
} ,
size:2
}

真的希望有人可以建议。开始拉我的头发在这一个。

解决方案

我其实很接近。因为我的标签可以有多个,我需要使用不是术语的术语,即

  def self.search(params = {})
tire.search(页面:params [:page],per_page:2,load:true)do
query do
boolean do
must {string params [:name],default_operator :AND}如果
必须{term:status,'live'}
must {term:refno,params [:refno]}如果params [:refno] .present?
必须{terms'tags.name.exact',params [:tag]}如果params [:tag] .present?
end
end
facettagsdo
terms'tags.name.exact'
end
#raise to_json
#raise to_curl
end
end

感谢您的建议,虽然imotov,Hoang。 / p>

When adding a term to my query instead of a filter I am getting 0 facets. FYI I am using the tire gem with Ruby.

Here is my model code with its mapping:

class Property < ActiveRecord::Base
  include Tire::Model::Search
  include Tire::Model::Callbacks

  has_and_belongs_to_many :tags

   mapping do
    indexes :id,                type: 'integer'
    indexes :status
    indexes :refno,             type: 'integer'
    indexes :name,              :analyzer => 'snowball', :boost => 100
    indexes :description
    indexes :tags,              type: 'object',
                                  properties: {
                                    name: { type: 'multi_field',
                                      fields: {
                                        name: { type: 'string', analyzer: 'snowball' },
                                        exact: { type: 'string', index: 'not_analyzed' }
                                      }
                                    }
                                   }                                 
  end

  def to_indexed_json
    to_json( include: { 
      tags: { only: [:name] },
     })
  end

Then here is the search method

  def self.search(params={})
    tire.search(page: params[:page], per_page: 2, load: true) do
      query do
        boolean do
         must { string params[:name], default_operator: "AND" } if params[:name].present?
         must { term :status, 'live' }
         must { term :refno, params[:refno]} if params[:refno].present?
         # must { term :tag, params[:tag]} if params[:tag].present? ## does not work either
         must { term 'tags.name.exact', params[:tag]} if params[:tag].present?
        end
      end
      facet "tags" do
        terms 'tags.name.exact'
      end
      raise to_json
      # raise to_curl
    end
  end

I get 0 Facets. But if I move facets to a filter ie below I get full facets.

  def self.search(params={})
    tire.search(page: params[:page], per_page: 2, load: true) do
      query do
        boolean do
         must { string params[:name], default_operator: "AND" } if params[:name].present?
         must { term :status, 'live' }
         must { term :refno, params[:refno]} if params[:refno].present?
        end
      end
      filter :term, 'tags.name.exact' => params[:tag] if params[:tag].present?
      facet "tags" do
        terms 'tags.name.exact'
      end
      raise to_json
      # raise to_curl
    end
  end

While this is ok it's not want, When a facet filter is clicked I want to remove non available tags from my facet filter and update the new facet count.

If it helps here is the json for the query which works and does not.

## No Factes
{
   "query":{
      "bool":{
         "must":[
            {
               "query_string":{
                  "query":"England",
                  "default_operator":"AND"
               }
            },
            {
               "term":{
                  "status":"live"
               }
            },
            {
               "term":{
                  "tags.name.exact":[
                     "Pet Friendly"
                  ]
               }
            }
         ]
      }
   },
   "facets":{
      "tags":{
         "terms":{
            "field":"tags.name.exact",
            "size":10,
            "all_terms":false
         }
      }
   },
   "size":2
}

## Facets working
{
   "query":{
      "bool":{
         "must":[
            {
               "query_string":{
                  "query":"England",
                  "default_operator":"AND"
               }
            },
            {
               "term":{
                  "status":"live"
               }
            }
         ]
      }
   },
   "facets":{
      "tags":{
         "terms":{
            "field":"tags.name.exact",
            "size":10,
            "all_terms":false
         }
      }
   },
   "filter":{
      "term":{
         "tags.name.exact":[
            "Pet Friendly"
         ]
      }
   },
   "size":2
}

Really hope someone can advise. Starting to pull my hair out on this one.

解决方案

I was actually very close. As my tags can have multiple I needed to use terms not term ie,

  def self.search(params={})
    tire.search(page: params[:page], per_page: 2, load: true) do
      query do
        boolean do
         must { string params[:name], default_operator: "AND" } if 
         must { term :status, 'live' }
         must { term :refno, params[:refno]} if params[:refno].present?
         must { terms 'tags.name.exact', params[:tag]} if params[:tag].present?
        end
      end
      facet "tags" do
        terms 'tags.name.exact'
      end
      # raise to_json
      # raise to_curl
    end
  end

Thank you for your advise though imotov, Hoang.

这篇关于方面麻烦与弹性搜索查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆