Elasticsearch "starts with" search

Hi

I would like to include hits where the search string matches the start of a product name or article number.

For example a search for “stow” I would want to result in a hit for “stowaway”. Can that be solved by adding a wildcard(*?) or another analyzer?

I have tried something like this with no luck:

`allQueries.Add((qc.Match(x => x.Field(z => z.Name).Query(searchQuery.Text + "*").Boost(10).SynonymAnalyzer())
                                        || qc.Match(x => x.Field(z => z.ArticleNumber).Query(searchQuery.Text.ToLower()).Boost(2).SynonymAnalyzer())
                                        || qc.Match(x => x.Field(z => z.Content).Query(searchQuery.Text).Fuzziness(fuzziness).SynonymAnalyzer())));`

Litium version: 7.5.0

I had to do this on a project back in Litium 7.2, don’t know if it’s a better way:
|| qc.MatchPhrasePrefix(x => x.Field(z => z.ModelNames).Query(searchQuery.Text).Boost(1).MaxExpansions(100))

1 Like

Its working for Name, but when including ArticleNumber also I get an error from the Elasticsearch instance:

The remote server returned an error: (400) Bad Request.. Call: Status code 400 from: POST /prjproductdocument.sv-se/_search?typed_keys=true. ServerError: Type: search_phase_execution_exception Reason: "all shards failed"

Also I wonder if this solution works with synonyms?

For me, article number did not work at all, so i replaced it with a new field in the index and added this:
|| qc.MatchPhrasePrefix(x => x.Field(z => z.SearchableArticleNumber)

1 Like

The searching is dependent on the types of the fields that exists in the model. If we have the default ProductDocument-model from Accelerator we can see that the ArticleNumber property is decorated with the Keyword-attribute and that it not is supposed to use other than direct hits.

From Find strings within strings faster with the Elasticsearch wildcard field | Elastic Blog

when it came to searching strings, Elasticsearch has broadly offered two choices:

  1. Text fields
  2. Keyword fields

Text fields “tokenize” strings into multiple tokens, each typically representing words. Searchers looking for quick foxes can therefore match articles talking about a quick brown fox .

Keyword fields tend to be used for shorter, structured content like a country code which would be indexed without analysis as a single token. These can then be used by analysts e.g. to visualize popular holiday destinations using aggregations .

That works, thank you!

Ok, got it. Is it possible to boost hits on the first word in a tokenized text field? Or are they always weighted the same?

For example the customer would like searches for “stainless” to rank “Stainless pan” above “Sink stainless”

Maybe something as they suggest in this may help (I have not test this) Boosting score relative to location - Elastic Stack / Elasticsearch - Discuss the Elastic Stack but I don’t think it exists a one-property-change function to make it bosting like that.

I solved this by adding a property in the ProductDocument with the first word of the name, and included that property in the search

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.