solr的模糊搜索主要有通配符,范围查询,近距离搜索等几类。下面分别探讨一下用法。

1. 通配符查询

通配符只是对单个term有效,对短语不起作用,ps:短语就是在查询条件上加双引号,比如 title:”xxx yyy”。
其实也就跟一般的通配符的匹配方式差不多了,比如我要查询title里有evaluat开头的文档:

title:evaluat*
  • 1

Note that:查询语句里一定要用小写,还没研究清楚大小写的区分,但是用大写的不行
solr之模糊搜索(Fuzzy matching)_双引号
另外通配符有很大的性能开销,尤其在匹配到大量数据时,比如:e*,建议不要这样用。
其他例子:

title:evaluat* cipro
  • 1

solr之模糊搜索(Fuzzy matching)_搜索_02

title:evaluat?
  • 1

solr之模糊搜索(Fuzzy matching)_solr_03
solr in action 例子:

Query: offi* Matches office, officer, official, and so on
Query: off*r Matches offer, officer, officiator, and so on
Query: off?r Matches offer, but not officer

Works: softwar* eng?neering
Does not work: "softwar* eng?neering"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2. 范围查询

范围查询可以支持时间范围,数值范围,字符串范围等
1. 时间范围:
查询2015-06-07T19:11:45Z TO 2015-06-10T19:11:45Z这个范围的文档,这里要注意格式必须是solr的日期格式,也就是2015-06-07T19:11:45Z这样的格式,必须大写TO

received_date:[2015-06-07T19:11:45Z TO 2015-06-10T19:11:45Z]
  • 1

solr之模糊搜索(Fuzzy matching)_lua_04

  1. 数值范围
Query: yearsOld:[18 TO 21] Matches 18, 19, 20, 21
  • 1
  1. 文本范围
source:[kyowa TO kyowb]
  • 1

实际匹配kyowa ….kyowb => a->b的范围,也就是只能包含kyowa, kyowb这两个单词
solr之模糊搜索(Fuzzy matching)_双引号_05
solr in action 例子:

Query: created:[2012-02-01T00:00.0Z TO 2012-08-02T00:00.0Z]
Query: title:[boat TO boulder] Matches boat, boil, book, boulder, etc.
Query: price:[12.99 TO 14.99] Matches 12.99, 13.000009, 14.99, etc
// 这是带边界的例子
Query: yearsOld:{18 TO 21} Matches 19 and 20 but not 18 or 21
Query: yearsOld:[18 TO 21} Matches 18, 19, 20, but not 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3. 近距离搜索(FUZZY/EDIT-DISTANCE SEARCHING/PROXIMITY SEARCHING)

  1. EDIT-DISTANCE SEARCHING
    这主要是为了解决输入错误的问题,比如输入good时错误的输入为goob了怎么办,solr用波浪…….
    线来容错,比如
source:kyowb~1
  • 1

这里本来要输入kyowa,结果输成了kyowb,那么加上~1就能把正确的kyowa查询出来,~N也就是允许有几个编辑位置错误,这里是1个位置错误,如果你输入kyoab~1,就查询不出来了,因为输入错误两个位置,这时就需要~2了,默认情况下是~2.
1个位置错误时:
solr之模糊搜索(Fuzzy matching)_搜索_06
2个位置错误时:
solr之模糊搜索(Fuzzy matching)_双引号_07
fixed

solr之模糊搜索(Fuzzy matching)_lua_08
solr in action 例子:

Query: administrator~1 Matches within one edit distance.
Query: administrator~2 Matches within two edit distances. (This is the default
if no edit distance is provided.)
Query: administrator~N Matches within N edit distances.
Please
  • 1
  • 2
  • 3
  • 4
  • 5
  1. 邻近搜索(PROXIMITY SEARCHING)
    这个功能主要用来解决短语的模糊搜索问题,比如你要查找chief executive officer,chief financial officer,chief marketing officer….等等如chief x officer的短语怎么办,用OR连起来显然麻烦,这就是邻近搜索的作用了,只需”chief officer”~1即可,~1表示chief officer之间最多只有一个单词,是最多,不是只有一个,也可以是0个,如果有N个单词,即~N,注意,一定要加双引号,这才表示是短语,不然就是一个term了。
    solr之模糊搜索(Fuzzy matching)_邻近搜索_09

solr in action 例子:

Query: "chief officer"~1
– Meaning: chief and officer must be a maximum of one position away.
– Examples: "chief executive officer", "chief financial officer"
 Query: "chief officer"~2
– Meaning: chief and officer must be a maximum of two edit distances away.
– Examples: "chief business development officer",
"officer chief"
 Query: "chief officer"~N
– Meaning: Finds chief within N positions of officer.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

By David_Ao