Elasticsearch可搜索的合成字段 [英] Elasticsearch searchable synthetic fields

查看:47
本文介绍了Elasticsearch可搜索的合成字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设在源文档(JSON)中存在几个名为 a b 的字段,类型为 long 的代码,我想构造一个合成字段(例如 c )通过将下一个字段的值与下划线连接起来,将其索引为关键字.

Provided that in a source document (JSON) exist a couple of fields named, a and b, that are of type long, I would like to construct a synthetic field (e.g. c) by concatenating the values of the previous fields with an underscore and index it as keyword.

也就是说,我正在研究一种可以通过虚构的,部分的映射支持的功能,如下所示:

That is, I am looking into a feature that could be supported with an imaginary, partial, mapping like this:

...

  "a": { "type": "long" },
  "b": { "type": "long" },
  "c": {
    "type": "keyword"
    "expression": "${a}_${b}" 
  },
...

注意:上面的映射只是出于示例的目的.这是无效的!

NOTE: The mapping above was made up just for the sake of the example. It is NOT valid!

所以我要寻找的是弹性搜索中是否有功能,要支持的配方或提示这个要求.无需在 _source 中注册该字段,只需进行搜索即可.

So what I am looking for, is if there is a feature in elasticsearch, a recipe or hint to support this requirement. The field need not be registered in _source, just need to be searchable.

推荐答案

有两个步骤-

There are 2 steps to this -- a dynamic_mapping and an ingest_pipeline.

我假设您的字段 c 不平凡,因此您可能想使用

I'm assuming your field c is non-trivial so you may want to match that field in a dynamic template using a match and assign the keyword mapping to it:

PUT synthetic
{
  "mappings": {
    "dynamic_templates": [
      {
        "c_like_field": {
          "match_mapping_type": "string",
          "match":   "c*",
          "mapping": {
            "type": "keyword"
          }
        }
      }
    ],
    "properties": {
      "a": {
        "type": "long"
      },
      "b": {
        "type": "long"
      }
    }
  }
}

然后,您可以建立一个管道,该管道将连接您的 a & b :

Then you can set up a pipeline which'll concatenate your a & b:

PUT _ingest/pipeline/combined_ab
{
  "description" : "Concatenates fields a & b",
  "processors" : [
    {
      "set" : {
        "field": "c",
        "value": "{{_source.a}}_{{_source.b}}"
      }
    }
  ]
}

在提取新文档后(使用已激活的管道!)

After ingesting a new doc (with the activated pipeline!)

POST synthetic/_doc?pipeline=combined_ab
{
  "a": 531351351351,
  "b": 251531313213
}

我们很乐意:

GET synthetic/_search

收益

{
  "a":531351351351,
  "b":251531313213,
  "c":"531351351351_251531313213"
}

也通过w/ GET合成/_mapping 进行验证.

这篇关于Elasticsearch可搜索的合成字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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