注意:
这是一个交叉发布,它首先发布在sphinx forum上,但是我没有答案,所以我将其发布在这里。
首先看一个例子:
以下是我的表格(仅用于测试):
+----+--------------------------+----------------------+ | Id | title | body | +----+--------------------------+----------------------+ | 1 | National first hospital | NASA | | 2 | National second hospital | Space Administration | | 3 | National govenment | Support the hospital | +----+--------------------------+----------------------+
I want to search the contents from the title and body field, so I config the sphinx.confas shown followed:
--------The sphinx config file---------- source mysql { type = mysql sql_host = localhost sql_user = root sql_pass =0000 sql_db = testfull sql_port = 3306 # optional, default is 3306 sql_query_pre = SET NAMES utf8 sql_query = SELECT * FROM test } index mysql { source = mysql path = var/data/mysql_old_test docinfo = extern mlock = 0 morphology = stem_en, stem_ru, soundex min_stemming_len = 1 min_word_len = 1 charset_type = utf-8 html_strip = 0 } indexer { mem_limit = 128M } searchd { listen = 9312 read_timeout = 5 max_children = 30 max_matches = 1000 seamless_rotate = 0 preopen_indexes = 0 unlink_old = 1 pid_file = var/log/searchd_mysql.pid log = var/log/searchd_mysql.log query_log = var/log/query_mysql.log } ------------------
Then I reindex the db and start the searchd daemon.
In my client side I set the attribute as:
----------Client side config-------------------
sc = new SphinxClient();
///other thing
HashMap<String, Integer> weiMap=new HashMap<String, Integer>();
weiMap.put("title", 100);
weiMap.put("body", 0);
sc.SetFieldWeights(weiMap);
sc.SetMatchMode(SphinxClient.SPH_MATCH_ALL);
sc.SetSortMode(SphinxClient.SPH_SORT_EXTENDED,"@weight DESC");
当我尝试搜索“国立医院”时,得到以下输出:
Query 'National hospital' retrieved 3 of 3 matches in 0.0 sec. Query stats: 'nation' found 3 times in 3 documents 'hospit' found 3 times in 3 documents Matches: 1. id=3, weight=101 2. id=1, weight=100 3. id=2, weight=100
The match number (three matched) is right,however the order of the result is not what Iwanted.
Obviously the document of id 1 and 2 should be the most closed items to the requiredstring( "National hospital" ), so in my opinion they should be given the largestweights,but they are orderd at the last position.
I wonder if there is anyway to meet my requirement?
PS:
1)please do not suggestion me set the sortModel to :
sc.SetSortMode(SphinxClient.SPH_SORT_EXTENDED,"@weight ASC");
这可能仅适用于此示例,它将导致其他一些潜在问题。
2)我表的内容是中文,我只是用“ National Hosp..l”做
一个例子。
最佳答案
1°您问“国立医院”,但狮身人面像搜索“国家”和“医院”,因为
morphology = stem_en, stem_ru, soundex
2°你给体重
weiMap.put("title", 100);
weiMap.put("body", 0);
到不存在的文本字段
sql_query = SELECT * FROM test
3°总结我对主要问题的简单回答
你按体重排序
第三行的分量更大,因为国家与医院之间没有任何话语
关于full-text-search - sphinx 如何计算重量?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3699398/