# Query string doesn't work correctly

**URL:** <https://discuss.elastic.co/t/query-string-doesnt-work-correctly/78690>\
**Category:** Elasticsearch\
**Created:** [March 15, 2017, 11:31am UTC](https://discuss.elastic.co/t/query-string-doesnt-work-correctly/78690 "2017-03-15T11:31:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grigorios\_Loukidis](https://avatars.discourse-cdn.com/v4/letter/g/f17d59/32.png) [@Grigorios\_Loukidis](https://discuss.elastic.co/u/Grigorios_Loukidis)\
**Post date:** [March 15, 2017, 11:31am UTC](https://discuss.elastic.co/t/query-string-doesnt-work-correctly/78690/1 "2017-03-15T11:31:00Z")

</div>

Hello! I am new to elastic. I am trying to search with the following code, but when I want to get back the word "house", if I type "hou" I can find it , but if I type "ouse" it doesn't work.Also, the analyzer doesn't [work.Is](http://work.Is) this the right place to add it?

```
   var response = client.Search<Homes>(n => n
                        .Index(index)
                        .Type(type)
                        .Size(searchSize)
                        .From(0)
                        //.Analyzer(analyzername)
                        .TrackScores(true)
                       .Query(q=>q.QueryString(qs=>qs.Query("*"+searchWord+"*")
                                                     .Fields(f=>f.Field(fieldsForSearchList[0]))
                                                     .Analyzer(analyzername)))
   );
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 15, 2017, 11:46am UTC](https://discuss.elastic.co/t/query-string-doesnt-work-correctly/78690/2 "2017-03-15T11:46:52Z")

</div>

You can define a ngram based analyzer in your mapping.

This might help:

[https://www.elastic.co/guide/en/elasticsearch/guide/2.x/\_ngrams\_for\_partial\_matching.html](https://www.elastic.co/guide/en/elasticsearch/guide/2.x/_ngrams_for_partial_matching.html)

---

<div class="post-metadata">

**Author:** ![Grigorios\_Loukidis](https://avatars.discourse-cdn.com/v4/letter/g/f17d59/32.png) [@Grigorios\_Loukidis](https://discuss.elastic.co/u/Grigorios_Loukidis)\
**Post date:** [March 15, 2017, 11:51am UTC](https://discuss.elastic.co/t/query-string-doesnt-work-correctly/78690/3 "2017-03-15T11:51:32Z")

</div>

Thanks david for your response! I have already use it but it returns me many unrelated results. I would like a more exact and efficient search.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 15, 2017, 12:38pm UTC](https://discuss.elastic.co/t/query-string-doesnt-work-correctly/78690/4 "2017-03-15T12:38:48Z")

</div>

Did you try to use a simple search\_analyzer in addition to the ngram analyzer?

If you provide a full example we can probably help.

---

<div class="post-metadata">

**Author:** ![Grigorios\_Loukidis](https://avatars.discourse-cdn.com/v4/letter/g/f17d59/32.png) [@Grigorios\_Loukidis](https://discuss.elastic.co/u/Grigorios_Loukidis)\
**Post date:** [March 16, 2017, 8:00am UTC](https://discuss.elastic.co/t/query-string-doesnt-work-correctly/78690/5 "2017-03-16T08:00:20Z")

</div>

Here is indexing:  
var response = client.CreateIndex(index, s =\> s  
.Settings(s1 =\> s1

```
                                                                              .NumberOfShards(5)
                                                                              .NumberOfReplicas(5)
                                                                              .Analysis(a => a.TokenFilters(t => t
                                                                                                                .IcuTransform("greeklatin", it => it.Id("Greek-Latin; NFD; [:Nonspacing Mark:] Remove; NFC")//
                                                                                                                                                  .Direction(IcuTransformDirection.Forward)) //
                                                                                                                .IcuTransform("latingreek", lg => lg.Id("Greek-Latin; NFD; [:Nonspacing Mark:] Remove; NFC")
                                                                                                                                                   .Direction(IcuTransformDirection.Reverse))
                                                                                                                .EdgeNGram("greekedge", ed => ed.MaxGram(35)
                                                                                                                                     .MinGram(2)
                                                                                                                                     .Side(EdgeNGramSide.Front)
                                                                                                                                    )

                                                                                                                .Stop("greekstop", sw => sw.StopWords())
                                                                                                                .Lowercase("greeklowercase", gl => gl.Language(Language.Greek.ToString()))
                                                                                                                .KeywordMarker("greekkeywords", gk => gk.Keywords(""))
                                                                                                                .Stemmer("greekstemmer", gs => gs.Language(Language.Greek.ToString())))

                                                                                             .Analyzers(a1 => a1
                                                                                             .Custom("greek", t => t.Tokenizer("standard")
                                                                                                                      .Filters("greekedge","greekstop", "greeklowercase", "greekkeywords", "greekstemmer", "greeklatin","latingreek"))
                                                                                             .Standard("standard",st=>st.MaxTokenLength(20))
                                                                                                                      
                                                                                                             ))
                                                                                                                  ).Mappings(m => m.Map(type, mt => mt.Properties(c => c.Text(c1 => c1.Name("SERVICE_SN").Analyzer("greek"))
                                                                                                                                      .Text(c2 => c2.Name("car").Analyzer("greek").SearchAnalyzer("standard"))
                                                                                                                                      .Text(c3 => c3.Name("carid").Analyzer("greek").SearchAnalyzer("standard"))
                                                                                                                                      .Text(c4 => c4.Name("route").Analyzer("greek").SearchAnalyzer("standard"))
                                                                                                                                      .Text(c5 => c5.Name("routeid").Analyzer("greek").SearchAnalyzer("standard"))
                                                                                                                                      .Text(c6 => c6.Name("partsdesc").Analyzer("greek").SearchAnalyzer("standard"))
                                                                                                                                      .Text(c7 => c7.Name("partid").Analyzer("greek").SearchAnalyzer("standard"))
                                                                                                                                      .Text(c8 => c8.Name("country").Analyzer("greek").SearchAnalyzer("standard"))

                                                                                                                                                ))));

```

and my searching

```
 var response = client.Search<Cars>(n => n
                        .Index(index)
                        .Type(type)
                        .Query(m => m.Bool(q => q
                        .Must(sh => sh.Match(m1 => m1
                              .Analyzer(analyzername)
                              .Query("*" + searchWord + "*")
                              .Field(fieldsForSearchList[0])
                              .Fuzziness(Fuzziness.EditDistance(1))))))
                        .Size(searchSize)
                        .From(0)
                        .TrackScores(true)
                    );
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 16, 2017, 4:53pm UTC](https://discuss.elastic.co/t/query-string-doesnt-work-correctly/78690/6 "2017-03-16T16:53:08Z")

</div>

`.Query("*" + searchWord + "*")` OMG this is scary.

Did you see: [Wildcard Query | Elasticsearch Guide [5.2] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-wildcard-query.html)

> Note that this query can be slow, as it needs to iterate over many terms. In order to prevent extremely slow wildcard queries, a wildcard term **should not start with one of the wildcards \* or ?**.

BTW please try to format correctly your posts (indentation) so it's more readable. You can use markdown like:

````
```
CODE
```

````

It would be easier also to have a full REST script which reproduces a full example of your problem.  
Have a look at [this example](https://discuss.elastic.co/t/about-the-elasticsearch-category/21).

[Analyze API](https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-analyze.html) will help you a lot to understand what is actually index and what elasticsearch will use at query time to analyze what the user is searching for.

Then it will be easier to check what match or does not match.

I don't believe that a match query needs to have any wildcard character TBH. I don't believe either that you should pass the analyzer within the query as it has been defined in the mapping.

Whatever, if you can provide a script, that will be easier to help.

---

<div class="post-metadata">

**Author:** ![Grigorios\_Loukidis](https://avatars.discourse-cdn.com/v4/letter/g/f17d59/32.png) [@Grigorios\_Loukidis](https://discuss.elastic.co/u/Grigorios_Loukidis)\
**Post date:** [March 17, 2017, 9:59am UTC](https://discuss.elastic.co/t/query-string-doesnt-work-correctly/78690/7 "2017-03-17T09:59:40Z")

</div>

thank you for your precious advices!!May in the future I will remove \* before searching term.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 14, 2017, 9:59am UTC](https://discuss.elastic.co/t/query-string-doesnt-work-correctly/78690/8 "2017-04-14T09:59:48Z")

</div>

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