0

我想将monthbibtex 条目的字段索引到 elasticsearch 并使其可通过range查询进行搜索。这要求基础字段类型是某种数字数据类型。在我的情况下short就足够了。

规范形式的 bibtexmonth字段需要三个字符的缩写,所以我尝试使用char_filter类似的:

...
"char_filter": {
    "month_char_filter": {
        "type": "mapping",
        "mappings": [
            "jan => 1",
            "feb => 2",
            "mar => 3",
            ...
            "nov => 11",
            "dec => 12"
        ]
    }
...
"normalizer": {
    "month_normalizer": {
        "type": "custom",
        "char_filter": [ "month_char_filter" ],
    },

并建立这样的映射:

...
"month": {
    "type": "short",
    "normalizer": "month_normalizer"
},
...

但它似乎不起作用,因为该type字段不支持这样的规范化器,也不支持分析器。

那么实现如char_filter部分所示的这样一个映射的方法是什么,以便有范围查询可能性?

4

1 回答 1

3

您的方法直观地说是有道理的,但是,规范化器只能应用于keyword字段,而分析器只能应用于text字段。

另一种方法是利用摄取处理器并使用script处理器在索引时进行映射。

您可以在下面找到此类处理器的模拟,该script处理器将根据字段monthNum中存在的月份创建一个名为的新month字段。

POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "script": {
          "source": """
          def mapping = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'];
          ctx.monthNum = mapping.indexOf(ctx.month) + 1;
          """
        }
      }
    ]
  },
  "docs": [
    {
      "_source": {
        "month": "feb"
      }
    },
    {
      "_source": {
        "month": "mar"
      }
    },
    {
      "_source": {
        "month": "jul"
      }
    },
    {
      "_source": {
        "month": "aug"
      }
    },
    {
      "_source": {
        "month": "nov"
      }
    },
    {
      "_source": {
        "month": "dec"
      }
    },
    {
      "_source": {
        "month": "xyz"
      }
    }
  ]
}

结果文件:

{
  "docs" : [
    {
      "doc" : {
        "_index" : "_index",
        "_type" : "_type",
        "_id" : "_id",
        "_source" : {
          "monthNum" : 2,
          "month" : "feb"
        },
        "_ingest" : {
          "timestamp" : "2019-05-08T12:28:27.006Z"
        }
      }
    },
    {
      "doc" : {
        "_index" : "_index",
        "_type" : "_type",
        "_id" : "_id",
        "_source" : {
          "monthNum" : 3,
          "month" : "mar"
        },
        "_ingest" : {
          "timestamp" : "2019-05-08T12:28:27.006Z"
        }
      }
    },
    {
      "doc" : {
        "_index" : "_index",
        "_type" : "_type",
        "_id" : "_id",
        "_source" : {
          "monthNum" : 7,
          "month" : "jul"
        },
        "_ingest" : {
          "timestamp" : "2019-05-08T12:28:27.006Z"
        }
      }
    },
    {
      "doc" : {
        "_index" : "_index",
        "_type" : "_type",
        "_id" : "_id",
        "_source" : {
          "monthNum" : 8,
          "month" : "aug"
        },
        "_ingest" : {
          "timestamp" : "2019-05-08T12:28:27.006Z"
        }
      }
    },
    {
      "doc" : {
        "_index" : "_index",
        "_type" : "_type",
        "_id" : "_id",
        "_source" : {
          "monthNum" : 11,
          "month" : "nov"
        },
        "_ingest" : {
          "timestamp" : "2019-05-08T12:28:27.006Z"
        }
      }
    },
    {
      "doc" : {
        "_index" : "_index",
        "_type" : "_type",
        "_id" : "_id",
        "_source" : {
          "monthNum" : 12,
          "month" : "dec"
        },
        "_ingest" : {
          "timestamp" : "2019-05-08T12:28:27.006Z"
        }
      }
    },
    {
      "doc" : {
        "_index" : "_index",
        "_type" : "_type",
        "_id" : "_id",
        "_source" : {
          "monthNum" : 0,
          "month" : "xyz"
        },
        "_ingest" : {
          "timestamp" : "2019-05-08T12:28:27.006Z"
        }
      }
    }
  ]
}
于 2019-05-08T12:29:53.070 回答