0
votes

I'm currently working on a NEST searcher for a phones database. I've had very little luck with the dynamic version of things in terms of making it so that a user can filter certain terms to search for in the frontend.

This is because NEST doesn't like replacing a field "f.something" with a variable. Due to this I've gone to static because I believe I can do that with some object instantiation.

However, now even though I'm getting valid NEST responses back they're always empty even though there's obviously a result to be had. Such as "Name" for field and "iPhone" for query. What am I missing? Thanks in advance.

P.S. The commented-out code used to have "bool" and "should" checks in but similarly I kept getting no results.

private ISearchResponse<MasterProduct> SearchThis(ElasticClient client, string query, string field, int pageSize, int recordNumber)
        {
            var searchLayout = new SearchRequest<MasterProduct>
            {
                Size = pageSize,
                From = recordNumber,
                Query = new MatchQuery
                {
                    Field = field,
                    Query = query,
                    Fuzziness = Fuzziness.Auto,
                    PrefixLength = 2,
                    Lenient = true,
                    FuzzyRewrite = MultiTermQueryRewrite.TopTermsBlendedFreqs(10)
                }
            };
            var searchResponse = client.Search<MasterProduct>(searchLayout);
            return searchResponse;
            }

            /*var searchResponse = client.Search<MasterProduct>(s => s
                .From(recordNumber)
                .Size(pageSize)
                .Query(q => q
                .Match(a => a
                    .Field(f => f.MasterProductName) 
                    .Query(query)
                    .Fuzziness(Fuzziness.Auto)
                    .PrefixLength(2)
                    .Fuzziness(Fuzziness.Auto)
                    .Lenient()
                    .FuzzyRewrite(MultiTermQueryRewrite.TopTermsBlendedFreqs(10))
                    )

                .Match(b => b
                    .Field(f => f.ManufacturerName)
                    .Query(query)
                    .Fuzziness(Fuzziness.Auto)
                    .PrefixLength(2)
                    .Fuzziness(Fuzziness.Auto)
                    .Lenient()
                    .FuzzyRewrite(MultiTermQueryRewrite.TopTermsBlendedFreqs(10))
                    )

                .Match(c => c
                    .Field(f => f.MasterAttributes)
                    .Query(query)
                    .Fuzziness(Fuzziness.Auto)
                    .PrefixLength(2)
                    .Fuzziness(Fuzziness.Auto)
                    .Lenient()
                    .FuzzyRewrite(MultiTermQueryRewrite.TopTermsBlendedFreqs(10))
                    )
                )
            );
            Console.WriteLine(searchResponse.Hits.Count());

            foreach (var hit in searchResponse.Documents)

            {

                Console.WriteLine(hit.MasterProductId);

            }*/
    }
1
"NEST doesn't like replacing a field "f.something" with a variable" - can you elaborate on what you mean? Are you looking for something like .Suffix(...): elastic.co/guide/en/elasticsearch/client/net-api/current/…Russ Cam
@RussCam thanks for responding Russ. Essentially, I want to search on a certain field in NEST depending on an inputted variable. So "MasterProductId" would be input and the query would search on that field. However I can't find a way to do this, it seems fields to search all need to be hard coded. Is this the case? Thanks again :)RMass
The Field type has an implicit conversion from string, so you can pass the string input for the field. Note that strings are taken verbatim and not cased as expressions or PropertyInfo are, so if you're rolling with the defaults, the string value will need to be camel-cased e.g. masterProductName.Russ Cam
@RussCam excellent thank you that worked, now I just need to fix how I can't query numbers for some reason hahaRMass

1 Answers

0
votes

@RussCam answered the question in the comments above. To reiterate, pass only strings in as parameters to fields, not other datatypes. Camelcase is also necessary for this to work.