We use ElasticSearch to index customer records (name + address) and in our queries we use trigram searches.
When i profiled some of the queries i noticed something peculiar that i don't understand, and i was hoping to get some insight.
Have a look at the profiler output below and check out the time spent on each of the trigram TermQueries
For example, the four TermQueries for the firstName "Sabine" all take around 208.000-244.000ns and thats more or less in line with all the other trigram queries in that search (fullName, street, city)...EXCEPT for lastName. Here the individual TermQueries take around 1.2-1.5 Million ns which is significantly slower and adds up to more than 24% of the total query time.
Initially i thought it might have something to do with the total amount of trigrams in each index. So because on average a first name might be shorter than a lastname, there will be less trigrams in the first name inverted index and it will be faster.
But that doesn't fit with the street field, which is on average probably the longest field and should then be the slowest. but it's not, it's in line (end even slightly faster) than the first name query.
by the way, there are around 15 million records in that index, so when i say "on average" then it should actually be a pretty good average to assume.
7,2% 1.529.277ns ( 0,00s) BoostQuery (person.fullName.trigram:sab person.fullName.trigram:abi person.fullName.trigram:bin person.fullName.trigram:ine person.fullName.trigram:koe person.fullName.trigram:oen person.fullName.trigram:eni person.fullName.trigram:nig)^2.0
11,7% 179.616ns ( 0,00s) TermQuery person.fullName.trigram:sab
9,8% 149.382ns ( 0,00s) TermQuery person.fullName.trigram:abi
9,5% 145.463ns ( 0,00s) TermQuery person.fullName.trigram:bin
9,9% 151.973ns ( 0,00s) TermQuery person.fullName.trigram:ine
8,6% 131.949ns ( 0,00s) TermQuery person.fullName.trigram:koe
9,5% 144.911ns ( 0,00s) TermQuery person.fullName.trigram:oen
9,3% 142.253ns ( 0,00s) TermQuery person.fullName.trigram:eni
9,0% 138.348ns ( 0,00s) TermQuery person.fullName.trigram:nig
6,3% 1.329.048ns ( 0,00s) SynonymQuery Synonym(person.firstName.phonetic:sYbini person.firstName.phonetic:sabin person.firstName.phonetic:sabini person.firstName.phonetic:savini person.firstName.phonetic:sobin person.firstName.phonetic:sobini person.firstName.phonetic:sovini person.firstName.phonetic:zYbini person.firstName.phonetic:zabini person.firstName.phonetic:zobini)
5,7% 1.215.579ns ( 0,00s) BooleanQuery person.firstName.trigram:sab person.firstName.trigram:abi person.firstName.trigram:bin person.firstName.trigram:ine
20,1% 244.919ns ( 0,00s) TermQuery person.firstName.trigram:sab
17,6% 213.711ns ( 0,00s) TermQuery person.firstName.trigram:abi
17,2% 208.705ns ( 0,00s) TermQuery person.firstName.trigram:bin
18,1% 219.848ns ( 0,00s) TermQuery person.firstName.trigram:ine
6,8% 1.436.954ns ( 0,00s) BoostQuery (Synonym(person.lastName.phonetic:kDnik person.lastName.phonetic:kYnik person.lastName.phonetic:koinik person.lastName.phonetic:kunik))^2.0
24,3% 5.147.155ns ( 0,01s) BoostQuery (person.lastName.trigram:koe person.lastName.trigram:oen person.lastName.trigram:eni person.lastName.trigram:nig)^2.0
29,3% 1.506.565ns ( 0,00s) TermQuery person.lastName.trigram:koe
27,8% 1.432.001ns ( 0,00s) TermQuery person.lastName.trigram:oen
25,9% 1.335.459ns ( 0,00s) TermQuery person.lastName.trigram:eni
24,0% 1.233.435ns ( 0,00s) TermQuery person.lastName.trigram:nig
2,7% 573.936ns ( 0,00s) BoostQuery (Synonym(address.street.phonetic:Dpdbl address.street.phonetic:apdbl address.street.phonetic:updbl address.street.phonetic:xDpdbl address.street.phonetic:xapdbl address.street.phonetic:xupdbl))^2.0
6,7% 1.413.559ns ( 0,00s) BoostQuery (address.street.trigram:hau address.street.trigram:aup address.street.trigram:upt address.street.trigram:ptb address.street.trigram:tbu address.street.trigram:bue address.street.trigram:ueh address.street.trigram:ehl)^2.0
12,6% 178.647ns ( 0,00s) TermQuery address.street.trigram:hau
10,8% 152.014ns ( 0,00s) TermQuery address.street.trigram:aup
10,6% 150.251ns ( 0,00s) TermQuery address.street.trigram:upt
6,1% 86.559ns ( 0,00s) TermQuery address.street.trigram:ptb
8,3% 116.926ns ( 0,00s) TermQuery address.street.trigram:tbu
10,0% 141.978ns ( 0,00s) TermQuery address.street.trigram:bue
9,9% 139.370ns ( 0,00s) TermQuery address.street.trigram:ueh
10,2% 144.440ns ( 0,00s) TermQuery address.street.trigram:ehl