Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch additional facet data

I have configured my Elastic Search implementation to facet the results by an id in the mapping and when I display that facet to the user I need to be able to show the human-readable name that represents it. The data I need is all present in the mapping, but I am not sure how I can return it as part of the facet. Surely it is possible?

Given the following example, I'd like to the facet to give me some way to correlate the thingId to thingName (or any other thing property that might be needed):

Mapping

{
  thingId,
  thingName
}

Facet Query

{
  "facets":{
    "things":{ "terms":{ "field":"thingId" } }
  }
}    

Result

{
  "hits":{
    "total":3,
    "max_score":1.0,
    "hits":[
      ...
    ]
  },
  "facets":{
    "things":{
      "_type":"terms",
      "missing":0,
      "total":3,
      "other":0,
      "terms":[
        {
          "term":"5",
          "count":1
        },
        {
          "term":"4",
          "count":1
        },
        {
          "term":"2",
          "count":1
        }
      ]
    }
  }
}

Edit

This answer regarding Solr suggests that I facet both properties (thingName and thingId) and then just loop over both facet result sets, assuming that the order of items would be the same. I don't know how reliable that would be, but it is an option.

Edit 2

This answer suggests that it's not possible to do what I want without combining the the two fields into a single value and faceting on that: thingId|thingName. Not ideal.

Edit 3

This answer suggests combining the values together into a single field and faceting on it (as above), but it uses a terms script to achieve the combination, thus not requiring me to index the combined form of the values. Still not perfect, but appears to be the least crappy option.

like image 477
Nathan Taylor Avatar asked Aug 30 '13 21:08

Nathan Taylor


People also ask

What are facets in elastic search?

Facets - A visual example of facets from the eyes of the Search UI. Facets are contained on the left side, next to the results. The example link is faceting on two different fields: world_heritage_site and states . A query like "old growth" will return many different parks.

What is faceted search example?

Here is an example of a faceted filter: A user is looking for an iPhone on eBay. What eBay offers is a logical filter with different facet types. The visitor can choose which storage capacity they want, which iPhone model and which color they prefer.

What is facet query?

Facets, also called smart filters, are a type of search filter that are used to help customers narrow down their search results quickly. Unlike static filters that are the same for every search, facets are dynamic — they can change depending on the context of the search query.

What is elastic search?

Elasticsearch is a distributed search and analytics engine built on Apache Lucene. Since its release in 2010, Elasticsearch has quickly become the most popular search engine and is commonly used for log analytics, full-text search, security intelligence, business analytics, and operational intelligence use cases.


1 Answers

If you're not happy using the terms scripting, then another options would be to use nested aggregations, assuming you're able to use 1.0.0.

Your aggregation would then look something like this:

{
    "query": {
        "match_all": {}
    },
    "aggs": {
        "theIds": {
            "terms" : {
                "field": "thingId"
            },
            "aggs":{
                "theNames": {
                    "terms": {
                        "field": "thingName"
                    }
                }
            }
        }
    }
}

And the response would be something like:

"aggregations": {
      "theIds": {
         "buckets": [
            {
               "key": "1",
               "doc_count": 5,
               "theNames": {
                  "buckets": [
                     {
                        "key": "AAA",
                        "doc_count": 3
                     },
                     {
                        "key": "BBB",
                        "doc_count": 3
                     },
                     {
                        "key": "CCC",
                        "doc_count": 2
                     }
                  ]
               }
            },
            {
               "key": "2",
               "doc_count": 2,
               "theNames": {
                  "buckets": [
                     {
                        "key": "AAA",
                        "doc_count": 2
                     }
                  ]
               }
            }
         ]
      }
   }
like image 53
Akshay Avatar answered Nov 13 '22 16:11

Akshay