elasticsearch ElasticList只需要返回一种语言的文本

fkaflof6  于 5个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(38)

Map:

PUT /test
{
    "mappings": {
        "properties": {
            "product_id": {
                "type": "integer"
            },
            "product_name": {
                "type": "nested",
                "include_in_parent": true,
                "properties": {
                    "lang_id": {
                        "type": "byte"
                    },
                    "name": {
                        "type": "text"
                    }
                }
            }
        }
    }
}

字符串
插入一些数据:

POST /test/_doc
{
    "product_id": 111,
    "product_name": [
        {
            "lang_id": 1,
            "name": "one"
        },
        {
            "lang_id": 2,
            "name": "ein"
        }
    ]
}

POST /test/_doc
{
    "product_id": 222,
    "product_name": [
        {
            "lang_id": 1,
            "name": "two"
        },
        {
            "lang_id": 2,
            "name": "zwei"
        }
    ]
}


我的查询返回lang_id = 2,name = 'ein'文档:

POST /test/_search
{
    "query": {
        "nested": {
            "path": "product_name",
            "query": {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "product_name.name": "ein"
                            }
                        },
                        {
                            "term": {
                                "product_name.lang_id": 2
                            }
                        }
                    ]
                }
            }
        }
    },
    "_source": [
        "product_id",
        "product_name"
    ]
}


其结果是:

...
                "_source": {
                    "product_id": 111,
                    "product_name": [
                        {
                            "lang_id": 1,
                            "name": "one"
                        },
                        {
                            "lang_id": 2,
                            "name": "ein"
                        }
                    ]
                }
...


Visibel at it返回所有product_names,但我只需要lang_id =2(ein)的名称
我的解决方案是:

POST /test/_search
{
    "query": {
        "nested": {
            "path": "product_name",
            "query": {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "product_name.name": "ein"
                            }
                        },
                        {
                            "term": {
                                "product_name.lang_id": 2
                            }
                        }
                    ]
                }
            }
        }
    },
    "_source": [
        "product_id"
    ],
    "script_fields": {
        "selected_name": {
            "script": {
                "source": "def name_langid = 0;for (int i = 0; i < params._source.product_name.length; i++) {if (params._source.product_name[i].lang_id == params.langid) {name_langid = params._source.product_name[i].name;}}name_langid;",
                "params": {
                    "langid": 2
                }
            }
        }
    }
}


返回预期结果:

...
                "_source": {
                    "product_id": 111
                },
                "fields": {
                    "selected_name": [
                        "ein"
                    ]
                }
...


但我认为这不是最好的解决方案,因为脚本可能会减慢查询速度。
有没有更简单的方法来达到同样的效果?

ltqd579y

ltqd579y1#

filter_path可以帮助您。

所有REST API都接受一个filter_path参数,该参数可用于减少Elasticsearch返回的响应。该参数采用逗号分隔的过滤器列表,用点表示法表示:https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#common-options-response-filtering

POST /test/_search?filter_path=**.product_name
{
  "query": {
    "nested": {
      "path": "product_name",
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "product_name.name": "ein"
              }
            },
            {
              "term": {
                "product_name.lang_id": 2
              }
            }
          ]
        }
      }
    }
  },
  "_source": [
    "product_id",
    "product_name"
  ]
}

字符串


的数据

相关问题