Query string doesn't work correctly


(Grigorios Loukidis) #1

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

(David Pilato) #2

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


(Grigorios Loukidis) #3

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.


(David Pilato) #4

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.


(Grigorios Loukidis) #5

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

(David Pilato) #6

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

Did you see: 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.

Analyze API 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.


(Grigorios Loukidis) #7

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


(system) closed #8

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