# Elasticsearch "starts with" search

**URL:** https://forum.litium.com/t/elasticsearch-starts-with-search/2063
**Category:** Questions
**Tags:** search, litium-7
**Created:** [May 19, 2021, 8:57am UTC](https://forum.litium.com/t/elasticsearch-starts-with-search/2063 "2021-05-19T08:57:39Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Johan](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/johan/32/357_2.png) [@Johan](https://forum.litium.com/u/Johan)
#### Post date: [May 19, 2021, 8:57am UTC](https://forum.litium.com/t/elasticsearch-starts-with-search/2063/1 "2021-05-19T08:57:39Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Ericsj11](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/ericsj11/32/899_2.png) [@Ericsj11](https://forum.litium.com/u/Ericsj11)
#### Post date: [May 19, 2021, 11:44am UTC](https://forum.litium.com/t/elasticsearch-starts-with-search/2063/2 "2021-05-19T11:44:21Z")

</div>

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))`

---

<div class="post-metadata">

### Author: ![Johan](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/johan/32/357_2.png) [@Johan](https://forum.litium.com/u/Johan)
#### Post date: [May 19, 2021, 2:08pm UTC](https://forum.litium.com/t/elasticsearch-starts-with-search/2063/3 "2021-05-19T14:08:57Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Ericsj11](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/ericsj11/32/899_2.png) [@Ericsj11](https://forum.litium.com/u/Ericsj11)
#### Post date: [May 19, 2021, 2:22pm UTC](https://forum.litium.com/t/elasticsearch-starts-with-search/2063/4 "2021-05-19T14:22:50Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)
#### Post date: [May 19, 2021, 7:38pm UTC](https://forum.litium.com/t/elasticsearch-starts-with-search/2063/5 "2021-05-19T19:38:26Z")

</div>

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](https://www.elastic.co/blog/find-strings-within-strings-faster-with-the-new-elasticsearch-wildcard-field)

> when it came to searching strings, Elasticsearch has broadly offered two choices:
> 
> 1. `Text` fields
> 2. `Keyword` fields
> 
> `Text` fields “[tokenize](https://www.elastic.co/guide/en/elasticsearch/reference/7.9/analysis-tokenizers.html)” 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_ .

---

<div class="post-metadata">

### Author: ![Johan](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/johan/32/357_2.png) [@Johan](https://forum.litium.com/u/Johan)
#### Post date: [May 21, 2021, 11:07am UTC](https://forum.litium.com/t/elasticsearch-starts-with-search/2063/6 "2021-05-21T11:07:15Z")

</div>

That works, thank you!

---

<div class="post-metadata">

### Author: ![Johan](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/johan/32/357_2.png) [@Johan](https://forum.litium.com/u/Johan)
#### Post date: [May 21, 2021, 11:12am UTC](https://forum.litium.com/t/elasticsearch-starts-with-search/2063/7 "2021-05-21T11:12:44Z")

</div>

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”

---

<div class="post-metadata">

### Author: ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)
#### Post date: [May 21, 2021, 12:06pm UTC](https://forum.litium.com/t/elasticsearch-starts-with-search/2063/8 "2021-05-21T12:06:20Z")

</div>

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](https://discuss.elastic.co/t/boosting-score-relative-to-location/13362) but I don’t think it exists a one-property-change function to make it bosting like that.

---

<div class="post-metadata">

### Author: ![Johan](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/johan/32/357_2.png) [@Johan](https://forum.litium.com/u/Johan)
#### Post date: [May 27, 2021, 3:04pm UTC](https://forum.litium.com/t/elasticsearch-starts-with-search/2063/9 "2021-05-27T15:04:16Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/12eddac3b8b661de1db86af10ac8151b571e6fb9.png) [@system](https://forum.litium.com/u/system)
#### Post date: [June 24, 2021, 3:04pm UTC](https://forum.litium.com/t/elasticsearch-starts-with-search/2063/10 "2021-06-24T15:04:34Z")

</div>

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