多面性(弹性搜索) [英] Multiple properties in facet (elasticsearch)

查看:82
本文介绍了多面性(弹性搜索)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  curl -XPUThttp:// localhost:9200 / test /-d' 
{
mappings:{
files:{
properties:{
name:{
type字符串,
index:not_analyzed
},
owner:{
type:nested,
properties
name:{
type:string,
index:not_analyzed
},
mail:{
type:string,
index:not_analyzed
}
}
}
}
}
}
}
'

带有示例文档:

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

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

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

我需要找到所有匹配一些查询的所有者,让我们说mit:

  curl -XGEThttp:// localhost:9200 / test / files / _search-d'
{
facet:{
owner:{
terms:{
field:owners.name
},
facet_filter {
query:{
query_string:{
query:* mit *,
default_field:owners.name

}
},
嵌套:所有者
}
}
}
'

这给了我以下结果:

  {
facets:{
owner:{
missing:0,
_type:terms,
other :0,
total:4,
条款:[
{
co $ 2
term:John Smith
},
{
count:1,
term:Joe Smith
},
{
count:1,
term:Ann Smith
}
]
}
},
timed_out:false,
hits:{...}
}

没关系
但是我需要的是让所有者拥有他们的电子邮件地址(对于每个条目,我需要结果中的其他字段)。
是否可以实现?

解决方案

不可能我想?根据你的需要,我会有


  1. 创建一个复合字段,名称和电子邮件,并在该领域执行方面,或者

  2. 除了构面外还运行查询,并从查询结果中提取查询,但这显然不可缩放

  3. 两步操作,获取方面,构建所需的查询并合并结果。


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?

解决方案

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.

这篇关于多面性(弹性搜索)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆