简体   繁体   中英

Elasticsearch facet value for an id field

im using elasticsearch on rails with Tire.

My code:

def to_indexed_json
    {
      :id                 => id,  
      :brand_name         => brand_name,
      :brand_id           => brand_id
    }.to_json
end

facet "brands" do
  terms :brand_id
end

I have to query on database to retrieve the name of all brands returned by elasticsearch. Is there a way to return these values (brand_name) from elasticsearch?

You could use a facet based on the id field combined with a script which adds the name to each entry, so that each entry will be then composed of both id and name:

"facets" : {
    "tag" : {
        "terms" : {
            "field" : "id",
            "script" : "term + \"_\" + _source.name"
        }
    }
}

Then on the frontend you need to somehow strip the id out of the facet entry in order to show only the name (I hope your id cannot contain a _ ), but you can keep the key to apply the filter when a user selects the entry itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM