10

I am trying to use Sphinx Search Engine with their Python API. The installation went fine. But when I use their Python API I do not get the complete result set. I only get the ID's? But when I use their ./search binary in ./bin I get the entire indexed content.

When using cpp ./search binary -

./search test

1. document=1, weight=1, group_id=1, date_added=Sat Sep 11 07:42:38 2010, title=2
    id=1
    group_id=1
    group_id2=5
    date_added=2010-09-11 07:42:38
    title=test one
    content=this is my test document number one. also checking search within phrases.

But when I use the Python API, I get -

>>> import sphinxapi
>>> client = sphinxapi.SphinxClient()
>>> client.SetServer('127.0.0.1', 9312)
>>> client.Query('test')
{'status': 0, 'matches': [{'id': 1, 'weight': 1, 'attrs': {'date_added': 1284171158, 'group_id': 1, 'title': 2}}, {'id': 2, 'weight': 1, 'attrs': {'date_added': 1284171158, 'group_id': 1, 'title': 3}}, {'id': 4, 'weight': 1, 'attrs': {'date_added': 1284171158, 'group_id': 2, 'title': 1}}], 'fields': ['content'], 'time': '0.022', 'total_found': 3, 'warning': '', 'attrs': [['group_id', 1], ['date_added', 2], ['title', 3]], 'words': [{'docs': 6, 'hits': 6, 'word': 'test'}], 'error': '', 'total': 3}

How do I get the string fields like 'title' or 'content' as part of the result set?

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
  • 2
    `Query` does not return the contents of the fulltext fields of each match. It only returns integer attributes and the document ids (in order). You will have to perform additional SQL query to retrieve the documents data. – leoluk Sep 11 '10 at 21:03
  • @leoluk thanks for the response! If what you said is true then I would have to perform additional SQL queries to actually get my data. Is there any way I can get them from Sphinx itself? Since obviously it's index has the relevant text... – Srikar Appalaraju Sep 12 '10 at 04:04
  • Yes, it is possible, but if I knew how I had made an answer out of it – leoluk Sep 13 '10 at 15:36
  • Hi I run the same code but geting error please help me – Sandeep Apr 18 '17 at 08:21
  • http://stackoverflow.com/questions/43466220/sphinx-search-in-python3-django – Sandeep Apr 18 '17 at 08:22
  • @SrikarAppal http://stackoverflow.com/questions/43467783/sphinx-search-assertionerror-error-python Please ans this – Sandeep Apr 18 '17 at 09:22

2 Answers2

10

Although it is possible to do, I don't think it's a good idea to store the "source" in sphinx. Sphinx is very fast for a dedicated search engine only (giving you just IDs and maybe ranking scores - if you need it).

Btw, Official SphinxSearch API is hardly updated, you can actually use MySQL driver/modul (e.g. pymysql). Following is an example:

import pymysql
db = pymysql.connect(host='127.0.0.1',port=9301,user='',passwd='',charset='utf8',db='')
cur = db.cursor()
qry='SELECT id,weight() FROM idx_name WHERE MATCH(\'"your Query"/1\') LIMIT 10 OPTION ranker=SPH04'
cur.execute(qry);row = cur.fetchall()
print(row)
cur.close();db.close()  
taufikedys
  • 319
  • 2
  • 10
4

You could use sql_field_string - add to your config

source YOUR_SOURCE
{
sql_field_string = title
sql_field_string = content

it would index data of these fields and also store these fields as string attributes so you could get them in your result set without additional SQL query.

However as all attributes string attributes always loads into memory that is why you could run out of your box memory quickly.

tmg_tt
  • 474
  • 2
  • 6
  • This is mentioned in Sphinx common mistakes http://sphinxsearch.com/blog/2014/10/14/several-common-mystakes/ and it's mentioned to use the SHOW_META directive to see more information of what was matched. – stommepoes Oct 19 '14 at 19:41
  • @tmg_tt http://stackoverflow.com/questions/43467783/sphinx-search-assertionerror-error-python please ans this – Sandeep Apr 18 '17 at 09:22
  • I have run this query but did not get request. this http://stackoverflow.com/questions/43469933/sphinx-search-not-working-with-python – Jaskaran singh Rajal Apr 18 '17 at 11:02
  • @tmg_tt, I want to highlight the matched text in searched result, can we do this in only one step by using API? – zhuj9 Feb 17 '20 at 05:08