Elasticsearch (爬虫)集成
    		       		warning:
    		            这篇文章距离上次修改已过444天,其中的内容可能已经有所变动。
    		        
        		                
                
from datetime import datetime
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Document, Date, Nested, Boolean, Text, Keyword, Integer, Float, connections
 
# 连接到Elasticsearch
connections.create_connection(hosts=['localhost:9200'])
 
class Product(Document):
    name = Text(fields={'raw': Keyword()})
    price = Float()
    in_stock = Boolean()
    timestamp = Date()
    class Index:
        name = 'products'
        using = 'products_index'
 
# 创建一个新的Product文档
product = Product(
    name='Example Product',
    price=99.99,
    in_stock=True,
    timestamp=datetime.now()
)
 
# 保存文档到Elasticsearch
product.save()
 
# 搜索所有产品
for hit in Product.search().query('match_all'):
    print(hit.name, hit.price)这段代码展示了如何使用Elasticsearch Python API创建一个简单的产品索引,并对其进行搜索。它首先连接到Elasticsearch实例,然后定义了一个Product文档类,并提供了一个示例来创建一个新的产品文档并将其保存到索引中。最后,它演示了如何执行一个简单的匹配所有文档的搜索。
评论已关闭