linux fulltext search

This commit is contained in:
j 2019-02-12 13:57:27 +05:30
parent 6115850f17
commit ee315399bb

View file

@ -2,6 +2,7 @@ import logging
import os import os
import subprocess import subprocess
import sys import sys
from urllib.parse import quote, unquote
from sqlalchemy.sql import operators from sqlalchemy.sql import operators
@ -14,6 +15,8 @@ def get_prefix():
def get_ids(books): def get_ids(books):
from item.models import File from item.models import File
if not books:
return []
ids = [b[0] for b in File.query.filter(operators.in_op(File.path, books)).values('sha1')] ids = [b[0] for b in File.query.filter(operators.in_op(File.path, books)).values('sha1')]
return ids return ids
@ -34,23 +37,29 @@ def find_fulltext_windows(query):
def find_fulltext_linux(query): def find_fulltext_linux(query):
prefix = get_prefix() prefix = get_prefix()
prefix_url = quote(prefix)
cmd = [ cmd = [
'tracker', 'tracker',
'sparql', 'sparql',
'-q', '-q',
"SELECT nie:url(?f) WHERE { ?f fts:match '%s' FILTER (tracker:uri-is-descendant ('file://%s', nie:url (?u))) }" % (query, prefix) "SELECT nie:url(?f) WHERE { ?f fts:match '%s' FILTER (tracker:uri-is-descendant ('file://%s', nie:url (?f))) }" % (query, prefix_url)
] ]
books = subprocess.check_output(cmd).decode().strip().split('\n') books = subprocess.check_output(cmd).decode().strip().split('\n')
books = [b.split(':')[0] for b in books] books = [
return get_ids(books, prefix) unquote(r.strip()).replace('file://', '')[len(prefix):]
for r in books if r.strip().startswith('file://')
]
return get_ids(books)
def find_fulltext(query): def find_fulltext(query):
ids = [] ids = []
if sys.platform == 'darwin': if sys.platform == 'darwin':
ids = find_fulltext_macos(query) ids = find_fulltext_macos(query)
elif sys.platform == 'linux':
ids = find_fulltext_linux(query)
else: else:
logger.debug('missing fulltext search implementation for %s', sys.platform) logger.debug('missing fulltext search implementation for %s', sys.platform)
return ids return ids
def platform_supported(): def platform_supported():
return sys.platform == 'darwin' return sys.platform in ('darwin', 'linux')