Multiple properties in facet (elasticsearch)

橙三吉。 提交于 2020-01-04 13:42:28

问题


I have following index:

curl -XPUT "http://localhost:9200/test/" -d '
{
    "mappings": {
        "files": {
            "properties": {
                "name": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "owners": {
                    "type": "nested",
                    "properties": {
                        "name": {
                            "type":"string",
                            "index":"not_analyzed"
                        },
                        "mail": {
                            "type":"string",
                            "index":"not_analyzed"
                        }
                    }
                }
            }
        }
    }
}
'

With sample documents:

curl -XPUT "http://localhost:9200/test/files/1" -d '
{
    "name": "first.jpg",
    "owners": [
        {
            "name": "John Smith",
            "mail": "js@example.com"
        },
        {
            "name": "Joe Smith",
            "mail": "joes@example.com"
        }
    ]
}
'

curl -XPUT "http://localhost:9200/test/files/2" -d '
{
    "name": "second.jpg",
    "owners": [
        {
            "name": "John Smith",
            "mail": "js@example.com"
        },
        {
            "name": "Ann Smith",
            "mail": "as@example.com"
        }
    ]
}
'

curl -XPUT "http://localhost:9200/test/files/3" -d '
{
    "name": "third.jpg",
    "owners": [
        {
            "name": "Kate Foo",
            "mail": "kf@example.com"
        }
    ]
}
'

And I need to find all owners that match some query, let's say "mit":

curl -XGET "http://localhost:9200/test/files/_search" -d '
{
    "facets": {
        "owners": {
            "terms": {
                "field": "owners.name"
            },
            "facet_filter": {
                "query": {
                    "query_string": {
                        "query": "*mit*",
                        "default_field": "owners.name"
                    }
                }
            },
            "nested": "owners"
        }
    }
}
'

This gives me following result:

{
  "facets" : {
    "owners" : {
      "missing" : 0,
      "_type" : "terms",
      "other" : 0,
      "total" : 4,
      "terms" : [
        {
          "count" : 2,
          "term" : "John Smith"
        },
        {
          "count" : 1,
          "term" : "Joe Smith"
        },
        {
          "count" : 1,
          "term" : "Ann Smith"
        }
      ]
    }
  },
  "timed_out" : false,
  "hits" : {...}
}

And it's ok. But what I exaclty need is to get owners with their email addresses (for each entry in facet I need additional field in results). Is it achievable?


回答1:


Not possible i think? Depending on your needs I would have

  1. Create a composite field with both name & email and do the facet on that field, or
  2. Run the query in addition to the facet and extract it from the query-result, but this is obviously not scalable
  3. Two step-operation, get the facet, build the needed queries and merge results.


来源:https://stackoverflow.com/questions/15277603/multiple-properties-in-facet-elasticsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!