【问题标题】:Elasticsearch error - While trying to search contents in the attachmentElasticsearch 错误 - 尝试搜索附件中的内容时
【发布时间】:2018-06-22 03:38:20
【问题描述】:

我正在使用摄取附件处理器插件来索引我的 pdf 文件,并且还能够使用 java 代码进行索引。现在,我想在我的 pdf 文件中搜索弹性搜索中可用的一些内容。

我正在使用以下查询来搜索文件中的内容,并且在执行此代码时遇到错误。

SearchRequest contentSearchRequest = new SearchRequest(ATTACHMENT); 
SearchSourceBuilder contentSearchSourceBuilder = new SearchSourceBuilder();
contentSearchRequest.types(TYPE);
QueryBuilder attachmentQB = QueryBuilders.matchQuery("attachment.content", "karthikeyan");
contentSearchSourceBuilder.query(attachmentQB);
contentSearchSourceBuilder.size(50);
searchRequest.source(contentSearchSourceBuilder);
SearchResponse contentSearchResponse = null;
try {
    contentSearchResponse = restHighLevelClient.search(contentSearchRequest);
} catch (IOException e) {
    e.getLocalizedMessage();
}

SearchHit[] contentSearchHits = contentSearchResponse.getHits().getHits();
long contenttotalHits=contentSearchResponse.getHits().totalHits;
System.out.println("condition Total Hits --->"+contenttotalHits);

for (SearchHit contenthit : contentSearchHits) {

    Map<String, Object> sourceAsMap = contenthit.getSourceAsMap();
    System.out.println("----------->"+sourceAsMap.get("resume"));
}

请找到我已索引的文档

{
  "_index": "attach_local",
  "_type": "doc",
  "_id": "106",
  "_version": 1,
  "found": true,
  "_source": {
    "resume": "1MzUgZg0KMDAwMDAwMDAwMCA2NTUzNSBmDQowMDAwMDAxODg1IDAwMDAwIG4NCjAwMDAwMDIxMzIgMDAwMDAgbg0KMDAwMDA4NTIzNiAwMDAwMCBuDQp0cmFpbGVyDQo8PC9TaXplIDIwL1Jvb3QgMSAwIFIvSW5mbyA5IDAgUi9JRFs8NDlEMTEzNzA1QjE0RDc0NUI4MkQ4NzhDODZFMzEzREU+PDQ5RDExMzcwNUIxNEQ3NDVCODJEODc4Qzg2RTMxM0RFPl0gPj4NCnN0YXJ0eHJlZg0KODU1MTMNCiUlRU9GDQp4cmVmDQowIDANCnRyYWlsZXINCjw8L1NpemUgMjAvUm9vdCAxIDAgUi9JbmZvIDkgMCBSL0lEWzw0OUQxMTM3MDVCMTRENzQ1QjgyRDg3OEM4NkUzMTNERT48NDlEMTEzNzA1QjE0RDc0NUI4MkQ4NzhDODZFMzEzREU+XSAvUHJldiA4NTUxMy9YUmVmU3RtIDg1MjM2Pj4NCnN0YXJ0eHJlZg0KODYwNjkNCiUlRU9G",
    "attachment": {
      "date": "2018-06-21T10:50:01Z",
      "content_type": "application/pdf",
      "author": "Karthikeyan A S",
      "language": "sv",
      "content": "My first Elastic Search attachment using ingest-attachment plugin. Karthikeyan",
      "content_length": 71
    },
    "postDate": "2018-06-21T10:53:53.161Z",
    "Name": "Karthikeyan"
  }
}

请找出下面出现的错误

ElasticsearchStatusException[Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]]
    at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:177)
    at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:573)
    at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:549)
    at java.lang.Thread.run(Thread.java:748)
    Suppressed: org.elasticsearch.client.ResponseException: method [GET], host [http://localhost:9200], URI [/attach_local/doc/_search?typed_keys=true&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&search_type=query_then_fetch&batched_reduce_size=512], status line [HTTP/1.1 400 Bad Request]
{"error":{"root_cause":[{"type":"query_shard_exception","reason":"Binary fields do not support searching","index_uuid":"JjE66zAUSKi11FJ_yHVLSA","index":"attach_local"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"attach_local","node":"uPLyU7R5RXeirg8XzRqhnA","reason":{"type":"query_shard_exception","reason":"Binary fields do not support searching","index_uuid":"JjE66zAUSKi11FJ_yHVLSA","index":"attach_local"}}]},"status":400}
        at org.elasticsearch.client.RestClient$1.completed(RestClient.java:357)

请查看我的映射详细信息

PUT attach_local
{
  "mappings" : {
    "doc" : {
      "properties" : {
        "attachment" : {
          "properties" : {
            "content" : {
              "type" : "binary"
            },
            "content_length" : {
              "type" : "long"
            },
            "content_type" : {
              "type" : "text"
            },
            "language" : {
              "type" : "text"
            }
          }
        },
        "resume" : {
          "type" : "text"
        }
      }
    }
  }
}

我使用的是 Elasticsearch 6.2.3 版

【问题讨论】:

  • 将附件.内容的映射更改为文本,然后搜索将适用于这种情况。通常,在使用摄取附件处理器时,附件的映射将是动态的,这意味着您应该让 ES 为您推断映射。
  • @sramalingam24 - 我已将content 的映射从binary 更改为text,它工作正常。

标签: java elasticsearch elastic-stack


【解决方案1】:

映射已更改如下。并且工作正常。

PUT attach_local
{
  "mappings" : {
    "doc" : {
      "properties" : {
        "attachment" : {
          "properties" : {
            "content" : {
              "type" : "text"
            },
            "content_length" : {
              "type" : "long"
            },
            "content_type" : {
              "type" : "text"
            },
            "language" : {
              "type" : "text"
            }
          }
        },
        "resume" : {
          "type" : "text"
        }
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多