通过每个单词的开头进行Solr搜索

时间:2019-08-09 13:13:15

标签: solr

我需要实现一个搜索,以寻找每个单词的开头。假设我们有一个带有值的文档:


from __future__ import print_function
from bs4 import BeautifulSoup
import os,sys

with open ("/tmp/crm_output.txt","r") as f:
#with os.popen ("/usr/sbin/crm_mon -r -X") as f:
    contents = f.read()
    soup = BeautifulSoup(contents, 'lxml')
    resource_status = soup.find("resource").attrs["role"]
    resource_name = soup.find("resource").attrs["id"]

if resource_status == "Started":
    print("The status of " +resource_name + " is " + resource_status)
    sys.exit(0)
elif resource_status == "Stopped" or resource_status == "Stopped (disabled)":
    print("The status of " +resource_name + " is " + resource_status)
    sys.exit(1)
elif resource_status == "Failed":
    print("The status of " +resource_name + " is " + resource_status)
    sys.exit(2)
else:
    print("The status of " +resource_name + " is " + "UNKNOWN")
    sys.exit(3)

我希望能够通过以下示例找到该文档:“ ne”,“ wo”,“ ne wo”。

前两个短语易于实现。问题出在最后一个。我已经尝试过以下查询:

with os.popen ("/usr/sbin/crm_mon -r -X") as f:

,并且有效。但是解决方案对我来说似乎有点丑陋,我想知道是否有更快更清洁的东西?使用TermQueries和Recommendationers进行了尝试,但似乎无济于事...

1 个答案:

答案 0 :(得分:1)

您可以使用the Complex Phrase Query Parser

它允许您编写q={!complexphrase inOrder=true}target:"ne* wo*",这与您的示例不同,world new不会产生匹配。

您的另一选择是use a EdgeNgramFilter,然后使用target:"ne wo"搜索。