Elasticsearch中GET查询指定文档无返回结果的原因排查
问题描述
我尝试导入一份PDF文档,已成功创建comptes_rendus_municipaux索引并完成文档导入。执行GET comptes_rendus_municipaux/_search?pretty=true能看到ID为29090-2023-09-04的文档存在,但执行GET comptes_rendus_municipaux/_doc/29090-2023-09-04查询该文档时,控制台无任何返回(无错误信息、无_index/_id等元数据)。
索引创建方式
PUT comptes_rendus_municipaux/ { "aliases": {}, "mappings": { "properties": { "attachment": { "properties": { "author": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "content": { "type": "text", "analyzer": "french", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "content_length": { "type": "long" }, "content_type": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "creator_tool": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "date": { "type": "date" }, "format": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "keywords": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "language": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "modified": { "type": "date" }, "title": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } }, "data": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } } }
文档导入方式
导入脚本index.sh
#!/bin/bash export source=$1 export index=$2 export entree=$3 # Le paramètre source doit être alimenté if [ -z "$source" ]; then echo "Le nom du fichier pdf à indexer dans Elastic est attendu en paramètre." >&2 exit 1 fi # Si le fichier source n'a pas d'extension, lui rajouter celle .pdf if [[ "$source" != *"."* ]]; then source=$source.pdf fi # Il doit avoir l'extension pdf if [[ "$source" != *".pdf" ]]; then echo "Le fichier à indexer dans Elastic doit avoir l'extension .pdf" >&2 exit 1 fi # Si l'index n'est pas alimenté, lui assigner apprentissage if [ -z "$index" ]; then index=apprentissage fi # Si l'entrée n'est pas alimentée, lui assigner le nom du fichier sans extension if [ -z "$entree" ]; then entree=$(basename "${source%.*}") fi host="http://localhost:9200" user="elastic" pwd="Ox-JoQT0po=pdOT-LeE*" json_file=$(mktemp) cur_url="$host/$index/_doc/$entree?pipeline=attachment" echo '{"data" : "'"$( base64 "$source" -w 0 )"'"}' >"$json_file" # echo "transfert via $json_file vers $cur_url" if ! ingest=$(curl -s -X PUT -H "Content-Type: application/json" -u "$user:$pwd" -d "@$json_file" "$cur_url"); then echo "Echec de l'ingestion dans Elastic de $source dans l'index $index, entrée $entree : $ingest" >&2 exit $? fi rm "$json_file" echo "$source indexé dans l'entrée $entree de l'index $index d'Elastic"
执行命令
index Kerlaz-Compte-rendu-conseil-04-09-2023 comptes_rendus_municipaux 29090-2023-09-04
执行结果
Kerlaz-Compte-rendu-conseil-04-09-2023.pdf indexé dans l'entrée 29090-2023-09-04 de l'index comptes_rendus_municipaux d'Elastic
可能原因及解决方法
1. 路由参数不匹配
这是此类现象最常见的原因:文档写入时使用了自定义路由值,但查询时未携带该参数。_search会遍历所有分片找到文档,而_doc/<id>查询需要路由值才能定位到正确的分片。
验证步骤:
执行以下查询获取文档的路由元字段:
GET comptes_rendus_municipaux/_search { "query": { "term": { "_id": "29090-2023-09-04" } }, "_source": false, "fields": ["_routing"] }
解决方法:
如果返回了_routing值,查询时携带该参数:
GET comptes_rendus_municipaux/_doc/29090-2023-09-04?routing=<查询到的路由值>
2. 索引分片未同步
若索引配置了多个副本,可能存在主分片写入成功但副本未同步的情况,导致直接查询ID时无法定位到文档。
验证步骤:
检查索引分片健康状态:
GET _cluster/health/comptes_rendus_municipaux?level=shards
解决方法:
手动刷新索引后重试查询:
POST comptes_rendus_municipaux/_refresh
3. ID存在一致性问题
Elasticsearch的文档ID大小写敏感,需确认_search返回的_id与查询时使用的ID完全一致(包括连字符、大小写等)。
4. 管道处理异常
导入时使用的attachment管道若存在错误逻辑(如条件过滤导致文档被丢弃),可能出现文档在搜索结果中可见但无法通过ID查询的异常。
验证步骤:
查看管道配置:
GET _ingest/pipeline/attachment
检查是否包含drop处理器或其他可能修改/丢弃文档的逻辑。
内容的提问来源于stack exchange,提问作者Marc Le Bihan
相关产品推荐
相关产品推荐

