Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Flask中实现MongoDB搜索引擎API_Python_Json_Mongodb_Full Text Search_Flask Restful - Fatal编程技术网

Python 在Flask中实现MongoDB搜索引擎API

Python 在Flask中实现MongoDB搜索引擎API,python,json,mongodb,full-text-search,flask-restful,Python,Json,Mongodb,Full Text Search,Flask Restful,我是MongoDB新手,我正在尝试在Flask中使用MongoDB构建一个简单的搜索页面 我在MongoDB中加入了一个餐厅.json: { "_id" : ObjectId("57506d62f57802807471dd42"), "name" : "456 Cookies Shop", "contact" : { "phone" : "604-555-0149", "email" : "456CookiesShop@example.org", "location" : [ -73.88500

我是MongoDB新手,我正在尝试在Flask中使用MongoDB构建一个简单的搜索页面

我在MongoDB中加入了一个餐厅.json

{ "_id" : ObjectId("57506d62f57802807471dd42"), "name" : "456 Cookies Shop", "contact" : { "phone" : "604-555-0149", "email" : "456CookiesShop@example.org", "location" : [ -73.8850023, 40.7494272 ] }, "stars" : 4, "categories" : [ "Bakery", "Cookies", "Cake", "Coffee" ] }
{ "_id" : ObjectId("57506d62f57802807471dd28"), "name" : "XYZ Bagels Restaurant", "contact" : { "phone" : "435-555-0190", "email" : "XYZBagelsRestaurant@example.net", "location" : [ -74.0707363, 40.59321569999999 ] }, "stars" : 4, "categories" : [ "Bagels", "Sandwiches", "Coffee" ] }
{ "_id" : ObjectId("57506d62f57802807471dd44"), "name" : "XYZ Steak Buffet", "contact" : { "phone" : "229-555-0197", "email" : "XYZSteakBuffet@example.org", "location" : [ -73.9799932, 40.7660886 ] }, "stars" : 3, "categories" : [ "Steak", "Salad", "Chinese" ] }
搜索.py

from flask import Flask, render_template, url_for, request, session, redirect, jsonify
import json
from flask import Flask
from flask_pymongo import PyMongo
from pymongo import MongoClient

app = Flask(__name__)
client = MongoClient('localhost',27017)
mongo = PyMongo(app)
db = client.dummy
collection = db.restaurants

@app.route('/search', methods = ['GET'])
def search():
    search = mongo.db.collection
    output = []
    for q in search.find():
        output.append({'name' : q['name'], 'categories' : q['categories']})
    return jsonify({'result' : output})

@app.route('/search/<name>', methods = ['GET'])
def search_by_keyword(name):
    search_by_keyword = mongo.db.collection
    q = search_by_keyword.find_one({'name' : name})
    output = {'name' : q['name']}

    return jsonify({'results' : output})

if __name__ == '__main__':
app.run(debug=True)
from wtforms import Form, StringField, SelectField

class SearchForm(Form):
    choices = [('name', 'name'),
           ('categories', 'categories')]
    select = SelectField('Search:', choices = choices)
    search = StringField('')
  • 如何进行关键字搜索以搜索所有Mongo字段,而不仅仅是我指定的字段(名称)
  • 如何使json输出文件在html中作为搜索结果发布

您需要在感兴趣的字段或使用通配符的所有字段上创建
文本
索引
*

db.restaurants.ensureIndex( { "$**": "text" } )
名称
类别

db.restaurants.ensureIndex({"name" : "text", "categories" : "text"})
在所有字段上创建索引
*

db.restaurants.ensureIndex( { "$**": "text" } )
搜索字符串

> db.restaurants.find({$text : {$search : "XYZ", $caseSensitive : false}})
{ "_id" : ObjectId("57506d62f57802807471dd44"), "name" : "XYZ Steak Buffet", "contact" : { "phone" : "229-555-0197", "email" : "XYZSteakBuffet@example.org", "location" : [ -73.9799932, 40.7660886 ] }, "stars" : 3, "categories" : [ "Steak", "Salad", "Chinese" ] }
{ "_id" : ObjectId("57506d62f57802807471dd28"), "name" : "XYZ Bagels Restaurant", "contact" : { "phone" : "435-555-0190", "email" : "XYZBagelsRestaurant@example.net", "location" : [ -74.0707363, 40.59321569999999 ] }, "stars" : 4, "categories" : [ "Bagels", "Sandwiches", "Coffee" ] }
搜索
类别
数组中的字符串

> db.restaurants.find({$text : {$search : "Chinese", $caseSensitive : false}})
{ "_id" : ObjectId("57506d62f57802807471dd44"), "name" : "XYZ Steak Buffet", "contact" : { "phone" : "229-555-0197", "email" : "XYZSteakBuffet@example.org", "location" : [ -73.9799932, 40.7660886 ] }, "stars" : 3, "categories" : [ "Steak", "Salad", "Chinese" ] }
> 
您可以使用返回的json在页面中呈现

显示索引

> db.restaurants.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "bitcoin.restaurants"
    },
    {
        "v" : 2,
        "key" : {
            "_fts" : "text",
            "_ftsx" : 1
        },
        "name" : "name_text_categories_text",
        "ns" : "bitcoin.restaurants",
        "weights" : {
            "categories" : 1,
            "name" : 1
        },
        "default_language" : "english",
        "language_override" : "language",
        "textIndexVersion" : 3
    }