Elasticsearch 找不到关键字搜索



我要去一个有PDF文件的文件夹。在 for 循环中,我提取每个 PDF 文件的文本。我的PDF文件中带有文件名的文本(字符串(以名为"e1"的JSON格式存储。然后,我将此 e1 插入弹性搜索数据库。 每次在 for 循环中增加索引号。

我希望能够根据关键字搜索获取 Json 对象列表。 这样我就可以看到关键字存在于哪些对象(我在 Elasticsearch 中插入的"e1"(中。 我现在收到错误DSL类science查询中不存在。虽然科学这个词在PDF中出现了很多次!

import PyPDF2
def read_pdf(pdf_file):
string_file=""
read_pdf = PyPDF2.PdfFileReader(pdf_file)
number_of_pages = read_pdf.getNumPages()
for page_number in range(number_of_pages):
page = read_pdf.getPage(page_number)
page_content = page.extractText()
string_file+=page_content
return string_file
import glob
pdf_list=glob.glob('/home/Jen/Mongo/PDF/*.pdf')
from elasticsearch import Elasticsearch
es=Elasticsearch([{'host':'localhost','port':9200}])

count=0
for i in pdf_list:
count +=1
print(count)
stringi = i.replace('/home/Jen/Mongo/PDF/','')
text=(read_pdf(i))
lowercase_name=stringi.lower()
text=text.lower()
e1={
"filename":stringi,
"text":text}
res = es.index(index=count,doc_type='PDF',id=1,body=e1)
z=input("keyword")# I insert science here
z=z.lower()
from elasticsearch_dsl import Search
s = Search().using(es).query(z)
print(s)

更新此代码不打印任何内容:

import PyPDF2
def read_pdf(pdf_file):
string_file=""
read_pdf = PyPDF2.PdfFileReader(pdf_file)
number_of_pages = read_pdf.getNumPages()
for page_number in range(number_of_pages):
page = read_pdf.getPage(page_number)
page_content = page.extractText()
string_file+=page_content
return string_file
import glob
pdf_list=glob.glob('/home/Jen/Mongo/PDF/*.pdf')
from elasticsearch import Elasticsearch
es=Elasticsearch([{'host':'localhost','port':9200}])

count=0
for i in pdf_list:
count +=1
print(count)
stringi = i.replace('/home/Jen/Mongo/PDF/','')
text=(read_pdf(i))
lowercase_name=stringi.lower()
text=text.lower()
e1={
"filename":stringi,
"text":text}
res = es.index(index="my_name",doc_type='PDF',id=count, body=e1)
print("Test")
from elasticsearch_dsl import Search    
s = Search(using=es, index="my_name").query("match", title="science")
response = s.execute()
for hit in response:
print(response.hits)

使用以下代码行:

res = es.index(index=count,doc_type='PDF',id=1,body=e1)

您正在创建类型为PDF的索引0,1,2..N(因为 count 从0N(,并且每个索引中的每个文档都有_id=1

查看文档

它应该是这样的:

res = es.index(index="my_name",doc_type='PDF',id=count, body=e1)

如果你正确地完成了数据处理的第一部分,你应该将所有文档放在索引my_name并且每个文档都有自己的_id(从 1 到 N(。

只需在 KibanaGET _cat/indices?v中运行,并检查您的 slout 和这些更改

。作为问题的第二部分,您可以搜索my_index中的"科学"(所有文档(:

GET my_index/_search
{
"query": {
"match": {
"my_field": "science"
}
}
}

已更新

GET my_index/_search
{
"query": {
"bool": {
"must": {
"match": {
"my_field": "science"
}
}
}
}
}

更新 2(蟒蛇(

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search
client = Elasticsearch()
s = Search(using=client, index="my_index").query("match", title="science")
response = s.execute()
for hit in response:
print(response.hits)
# print(hit) / or print(hit.title, hit.id, ..)

最新更新