Skip to content

why can't search anyting, including no condition #2809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
xiaowangAndyismengxin opened this issue Mar 1, 2025 · 5 comments
Closed

why can't search anyting, including no condition #2809

xiaowangAndyismengxin opened this issue Mar 1, 2025 · 5 comments
Labels
Area: Client Manually written code that fits in no other area Category: Question

Comments

@xiaowangAndyismengxin
Copy link

{'took': 0, 'timed_out': False, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 0, 'relation': 'eq'}, 'max_score': None, 'hits': []}}

@pquentin
Copy link
Member

pquentin commented Mar 1, 2025

Hello, can you please provide more details? What query did you try to run, and what results did you expect?

@xiaowangAndyismengxin
Copy link
Author

result = es.search(index='news')
print(result)
{'took': 0, 'timed_out': False, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 0, 'relation': 'eq'}, 'max_score': None, 'hits': []}}
even kibana says the data is exist

@xiaowangAndyismengxin
Copy link
Author

xiaowangAndyismengxin commented Mar 1, 2025

my code

from elasticsearch import Elasticsearch
import json

es = Elasticsearch(
    "https://elastic:E3VUUEUJ5GcLxc2Pp%3D7s@localhost:9200/",
    verify_certs=False,
    ssl_show_warn=False,
)

es.options(ignore_status=404).indices.delete(index="news")
es.options(ignore_status=400).indices.create(index="news")

for data in enumerate(json.load(open("novel_data.json", encoding="UTF-8"))):
    es.create(index="news", id=str(data[0]), body=data[1])
    print(f"数据插入成功 id: {data[0]}")

data = {
    "title": "985保送路线有哪些?",
}
status = es.update(index="news", id="3", body={"doc": data})
print(status)
doc = es.get(index="news", id="3")
print(doc)

status = es.delete(index="news", id="3")
print(status)
result = es.search(index="news")
print(result)

output:

D:\13802\Desktop\CrawlerNotePad\cnp_venv\Scripts\python.exe D:\13802\Desktop\CrawlerNotePad\第四章:数据的存储\Elasticsearch搜索引擎存储\数据的操作.py
数据插入成功 id: 0
数据插入成功 id: 1
数据插入成功 id: 2
数据插入成功 id: 3
数据插入成功 id: 4
数据插入成功 id: 5
数据插入成功 id: 6
数据插入成功 id: 7
{'_index': 'news', '_id': '3', '_version': 2, 'result': 'updated', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 8, '_primary_term': 1}
{'_index': 'news', '_id': '3', '_version': 2, '_seq_no': 8, '_primary_term': 1, 'found': True, '_source': {'title': '985保送路线有哪些?', 'categorize': 'useful novel'}}
{'_index': 'news', '_id': '3', '_version': 3, 'result': 'deleted', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 9, '_primary_term': 1}
{'took': 0, 'timed_out': False, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 0, 'relation': 'eq'}, 'max_score': None, 'hits': []}}

进程已结束,退出代码为 0

@pquentin
Copy link
Member

pquentin commented Mar 1, 2025

Elasticsearch is a near real time system, where documents are not indexed directly after having been created: https://www.elastic.co/guide/en/elasticsearch/reference/current/near-real-time.html.

Get, update and delete are not affected, but search is. To fix this, you need to either wait or refresh your index before searching:

es.indices.refresh(index="news")

result = es.search(index="news")
print(result)

@xiaowangAndyismengxin
Copy link
Author

Oh, thank you.

@pquentin pquentin closed this as not planned Won't fix, can't repro, duplicate, stale Mar 1, 2025
@pquentin pquentin added Area: Client Manually written code that fits in no other area Category: Question labels Mar 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Client Manually written code that fits in no other area Category: Question
Projects
None yet
Development

No branches or pull requests

2 participants