首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >查询子属性

查询子属性
EN

Stack Overflow用户
提问于 2019-09-13 06:17:00
回答 1查看 67关注 0票数 0

我想复制我的索引,但是当一个属性与一个特定值匹配时跳过它。我知道了如何一起排除一个属性,但我需要这样的东西:

exclude 'terms property' WHERE 'source_terminology subproperty' not like '%earthMaterials%

这在ElasticSearch中是可能的,还是我应该用一种不同的方式呢?

代码语言:javascript
运行
复制
POST _reindex
{
  "source" : {
    "index" : "documents3",
    "_source":{
      "excludes": [
        "terms"   
      ]
    }
  },
  "dest" : {
    "index" : "documents4"
  }
} 

这是我的映射的简化版本:

代码语言:javascript
运行
复制
{
  "documents4": {
    "mappings": {
      "doc": {
        "properties": {
          "abstract": {
            "type": "text"
          },
          "author": {
            "type": "text",
            "fields": {
              "raw": {
                "type": "keyword"
              }
            }
          },
          "terms": {
            "properties": {
              "source_terminology": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                  }
                }
              },
              "uri": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

下面是我的数据现在的样子:

代码语言:javascript
运行
复制
      {
        "_index": "documents4",
        "_type": "doc",
        "_id": "6bf03d1e-f7dc-40c6-a32d-c9aa09e7b051",
        "_score": 1,
        "_source": {
          "terms": [
            {
              "source_terminology": "exploration-activity-type",
              "label": "feasibility study",
              "uri": "http://resource.geosciml.org/classifier/cgi/exploration-activity-type/feasibility-study"
            },
                        {
              "source_terminology": "earthMaterialsAT",
              "label": "rock",
              "uri": "http://www.similarto.com/ontologies/lithology/2010/12/earthMaterialsAT#rock"
            },
            "title": "Miguel Auza Initial Prospectus"
        }
      }
EN

回答 1

Stack Overflow用户

发布于 2019-09-13 13:31:37

您可以使用无痛脚本来添加所需的条件。

代码语言:javascript
运行
复制
POST _reindex
{
  "source" : {
    "index" : "documents4"
  },
  "dest" : {
    "index" : "documents4-copy3"
  },
  "script": {
    "source": "int index = 0; def list = new ArrayList(); for(term in ctx._source.terms) { if(term.source_terminology =~ /^(?:(?!exploration).)+$/) { list.add(0, index) } index++;} for(item in list) { ctx._source.terms.remove(item)}",
    "lang": "painless"
  }
} 

您需要在elasticsearch.yml文件中将script.painless.regex.enabled值设置为true才能执行此操作。

无痛脚本的格式化版本

代码语言:javascript
运行
复制
int index = 0;
def list = new ArrayList();
for (term in ctx._source.terms) {
  if (term.source_terminology = ~ /^(?:(?!earthMaterials).)+$/) {
    // Need to add matched index at start to avoid
    // index_out_of_bounds_exception when removing items later
    list.add(0, index)
    // If you try to remove item as soon as match is found,
    // you will get concurrent_modification_exception
  }
  index++;
}
for (item in list) {
  ctx._source.terms.remove(item)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57915132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档