elasticsearch nest在搜索之后没有提供正确的响应

pw9qyyiw  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(0)|浏览(188)

我正在使用elastic search 7.8.2和nest 7.8.2。我已经成功地创建了一个索引,并且能够将文档推送到索引中。我需要一些搜索帮助,因为当我试图搜索我没有得到所需的结果。
下面是索引创建。

await searchClient.Indices.CreateAsync(index, c => c
              .Settings(s => s
                  .Analysis(a => a
                      //.AddSearchAnalyzer()
                      .Analyzers(an => an
                        .Custom("autocomplete", cu => cu
                            .Tokenizer("standard")
                            .Filters("lowercase", "substring")
                        )
                        .Custom("autocomplete_search", cu =>
                            cu.Tokenizer("lowercase")
                            .Filters("lowercase", "substring")
                        )
                        .Custom("synonymanalyzer", cu =>
                            cu.Tokenizer("standard").Filters("synonyms")
                        )
                    ).TokenFilters(tf => tf.Synonym("synonyms", sy => sy.Tokenizer("standard").SynonymsPath("synonym.txt")))
                    .Tokenizers(t => t
                        .EdgeNGram("autocomplete", e => e
                            .MinGram(2)
                            .MaxGram(50)
                            .TokenChars(TokenChar.Letter, TokenChar.Digit)
                        )
                    )
                  )
              )
          //.Map<T>(m => m.AutoMap()));
          .Map<Products>(m => m.AutoMap()))

产品.cs

public class Products
        {
             public int ProductID {get;set;}
             [Text(Analyzer = SearchHelper.IndexAnalyzerName)]       
             public string Name { get; set; }
             [Keyword]        
             public string NameNotAnalyzed { get { return Name; } }
             [Text(Analyzer = SearchHelper.IndexAnalyzerName)]
             public string BrandName { get; set; }
             [Keyword]
             public string BrandNameNotAnalyzed
             {
               get { return BrandName; }
             }
        }

示例json

{
             "ProductID": 1,
             "Name": "100Poly Criss-Cross Tunic",
             "NameNotAnalyzed": "100Poly Criss-Cross Tunic",
             "BrandName": "Rocky",
             "BrandNameNotAnalyzed" : "Rocky"
           },
           {
             "ProductID": 2,
             "Name": "Men's 100% Polyester Pant",
             "NameNotAnalyzed": "Men's 100% Polyester Pant",
             "BrandName": "Rocky",
             "BrandNameNotAnalyzed" : "Rocky"
           },
           {
             "ProductID": 3,
             "Name": "Men's Black 100% Polyester Pant",
             "NameNotAnalyzed": "Men's Black 100% Polyester Pant",
             "BrandName": "Rocky",
             "BrandNameNotAnalyzed": "Rocky"
           }

搜索查询:

var searchQuery = default(QueryContainer);
            searchQuery = fuzzyText.GetSearchQuery();

          var fuzzySearchResult = elasticClient.Search<ProductView>(s => s
                .Index(ProductIndex)
                 .Query(f =>
                 {
                     return searchQuery;
                 })                
            );

            public static QueryContainer GetSearchQuery(this string searchText)
    {
        var keys = new string[] { "Name", "Brand"};

        var queryContainer = default(QueryContainer);
        if (!string.IsNullOrEmpty(searchText))
        {
            string[] searchTerms = searchText.Split(' ');

            foreach (var key in keys)
            {
                var subQueryContainer = default(QueryContainer);
                foreach (var term in searchTerms)
                {
                    switch (key)
                    {
                        case "Name":
                            subQueryContainer |= Query<Products>.Match(b => b.Field(f => f.Name).Query(term).Boost(50));                               

                            break;                            
                        case "Brand":
                            subQueryContainer |= Query<ProductView>.Match(b => b.Field(f => f.BrandName).Query(term).Boost(3));
                            break;

                    }                        
                }
                queryContainer |= subQueryContainer;

            }
        }
        return queryContainer;
    }

现在,当我试图执行搜索操作时,它没有给出所需的结果。就像我试图寻找

"men" or "mens"

它应该给我2和3个产品的结果,因为它返回零文件,当我搜索

"Poly"

这三种产品都应该还给我,能不能有人告诉我哪里出了问题。任何帮助都非常感谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题