【问题标题】:Multiple properties in facet (elasticsearch)方面的多个属性(弹性搜索)
【发布时间】:2013-03-07 17:18:50
【问题描述】:

我有以下索引:

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"
                        }
                    }
                }
            }
        }
    }
}
'

附样本文件:

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"
        }
    ]
}
'

我需要找到与某个查询匹配的所有所有者,比如说“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"
        }
    }
}
'

这给了我以下结果:

{
  "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" : {...}
}

没关系。 但我确切需要的是让所有者拥有他们的电子邮件地址(对于构面中的每个条目,我需要在结果中添加额外的字段)。 可以实现吗?

【问题讨论】:

    标签: elasticsearch faceted-search


    【解决方案1】:

    我认为不可能?根据您的需要,我会拥有

    1. 创建一个包含姓名和电子邮件的复合字段,并在该字段上执行构面,或者
    2. 除了 facet 之外运行查询并从查询结果中提取它,但这显然不可扩展
    3. 两步操作,获取 facet,构建所需的查询并合并结果。

    【讨论】:

    • 感谢您的提示。我已经尝试过复合字段,我认为这是目前最好的方法,因为我想避免过多的查询和处理。干杯!
    猜你喜欢
    • 2016-03-28
    • 2017-04-24
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 2012-12-05
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多