You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Christopher Schultz <ch...@christopherschultz.net> on 2018/07/24 16:05:27 UTC

Possible to define a field so that substring-search is always used?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

All,

We are using Solr as a user index, and users have email addresses.

Our old search behavior used a SQL substring match for any search
terms entered, and so users are used to being able to search for e.g.
"chr" and finding my email address ("chris@christopherschultz.net").

By default, Solr doesn't perform substring matches, and it might be
difficult to re-train users to use *chr* to find email addresses by
substring.

Is there a way to define the field such that searches are always done
as a substring? While we are at it, I'd like to define the field to
avoid tokenization because it's never useful to search for
"me@gmail.com" and find a few million search results because many
users use @gmail.com email addresses.

Here is the current field definition from our create-schema script:

  "add-field":{
     "name":"email_address",
     "type":"text_general",
     "multiValued" : false,
     "stored":true },

Later, we add the email address to the "all" field (which aggregates
everything from all useful fields into the field used as the
default-field):

  "add-copy-field":{
     "source":"email_address",
     "dest":"all" },

Is there a way to define these fields such that:

1. The email_address field is always searched using a substring
2. The email_address field is not tokenized
3. The copied-email-address is not tokenized in the "all" field

Thanks,
- -chris
-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAltXTkcACgkQHPApP6U8
pFh1aRAAilB2nVGycjVyY2taAJv6x2ss33UcVL6xBATRUkHTCbyAr5LFN3FWmcOR
iCbZdxCU5LSa0x0clMTlRjR0U8HF+l2J4ArMQYiveA9mXc6fZz+ovAYrBqDguE6b
UZnbOcR3pDF+P5h3ch9aMbdkHAhsVN7AX5yiSIS0fqKn6irNrI7TkvRmiZqNzVFx
sDIPChL9meMfh8rz7vVmu5IjaImnQZ+2tmc+QruFsbgKGXJMR4n+d0CjacIfd5vp
hoZDpg9qcasnYau925xqlj4BBrPS1XiYOqvdgCxnO1l6qqVfBK+lVsPaP5FOtXZP
7Fe/unkzuK8j1Y0mZNpcZtMYYhsMHboT1Kegrn1mUZp9S6iL1NzbqzmsbDQyNqlg
8HghvGG7ROj/hkqLPOlGy6wp72GFQYrHuIEzdyDI9wHOaP+cdliCdkkmqIAQJilR
ketzTVhEbOHGEHGa9obHg0NPqmYwP4DDmSOZ42z5UPr2KqaqpeXsqcB2CV7nnvB3
6hvKuHVWIrHE1P1k1XFwMF3Vy+YbeojFbvKLH+eNKXXOXu8PEn2MaZU5v12WNWEr
0l6K16VnFf436WqH/fSa1DZUfuphA4z0qg/oHqcUcfhVFjc+U1wSZVvdvpG+rSf1
n3NS9pqFAWruWq7V0ID5cV0PVRwp9g6pgs4XJAhKYEkiXVO8u7Y=
=wAsa
-----END PGP SIGNATURE-----

Re: Possible to define a field so that substring-search is always used?

Posted by Erick Erickson <er...@gmail.com>.
1. the standard way to do this is to use ngrams. The index is larger,
but it gives you much quicker searches than trying to to
pre-and-postfix wildcards

2. use a fieldType with KeywordTokenizerFactory + (probably)
LowerCaseFilterFactory + TrimFilterFactory. And, in your case,
NGramTokenizerFactory (I'd start with bigrams, i.e. min=2 and max=2)

3. no. The destination field has it's own field type and that's how
the input stream is analyzed. There's no good way to say "don't
analyze input from field X when copied to field Y". Probably best not
to copy it there at all.

Best,
Erick

On Tue, Jul 24, 2018 at 9:05 AM, Christopher Schultz
<ch...@christopherschultz.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
>
> All,
>
> We are using Solr as a user index, and users have email addresses.
>
> Our old search behavior used a SQL substring match for any search
> terms entered, and so users are used to being able to search for e.g.
> "chr" and finding my email address ("chris@christopherschultz.net").
>
> By default, Solr doesn't perform substring matches, and it might be
> difficult to re-train users to use *chr* to find email addresses by
> substring.
>
> Is there a way to define the field such that searches are always done
> as a substring? While we are at it, I'd like to define the field to
> avoid tokenization because it's never useful to search for
> "me@gmail.com" and find a few million search results because many
> users use @gmail.com email addresses.
>
> Here is the current field definition from our create-schema script:
>
>   "add-field":{
>      "name":"email_address",
>      "type":"text_general",
>      "multiValued" : false,
>      "stored":true },
>
> Later, we add the email address to the "all" field (which aggregates
> everything from all useful fields into the field used as the
> default-field):
>
>   "add-copy-field":{
>      "source":"email_address",
>      "dest":"all" },
>
> Is there a way to define these fields such that:
>
> 1. The email_address field is always searched using a substring
> 2. The email_address field is not tokenized
> 3. The copied-email-address is not tokenized in the "all" field
>
> Thanks,
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Comment: GPGTools - http://gpgtools.org
> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
>
> iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAltXTkcACgkQHPApP6U8
> pFh1aRAAilB2nVGycjVyY2taAJv6x2ss33UcVL6xBATRUkHTCbyAr5LFN3FWmcOR
> iCbZdxCU5LSa0x0clMTlRjR0U8HF+l2J4ArMQYiveA9mXc6fZz+ovAYrBqDguE6b
> UZnbOcR3pDF+P5h3ch9aMbdkHAhsVN7AX5yiSIS0fqKn6irNrI7TkvRmiZqNzVFx
> sDIPChL9meMfh8rz7vVmu5IjaImnQZ+2tmc+QruFsbgKGXJMR4n+d0CjacIfd5vp
> hoZDpg9qcasnYau925xqlj4BBrPS1XiYOqvdgCxnO1l6qqVfBK+lVsPaP5FOtXZP
> 7Fe/unkzuK8j1Y0mZNpcZtMYYhsMHboT1Kegrn1mUZp9S6iL1NzbqzmsbDQyNqlg
> 8HghvGG7ROj/hkqLPOlGy6wp72GFQYrHuIEzdyDI9wHOaP+cdliCdkkmqIAQJilR
> ketzTVhEbOHGEHGa9obHg0NPqmYwP4DDmSOZ42z5UPr2KqaqpeXsqcB2CV7nnvB3
> 6hvKuHVWIrHE1P1k1XFwMF3Vy+YbeojFbvKLH+eNKXXOXu8PEn2MaZU5v12WNWEr
> 0l6K16VnFf436WqH/fSa1DZUfuphA4z0qg/oHqcUcfhVFjc+U1wSZVvdvpG+rSf1
> n3NS9pqFAWruWq7V0ID5cV0PVRwp9g6pgs4XJAhKYEkiXVO8u7Y=
> =wAsa
> -----END PGP SIGNATURE-----

Re: Possible to define a field so that substring-search is always used?

Posted by Erick Erickson <er...@gmail.com>.
If you're not familiar with the edismax query parser, that's often what
people use to fire the query off against more than one field without
having the users be aware of it. That means you could ngram
the e-mail field and when a user types something in the search
box search against both the "all" and "email" fields without them having
to know they exist.

Best,
Erick

On Wed, Jul 25, 2018 at 6:23 AM, Christopher Schultz
<ch...@christopherschultz.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
>
> Chris,
>
> On 7/24/18 4:46 PM, Chris Hostetter wrote:
>>
>> : We are using Solr as a user index, and users have email
>> addresses. : : Our old search behavior used a SQL substring match
>> for any search : terms entered, and so users are used to being able
>> to search for e.g. : "chr" and finding my email address
>> ("chris@christopherschultz.net"). : : By default, Solr doesn't
>> perform substring matches, and it might be : difficult to re-train
>> users to use *chr* to find email addresses by : substring.
>>
>> In the past, were you really doing arbitrary substring matching, or
>> just prefix matching?  ie would a search for "sto" match
>> "chris@christopherschultz.net"
>
> Yes. Searching for "sto" would result in a SQL query with a " WHERE
> ... LIKE '%sto%'" clause. So it was slow as hell, of course.
>
>> Personally, if you know you have an email field, would suggest
>> using a custom tokenizer that splits on "@" and "." (and maybe
>> other punctuation characters like "-") and then take your raw user
>> input and feed it to the prefix parser (instead of requiring your
>> users to add the "*")...
>>
>> q={!prefix f=email v=$user_input}&user_input=chr
>>
>> ...which would match chris@gmail.com, foo@chris.com, foo@bar.chr
>> etc.
>>
>> (this wouldn't help you though if you *really* want arbitrary
>> substring matching -- as erick suggested ngrams is pretty much your
>> best bet for something like that)
>>
>> Bear in mind, you can combine that "forced prefix" query against
>> the (otkenized) email field with other queries that could parse
>> your input in other ways...
>>
>> user_input=... q=({!prefix f=email v=$user_input} OR {!dismax
>> qf="first_name last_name" ..etc.. v=$user_input})
>>
>> so if your user input is "chris" you'll get term matches on the
>> first_name field, or the last_name field as well as prefix matches
>> on the email field.
>
> The problem is that our users (admins) sometimes need to locate users
> by their email address, and people often forget the exact spelling. So
> they'll call and say "I can't get in" and we have to search for "chris
> schultz" and then "chris" and then it turns out that their email
> address was actually sexylover42@yahoo.com, so they often have to try
> a bunch of searches before finding the right user record. Having to
> search for "sexylover42", a complete-match word, isn't going to work
> for their use-case. They need to be able to search for "lover" and
> have it work. I think n-grams sounds like the only way to get this
> done. I'll have to play-around with it a little bit to see how it behave
> s.
>
> Thanks,
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Comment: GPGTools - http://gpgtools.org
> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
>
> iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAltYedQACgkQHPApP6U8
> pFjzgQ/9GW7kI9Lefnmj7zH8JsqZfW1Y/PrF4YA1RjbliNWRn2dRPz7Q7C2ITO/n
> Ys73uUII3qPz8M/H6d0LN57Un96BGAjIhf6WZSiIRAQcvenhGaS/lROciq6I8iN8
> hB+1X2GixTG8fbq6Q6Q3jRG22S0GpW+OL2mJcu3wCkQ2dzyBWObWxjF1ag5O4pT+
> AP0lqAgpUTsWAeMPPd6dkuStOhXraJQc+1WwwEw36gohwaZwLMftcOl2ohnys/DM
> pdyqQEQ6fOldJLBHLU8PyNVHxJA5qZjVTwu3S7zv7w+2N+V8bHOl6y5ir3krOEs0
> OIvFX+Do+pbsg+QQ5VY8LDxbPBCjgDiWTpplh3Ym0raaVMoMQ6GfFfsOPF9jYhxS
> gb0eMwVTJFWM0xvMaH4xSXLR/Dh6upT/0do1sTr7kKjhIlwc3pfR/vIwqsVer1HJ
> Qsj6Pc+ZJckOrPGGIYCZEWZwlS8ONinAx4fh23/C1GltU19kHtRvGTQLzRT+9sus
> 2stvkD44Lv7zuc49/Y07NISxcUceTlbZHKC5ebzAtKNDS2p+qYLJlbdTZQIofMsb
> zmncdP+s5cSYgiCZZS19E2GxP7Yw2rmSn2zsSF6yJMgMy9logJi5HS1UQ54IWvn7
> eAzvM+TcV6i+8Hf9kijNcg4/OZPv67DZt6HDcXO2K+a/AMyQElE=
> =4Y/b
> -----END PGP SIGNATURE-----

Re: Possible to define a field so that substring-search is always used?

Posted by Alexandre Rafalovitch <ar...@gmail.com>.
> I think n-grams sounds like the only way to get this done.

You don't have to settle for "the only way". You can totally have the
same field(s) copyFielded into multiple locations and then have each
target field use a different indexing pipeline, including ngrams,
phonetic processing, full match with/without "@domain" part, etc.
Then, with eDismax multi-field searches and/or boost queries you can
give higher boost to the copies with least amount of processing and
lower boost to less-precise, more-inclusive matches.

Regards,
   Alex.

On 25 July 2018 at 09:23, Christopher Schultz
<ch...@christopherschultz.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
>
> Chris,
>
> On 7/24/18 4:46 PM, Chris Hostetter wrote:
>>
>> : We are using Solr as a user index, and users have email
>> addresses. : : Our old search behavior used a SQL substring match
>> for any search : terms entered, and so users are used to being able
>> to search for e.g. : "chr" and finding my email address
>> ("chris@christopherschultz.net"). : : By default, Solr doesn't
>> perform substring matches, and it might be : difficult to re-train
>> users to use *chr* to find email addresses by : substring.
>>
>> In the past, were you really doing arbitrary substring matching, or
>> just prefix matching?  ie would a search for "sto" match
>> "chris@christopherschultz.net"
>
> Yes. Searching for "sto" would result in a SQL query with a " WHERE
> ... LIKE '%sto%'" clause. So it was slow as hell, of course.
>
>> Personally, if you know you have an email field, would suggest
>> using a custom tokenizer that splits on "@" and "." (and maybe
>> other punctuation characters like "-") and then take your raw user
>> input and feed it to the prefix parser (instead of requiring your
>> users to add the "*")...
>>
>> q={!prefix f=email v=$user_input}&user_input=chr
>>
>> ...which would match chris@gmail.com, foo@chris.com, foo@bar.chr
>> etc.
>>
>> (this wouldn't help you though if you *really* want arbitrary
>> substring matching -- as erick suggested ngrams is pretty much your
>> best bet for something like that)
>>
>> Bear in mind, you can combine that "forced prefix" query against
>> the (otkenized) email field with other queries that could parse
>> your input in other ways...
>>
>> user_input=... q=({!prefix f=email v=$user_input} OR {!dismax
>> qf="first_name last_name" ..etc.. v=$user_input})
>>
>> so if your user input is "chris" you'll get term matches on the
>> first_name field, or the last_name field as well as prefix matches
>> on the email field.
>
> The problem is that our users (admins) sometimes need to locate users
> by their email address, and people often forget the exact spelling. So
> they'll call and say "I can't get in" and we have to search for "chris
> schultz" and then "chris" and then it turns out that their email
> address was actually sexylover42@yahoo.com, so they often have to try
> a bunch of searches before finding the right user record. Having to
> search for "sexylover42", a complete-match word, isn't going to work
> for their use-case. They need to be able to search for "lover" and
> have it work. I think n-grams sounds like the only way to get this
> done. I'll have to play-around with it a little bit to see how it behave
> s.
>
> Thanks,
> - -chris

Re: Possible to define a field so that substring-search is always used?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Chris,

On 7/24/18 4:46 PM, Chris Hostetter wrote:
> 
> : We are using Solr as a user index, and users have email
> addresses. : : Our old search behavior used a SQL substring match
> for any search : terms entered, and so users are used to being able
> to search for e.g. : "chr" and finding my email address
> ("chris@christopherschultz.net"). : : By default, Solr doesn't
> perform substring matches, and it might be : difficult to re-train
> users to use *chr* to find email addresses by : substring.
> 
> In the past, were you really doing arbitrary substring matching, or
> just prefix matching?  ie would a search for "sto" match 
> "chris@christopherschultz.net"

Yes. Searching for "sto" would result in a SQL query with a " WHERE
... LIKE '%sto%'" clause. So it was slow as hell, of course.

> Personally, if you know you have an email field, would suggest
> using a custom tokenizer that splits on "@" and "." (and maybe
> other punctuation characters like "-") and then take your raw user
> input and feed it to the prefix parser (instead of requiring your
> users to add the "*")...
> 
> q={!prefix f=email v=$user_input}&user_input=chr
> 
> ...which would match chris@gmail.com, foo@chris.com, foo@bar.chr
> etc.
> 
> (this wouldn't help you though if you *really* want arbitrary
> substring matching -- as erick suggested ngrams is pretty much your
> best bet for something like that)
> 
> Bear in mind, you can combine that "forced prefix" query against 
> the (otkenized) email field with other queries that could parse
> your input in other ways...
> 
> user_input=... q=({!prefix f=email v=$user_input} OR {!dismax
> qf="first_name last_name" ..etc.. v=$user_input})
> 
> so if your user input is "chris" you'll get term matches on the 
> first_name field, or the last_name field as well as prefix matches
> on the email field.

The problem is that our users (admins) sometimes need to locate users
by their email address, and people often forget the exact spelling. So
they'll call and say "I can't get in" and we have to search for "chris
schultz" and then "chris" and then it turns out that their email
address was actually sexylover42@yahoo.com, so they often have to try
a bunch of searches before finding the right user record. Having to
search for "sexylover42", a complete-match word, isn't going to work
for their use-case. They need to be able to search for "lover" and
have it work. I think n-grams sounds like the only way to get this
done. I'll have to play-around with it a little bit to see how it behave
s.

Thanks,
- -chris
-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAltYedQACgkQHPApP6U8
pFjzgQ/9GW7kI9Lefnmj7zH8JsqZfW1Y/PrF4YA1RjbliNWRn2dRPz7Q7C2ITO/n
Ys73uUII3qPz8M/H6d0LN57Un96BGAjIhf6WZSiIRAQcvenhGaS/lROciq6I8iN8
hB+1X2GixTG8fbq6Q6Q3jRG22S0GpW+OL2mJcu3wCkQ2dzyBWObWxjF1ag5O4pT+
AP0lqAgpUTsWAeMPPd6dkuStOhXraJQc+1WwwEw36gohwaZwLMftcOl2ohnys/DM
pdyqQEQ6fOldJLBHLU8PyNVHxJA5qZjVTwu3S7zv7w+2N+V8bHOl6y5ir3krOEs0
OIvFX+Do+pbsg+QQ5VY8LDxbPBCjgDiWTpplh3Ym0raaVMoMQ6GfFfsOPF9jYhxS
gb0eMwVTJFWM0xvMaH4xSXLR/Dh6upT/0do1sTr7kKjhIlwc3pfR/vIwqsVer1HJ
Qsj6Pc+ZJckOrPGGIYCZEWZwlS8ONinAx4fh23/C1GltU19kHtRvGTQLzRT+9sus
2stvkD44Lv7zuc49/Y07NISxcUceTlbZHKC5ebzAtKNDS2p+qYLJlbdTZQIofMsb
zmncdP+s5cSYgiCZZS19E2GxP7Yw2rmSn2zsSF6yJMgMy9logJi5HS1UQ54IWvn7
eAzvM+TcV6i+8Hf9kijNcg4/OZPv67DZt6HDcXO2K+a/AMyQElE=
=4Y/b
-----END PGP SIGNATURE-----

Re: Possible to define a field so that substring-search is always used?

Posted by Chris Hostetter <ho...@fucit.org>.
: We are using Solr as a user index, and users have email addresses.
: 
: Our old search behavior used a SQL substring match for any search
: terms entered, and so users are used to being able to search for e.g.
: "chr" and finding my email address ("chris@christopherschultz.net").
: 
: By default, Solr doesn't perform substring matches, and it might be
: difficult to re-train users to use *chr* to find email addresses by
: substring.

In the past, were you really doing arbitrary substring matching, or just 
prefix matching?  ie would a search for "sto" match 
"chris@christopherschultz.net"

Personally, if you know you have an email field, would suggest using a 
custom tokenizer that splits on "@" and "." (and maybe other punctuation 
characters like "-") and then take your raw user input and feed it to the 
prefix parser (instead of requiring your users to add the "*")...

 q={!prefix f=email v=$user_input}&user_input=chr

...which would match chris@gmail.com, foo@chris.com, foo@bar.chr etc. 

(this wouldn't help you though if you *really* want arbitrary substring 
matching -- as erick suggested ngrams is pretty much your best bet for 
something like that)

Bear in mind, you can combine that "forced prefix" query against 
the (otkenized) email field with other queries that 
could parse your input in other ways...

user_input=...
q=({!prefix f=email v=$user_input} 
   OR {!dismax qf="first_name last_name" ..etc.. v=$user_input})

so if your user input is "chris" you'll get term matches on the 
first_name field, or the last_name field as well as prefix matches on the 
email field.



-Hoss
http://www.lucidworks.com/