java - 无法在elasticsearch 6.1.2的映射中插入 "index":"not_analyzed"

标签 java elasticsearch case-sensitive case-insensitive elasticsearch-6

我正在尝试查找区分大小写的匹配名称。当我尝试下面的代码时,它按预期工作,不区分大小写。

        HashMap<String, String> data = new HashMap<String, String>();
        data.put("GroupId", "3");
        data.put("GroupName", "testGroup three");
        data.put("CreatedDateTime", "20180115130026757+0000");
        data.put("UpdatedDateTime", "20180115130026757+0000");
        data.put("Createdby", "3");
        data.put("GroupUser",  "{1,2,3,4}");
        data.put("status", "active");

        String mapping = {"typename":{"properties":{
        "GroupName":{"type":"text","index":"not_analyzed"},
        "Createdby":{"type":"text","index":"not_analyzed"},
        "GroupUser":{"type":"text","index":"not_analyzed"},
        "UpdatedDateTime":{"type":"text","index":"not_analyzed"},
        "CreatedDateTime":{"type":"text","index":"not_analyzed"},
        "GroupId":{"type":"text","index":"not_analyzed"},
        "status":{"type":"text","index":"not_analyzed"}}}}

        client = new PreBuiltTransportClient(settings).addTransportAddresses(new TransportAddress(new InetSocketAddress(ipaddress, port)));

        //inserting record
        IndexResponse response = client.prepareIndex(indexName, typeName).setSource(data).get();

        //inserting mapping
        client.admin().indices().preparePutMapping(indexName)
        .setType("typeName")
        .setSource(mapping, XContentType.JSON)
        .get();

为了查找区分大小写的值,我发现要使用索引为not_analyzed。我尝试了以下方法

        HashMap<String, String> data = new HashMap<String, String>();
        data.put("GroupId", "3");
        data.put("GroupName", "testGroup three");
        data.put("CreatedDateTime", "20180115130026757+0000");
        data.put("UpdatedDateTime", "20180115130026757+0000");
        data.put("Createdby", "3");
        data.put("GroupUser",  "{1,2,3,4}");
        data.put("status", "active");

        String mapping = {"typename":{"properties":{
        "GroupName":{"type":"text","index":"not_analyzed"},
        "Createdby":{"type":"text","index":"not_analyzed"},
        "GroupUser":{"type":"text","index":"not_analyzed"},
        "UpdatedDateTime":{"type":"text","index":"not_analyzed"},
        "CreatedDateTime":{"type":"text","index":"not_analyzed"},
        "GroupId":{"type":"text","index":"not_analyzed"},
        "status":{"type":"text","index":"not_analyzed"}}}}

        client = new PreBuiltTransportClient(settings).addTransportAddresses(new TransportAddress(new InetSocketAddress(ipaddress, port)));

        //inserting record
        IndexResponse response = client.prepareIndex(indexName, typeName).setSource(data).get();

        //inserting mapping
        client.admin().indices().preparePutMapping(indexName)
        .setType("typeName")
        .setSource(mapping, XContentType.JSON)
        .get();

我收到如下所示的异常

 java.lang.IllegalArgumentException: Could not convert [GroupName.index] to boolean

我想完成以下两个场景:

 1. Find by case sensitive
 2. Find by case insensitive.

Elasticsearch版本是6.1.2。

非常感谢任何帮助。

更新1

根据@Val,我已将代码更改为:

    HashMap<String, String> data = new HashMap<String, String>();
    data.put("GroupId", "3");
    data.put("GroupName", "testGroup three");
    data.put("CreatedDateTime", "20180115130026757+0000");
    data.put("UpdatedDateTime", "20180115130026757+0000");
    data.put("Createdby", "3");
    data.put("GroupUser",  "{1,2,3,4}");
    data.put("status", "active");

    String mapping = {"typename":{"properties":{
    "GroupName":{"type":"keyword"},
    "Createdby":{"type":"keyword"},
    "GroupUser":{"type":"keyword"},
    "UpdatedDateTime":{"keyword":"text"},
    "CreatedDateTime":{"keyword":"text"},
    "GroupId":{"type":"keyword"},
    "status":{"type":"keyword"}}}}

    client = new PreBuiltTransportClient(settings).addTransportAddresses(new TransportAddress(new InetSocketAddress(ipaddress, port)));

   client.admin().indices().prepareCreate("indexName").get(); 

    //inserting mapping
    client.admin().indices().preparePutMapping(indexName)
    .setType("typeName")
    .setSource(mapping, XContentType.JSON)
    .get();

    //inserting record
    IndexResponse response = client.prepareIndex(indexName, typeName).setSource(data).get();

现在我可以插入了。但是当我搜索GroupName中的a值时,结果是空的。

最佳答案

not_analyzed 已弃用,您需要使用 keyword 代替。

尝试下面的映射:

String mapping = {
  "typename": {
    "properties": {
      "GroupName": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "Createdby": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "GroupUser": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "UpdatedDateTime": {
        "type": "date",
        "format": "yyyyMMddHHmmssSSSZ"
      },
      "CreatedDateTime": {
        "type": "date",
        "format": "yyyyMMddHHmmssSSSZ"
      },
      "GroupId": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "status": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

还要确保在插入数据之前放置映射,即

执行:

//inserting mapping

之前:

//inserting record

关于java - 无法在elasticsearch 6.1.2的映射中插入 "index":"not_analyzed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48882311/

相关文章:

elasticsearch - Elasticsearch如何搜索像@这样的字符

elasticsearch - Elasticsearch 首选项设置为自定义值,文档仍从不同分片返回

sql - 如何执行区分大小写的嵌套 REPLACE 命令?

scheme - Scheme 符号区分大小写

java - 如何在打开下一个 Activity 之前关闭旧 Activity ?

java - 使用点符号的 Thymeleaf 3 map 访问

java - 在“选项”菜单上切换 Wifi 状态

java - Libgdx 渲染矩形阵列不同的纹理

sorting - Elasticsearch 通过分页按价格对 InnerHits 进行排序

php - PHP 函数是否区分大小写?