1

我目前正在通过我的 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 中执行此操作。请提供帮助或好的链接?

4

2 回答 2

1

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

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

让我向您展示如何使用 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],因为它没有被索引。” }

于 2020-03-21T03:30:23.460 回答
1

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

注意:您传递的type是 ES 7.X 中已弃用的,我使用的是 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这是可搜索的字段,并且没有此选项。
  • index mapping使用此官方 ES 文档中提供的 Map 方法创建。
  • 您可以使用映射 REST API检查此代码创建的映射。
  • 下面是在我的系统中为此代码生成的映射,您可以看到,index: false添加到地址字段中。
{
  "employee": {
    "mappings": {
      "properties": {
        "address": {
          "type": "text",
          "index": false
        },
        "name": {
          "type": "text"
        }
      }
    }
  }
}
  • 您可以使用上一个答案中提到的相同搜索 JSON 来测试它是否不可搜索。
于 2020-03-21T04:43:21.183 回答