比亚迪技术鱼池持续高产 整车智能重新定义智能化下半场
2024-12-20
CREATE TABLE Model ( id int(10) unsigned NOT NULL auto_increment, label varchar(7) NOT NULL, description varchar(256) NOT NULL, begin_production int(4) NOT NULL, end_production int(4) NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB; |
INSERT INTO Model (`id`, `label`, `description`, `begin_production`, `end_production`) VALUES (1,'X Sedan','Four-door performance sedan',1998,1999), (3,'X Sedan','Four door performance sedan, 1st model year',1995,1997), (4,'J Convertible','Two-door roadster, metal retracting roof',2002,2005), (5,'J Convertible','Two-door roadster',2000,2001), (7,'W Wagon','Four-door, all-wheel drive sport station wagon',2007,0); |
CREATE TABLE Assembly ( id int(10) unsigned NOT NULL auto_increment, label varchar(7) NOT NULL, description varchar(128) NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB; |
INSERT INTO Assembly (`id`, `label`, `description`) VALUES (1,'5-00','Seats'), (2,'4-00','Electrical'), (3,'3-00','Glasses'), (4,'2-00','Frame'), (5,'1-00','Engine'), (7,'101-00','Accessories'); |
CREATE TABLE Inventory ( id int(10) unsigned NOT NULL auto_increment, partno varchar(32) NOT NULL, description varchar(256) NOT NULL, price float unsigned NOT NULL default '0', PRIMARY KEY (id), UNIQUE KEY partno USING BTREE (partno) ) ENGINE=InnoDB; |
INSERT INTO `Inventory` (`id`, `partno`, `description`, `price`) VALUES (1,'WIN408','Portal window',423), (2,'ACC711','Jack kit',110), (3,'ACC43','Rear-view mirror',55), (4,'ACC5409','Cigarette lighter',20), (5,'WIN958','Windshield, front',500), (6,'765432','Bolt',0.1), (7,'ENG001','Entire engine',10000), (8,'ENG088','Cylinder head',55), (9,'ENG976','Large cylinder head',65); |
CREATE TABLE Schematic ( id int(10) unsigned NOT NULL auto_increment, partno_id int(10) unsigned NOT NULL, assembly_id int(10) unsigned NOT NULL, model_id int(10) unsigned NOT NULL, PRIMARY KEY (id), KEY partno_index USING BTREE (partno_id), KEY assembly_index USING BTREE (assembly_id), KEY model_index USING BTREE (model_id), FOREIGN KEY (partno_id) REFERENCES Inventory(id), FOREIGN KEY (assembly_id) REFERENCES Assembly(id), FOREIGN KEY (model_id) REFERENCES Model(id) ) ENGINE=InnoDB; |
INSERT INTO `Schematic` (`id`, `partno_id`, `assembly_id`, `model_id`) VALUES (1,6,5,1), (2,8,5,1), (3,1,3,1), (4,5,3,1), (5,8,5,7), (6,6,5,7), (7,4,7,3), (8,9,5,3); |
source catalog { type = mysql sql_host = localhost sql_user = reaper sql_pass = s3cr3t sql_db = body_parts sql_sock = /var/run/mysqld/mysqld.sock sql_port = 3306 |
CREATE OR REPLACE VIEW Catalog AS SELECT Inventory.id, Inventory.partno, Inventory.description, Assembly.id AS assembly, Model.id AS model FROM Assembly, Inventory, Model, Schematic WHERE Schematic.partno_id=Inventory.id AND Schematic.model_id=Model.id AND Schematic.assembly_id=Assembly.id; |
mysql> use body_parts; Database changed mysql> select * from Catalog; +----+---------+---------------------+----------+-------+ | id | partno | description | assembly | model | +----+---------+---------------------+----------+-------+ | 6 | 765432 | Bolt | 5 | 1 | | 8 | ENG088 | Cylinder head | 5 | 1 | | 1 | WIN408 | Portal window | 3 | 1 | | 5 | WIN958 | Windshield, front | 3 | 1 | | 4 | ACC5409 | Cigarette lighter | 7 | 3 | | 9 | ENG976 | Large cylinder head | 5 | 3 | | 8 | ENG088 | Cylinder head | 5 | 7 | | 6 | 765432 | Bolt | 5 | 7 | +----+---------+---------------------+----------+-------+ 8 rows in set (0.00 sec) |
# indexer query # document_id MUST be the very first field # document_id MUST be positive (non-zero, non-negative) # document_id MUST fit into 32 bits # document_id MUST be unique sql_query = \ SELECT \ id, partno, description, \ assembly, model \ FROM \ Catalog; sql_group_column = assembly sql_group_column = model # document info query # ONLY used by search utility to display document information # MUST be able to fetch document info by its id, therefore # MUST contain '$id' macro # sql_query_info = SELECT * FROM Inventory WHERE id=$id } |
index catalog { source = catalog path = /var/data/sphinx/catalog morphology = stem_en min_word_len = 3 min_prefix_len = 0 min_infix_len = 3 } |
source catalog { type = mysql sql_host = localhost sql_user = reaper sql_pass = s3cr3t sql_db = body_parts sql_sock = /var/run/mysqld/mysqld.sock sql_port = 3306 # indexer query # document_id MUST be the very first field # document_id MUST be positive (non-zero, non-negative) # document_id MUST fit into 32 bits # document_id MUST be unique sql_query = \ SELECT \ id, partno, description, \ assembly, model \ FROM \ Catalog; sql_group_column = assembly sql_group_column = model # document info query # ONLY used by search utility to display document information # MUST be able to fetch document info by its id, therefore # MUST contain '$id' macro # sql_query_info = SELECT * FROM Inventory WHERE id=$id } index catalog { source = catalog path = /var/data/sphinx/catalog morphology = stem_en min_word_len = 3 min_prefix_len = 0 min_infix_len = 3 } searchd { port = 3312 log = /var/log/searchd/searchd.log query_log = /var/log/searchd/query.log pid_file = /var/log/searchd/searchd.pid } |
底部的 searchd 部分将配置 searchd 守护程序本身。该部分中的条目不言自明。query.log 尤为有用:它将在运行时显示每次搜索并显示结果,例如搜索的文档数和匹配总数。
构建和测试索引
您现在已经准备好为 Body Parts 应用程序构建索引。为此,需要执行以下步骤:
键入 $ sudo mkdir -p /var/data/sphinx 创建目录结构 /var/data/sphinx #p#分页标题#e#
假定 MySQL 正在运行,使用如下所示的代码运行索引器来创建索引。
清单 10. 创建索引
$ sudo /usr/local/bin/indexer --config /usr/local/etc/sphinx.conf --all Sphinx 0.9.7 Copyright (c) 2001-2007, Andrew Aksyonoff using config file '/usr/local/etc/sphinx.conf'... indexing index 'catalog'... collected 8 docs, 0.0 MB sorted 0.0 Mhits, 82.8% done total 8 docs, 149 bytes total 0.010 sec, 14900.00 bytes/sec, 800.00 docs/sec |
$ /usr/local/bin/search --config /usr/local/etc/sphinx.conf ENG Sphinx 0.9.7 Copyright (c) 2001-2007, Andrew Aksyonoff index 'catalog': query 'ENG ': returned 2 matches of 2 total in 0.000 sec displaying matches: 1. document=8, weight=1, assembly=5, model=7 id=8 partno=ENG088 description=Cylinder head price=55 2. document=9, weight=1, assembly=5, model=3 id=9 partno=ENG976 description=Large cylinder head price=65 words: 1. 'eng': 2 documents, 2 hits $ /usr/local/bin/search --config /usr/local/etc/sphinx.conf wind Sphinx 0.9.7 Copyright (c) 2001-2007, Andrew Aksyonoff index 'catalog': query 'wind ': returned 2 matches of 2 total in 0.000 sec displaying matches: 1. document=1, weight=1, assembly=3, model=1 id=1 partno=WIN408 description=Portal window price=423 2. document=5, weight=1, assembly=3, model=1 id=5 partno=WIN958 description=Windshield, front price=500 words: 1. 'wind': 2 documents, 2 hits $ /usr/local/bin/search \ --config /usr/local/etc/sphinx.conf --filter model 3 ENG Sphinx 0.9.7 Copyright (c) 2001-2007, Andrew Aksyonoff index 'catalog': query 'ENG ': returned 1 matches of 1 total in 0.000 sec displaying matches: 1. document=9, weight=1, assembly=5, model=3 id=9 partno=ENG976 description=Large cylinder head price=65 words: 1. 'eng': 2 documents, 2 hits |
<?php include('sphinx-0.9.7/api/sphinxapi.php'); $cl = new SphinxClient(); $cl->SetServer( "localhost", 3312 ); $cl->SetMatchMode( SPH_MATCH_ANY ); $cl->SetFilter( 'model', array( 3 ) ); $result = $cl->Query( 'cylinder', 'catalog' ); if ( $result === false ) { echo "Query failed: " . $cl->GetLastError() . ".\n"; } else { if ( $cl->GetLastWarning() ) { echo "WARNING: " . $cl->GetLastWarning() . ""; } if ( ! empty($result["matches"]) ) { foreach ( $result["matches"] as $doc => $docinfo ) { echo "$doc\n"; } print_r( $result ); } } exit; ?> |
$ sudo mkdir -p /var/log/searchd $ sudo /usr/local/bin/searchd --config /usr/local/etc/sphinx.conf $ php search.php 9 Array ( [fields] => Array ( [0] => partno [1] => description ) [attrs] => Array( [assembly] => 1 [model] => 1 ) [matches] => Array( [9] => Array( [weight] => 1 [attrs] => Array( [assembly] => 5 [model] => 3 ) ) ) [total] => 1 [total_found] => 1 [time] => 0.000 [words] => Array( [cylind] => Array( [docs] => 2 [hits] => 2 ) ) ) |
评论 {{userinfo.comments}}
{{child.content}}
{{question.question}}
提交