4

The object that I am indexing has both a UserId (a GUID) and a FullName property. I would like to do a faceted search using the UserId but display the FullName so that it's readable. I don't want to do the facet on the FullName since it technically doesn't have to be unique.

Right now I'm doing something like this:

{
    "query": {
        "match_all": {}
    },
    "facets": {
        "userFacet": {
            "terms": {
                "field": "userId"
            }
        }
    }
}

But then it is giving me the Guids in the response which I would need to hit the database to lookup the full name which is obviously not a real solution.

So how can I use one field to do the facets with and then a different field for the display values to use?

Jeff Treuting
  • 13,910
  • 8
  • 36
  • 47
  • Could you please add the results you are looking for? I don't quite understand your use. do you want to return the guid and the name as an array of objects? – J.T. Oct 09 '13 at 02:55
  • I would like to use the GUID to do the faceting but I would like to also return the name that is associated with that user so I can display that as the label for the facet on the webpage. Right now if I only get the GUID back from ElasticSearch, I will need to then lookup the name based on the GUID from the database which is not efficient and since ES has the data in the source object it seems like there might be a way to get this done without the extra DB queries. – Jeff Treuting Oct 09 '13 at 03:42
  • Yes, if you would add user name and guid as a nested document and run a nested facet, you will get both back. When I am not on my phone tomorrow I can supply the answer if you need. Terms facet is just for simple values. – J.T. Oct 09 '13 at 03:50
  • Oh okay I didn't know about nested facets. I will look at the documentation and see if I can figure it out. If you have an example that would of course be appreciated. Thanks. – Jeff Treuting Oct 09 '13 at 04:11

2 Answers2

0

i am using the below line for displaying the fields. It is working for grids only but not facets. Still facet is taking from the id only not label.

fields: [{
    id: 'name_first', 
    'label': 'First Name'
}]
bigmike7801
  • 3,908
  • 9
  • 49
  • 77
subbu
  • 1
-1

Try adding a fields clause to your query, as follows:

{
    "query": {
        "match_all": {}
    },
    "facets": {
        "userFacet": {
            "terms": {
                "field": "userId"
            }
        }
    },
    "fields": [ "FullName" ]
}

Note that in order to return field values in search results, you need to actually store them in ES. So you need to either store the _source (this happens by default, unless you override it with "_source" : {"enabled" : false}, or set "store": "yes" for that field in your mapping.

Chris B
  • 9,149
  • 4
  • 32
  • 38
  • When I do that, in the hits array returned it doesn't have the _source anymore and is replaced a fields object which contains only the properties that are included in that fields array of the request. So this seems to be affecting the hits array and not the facets at all. The facets are just showing the userID GUID as the term. – Jeff Treuting Oct 08 '13 at 15:31
  • Sorry, I thought that was exactly what you were trying to do. Looks like I misunderstood your question. – Chris B Oct 09 '13 at 08:15