【问题标题】:ElasticSearch Make Field non-searchable from javaElasticSearch 使字段不可从 java 中搜索
【发布时间】:2020-03-20 16:49:18
【问题描述】:

我目前正在通过我的 java Application 进行弹性搜索。我知道如何使用 RestHighLevelClient 索引 Java pojo。我怎样才能只在新领域而不是完整的 pojo 上进行搜索?

    public class Employee{ 
        private long id;
        private String name;
        private String designation;
        private String address;       //want to index but not searchable in elastic search    
     }

我的索引代码如下,运行良好:

 public String saveToEs(Employee employee) throws IOException {
    Map<String, Object> map = objectMapper.convertValue(employee, Map.class);

    IndexRequest indexRequest =
        new IndexRequest(INDEX, TYPE, employee.getId().toString()).source(map, XContentType.JSON);


    IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);

我需要在 java 中执行此操作。任何帮助或好的链接?

【问题讨论】:

    标签: java elasticsearch resthighlevelclient


    【解决方案1】:

    RestHighLevelClient 写另一个答案因为另一个答案对于不使用 Rest 客户端的人很有用,并且在第一个答案中添加它会使其太长。

    注意:您传递的是 ES 7.X 中已弃用的 type,而我使用的是 ES 7.X 版本,所以我的代码是根据 7.X 编写的。

            CreateIndexRequest request = new CreateIndexRequest("employee");
            Map<String, Object> name = new HashMap<>();
            name.put("type", "text");
            Map<String, Object> address = new HashMap<>();
            address.put("type", "text");
            address.put("index", false);
    
            Map<String, Object> properties = new HashMap<>();
            properties.put("name", name);
            properties.put("address", address);
            Map<String, Object> mapping = new HashMap<>();
            mapping.put("properties", properties);
            request.mapping(mapping);
            CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
    

    要点

    • 出于说明目的,我只使用了 2 个字段,其中一个是不可搜索的 address 字段,为此我使用了 address.put("index", false); ,而 name 是可搜索字段,并且此选项不可用不存在。
    • 我使用 official ES doc 中提供的 Map 方法创建了 index mapping
    • 您可以使用mapping REST API检查此代码创建的映射。
    • 下面是我系统为这段代码生成的映射,你可以看到,index: false被添加到了地址字段中。
    {
      "employee": {
        "mappings": {
          "properties": {
            "address": {
              "type": "text",
              "index": false
            },
            "name": {
              "type": "text"
            }
          }
        }
      }
    }
    
    • 您可以使用上一个答案中提到的相同搜索 JSON 来测试它是否不可搜索。

    【讨论】:

      【解决方案2】:

      在地址字段中使用 index option 作为 false,默认情况下为 true 以使其不可搜索。正如同一个官方 ES 链接中提到的:

      索引选项控制字段值是否被索引。它接受 真或假,默认为真。未编入索引的字段是 不可查询。

      让我向您展示如何使用 REST API 测试它,然后使用 java 代码(使用 REST 高级客户端)来完成它。

      映射

      {
          "mappings": {
              "properties": {
                  "id": {
                      "type": "long"
                  },
                  "name": {
                      "type": "text"
                  },
                  "designation": {
                      "type": "text"
                  },
                  "address": {
                      "type": "text",
                      "index" : false --> made `index` to false
                  }
              }
          }
      }
      

      索引几个文档

      {
          "address" : "USA",
          "name"  : "Noshaf",
          "id" :  234567892,
          "designation" : "software engineer"
      }
      
      {
          "address" : "USA california state",
          "name"  : "opster",
          "id" :  234567890,
          "designation" : "software engineer"
      }
      

      address 字段上 JSON 格式的简单匹配搜索查询

      {
          "query": {
              "match" : {
                  "address" : "USA"
              }
          }
      }
      

      Elasticsearch 中明确提到的异常,不可搜索

      "caused_by": { "type": "illegal_argument_exception", "reason": "无法搜索字段 [address],因为它未编入索引。" }

      【讨论】:

        猜你喜欢
        • 2017-05-25
        • 2020-10-05
        • 1970-01-01
        • 1970-01-01
        • 2021-03-10
        • 1970-01-01
        • 2020-09-12
        • 2013-09-12
        • 2018-12-28
        相关资源
        最近更新 更多