【问题标题】:Elasticsearch - Nest - specific searchElasticsearch - Nest - 特定搜索
【发布时间】:2015-01-30 16:06:13
【问题描述】:

关于问题: Elasticsearch/Nest - using MatchPhrase with OnFieldsWithBoost

我有一个看起来像这样的索引:

{
    "_index": "myIndex",
    "_type": "Page",
    "_id": "119",
    "_score": 0.104187615,
    "_source": {
        "dataBaseId": 119,
        "category": "InfoPage",
        "type": "Page",
        "metaTitle": "myMeta",
        "metaDescription": "Description",
        "rawText": "my search text"
    }
}

我的代码如下所示:

var result = ElasticClient.Search<SearchReportDocument>(s => s
            .Index("myIndex")
            .Type("Page")
            .Size(10)
            .Query(q =>
                q.MultiMatch(m => m.OnFieldsWithBoost(f => f.Add(b => b.MetaTitle, 5).Add(b => b.RawText, 1)).Type(TextQueryType.PhrasePrefix).Query(searchQuery))
                )
            );

我想将其扩展为仅返回“类别”等于 InfoPage 的结果。

【问题讨论】:

    标签: c# elasticsearch nest


    【解决方案1】:

    使用TermQuery

    字段:“类别”

    值:“信息页”


    示例:

     List<QueryContainer> shoudQuery = new List<QueryContainer>();
    List<QueryContainer> mustQuery = new List<QueryContainer>();
    
    
    shoudQuery.Add(new MultiMatchQuery()
        {
            //your Query
        });
    mustQuery.Add(new termQuery()
        {
           Field = "category",
           value= "infopage",    
        });
    
    
    QueryContainer queryContainer = new BoolQuery
            {
                 Should = shoudQuery.ToArray(),
                 Must = mustQuery.ToArray(),
                 MinimumShouldMatch = 1,
    
             };
    
    
    var result = Client.Search(s => s.Size(resultSize).Query(q => queryContainer)
    

    【讨论】:

      【解决方案2】:

      根据您的索引结构,我假设您有一个 Page 类,因此您可以找到具有该类别的页面,如下所示:

       var result= client.Search<Page>(sd => sd.Index("myIndex")
                                        .From(0)
                                        .Size(10).Query(q =>
                                        q.MultiMatch( m =>
                                        m.OnFieldsWithBoost(f => f.Add(b => b.MetaTitle, 5).Add(b => b.RawText, 1))
                                        .Type(TextQueryType.PhrasePrefix)
                                        .Query(searchQuery))
                                        &&  q.Term("category", "infopage")));
      

      现在,如果你SearchReportDocumentclass 有Page 相同的字段,你也可以这样找到页面:

        var result= client.Search<SearchReportDocument>(sd => sd.Index("myIndex").Type("Page")
                                        .From(0)
                                        .Size(10).Query(q =>
                                        q.MultiMatch( m =>
                                        m.OnFieldsWithBoost(f => f.Add(b => b.MetaTitle, 5).Add(b => b.RawText, 1))
                                        .Type(TextQueryType.PhrasePrefix)
                                        .Query(searchQuery))
                                        &&  q.Term("category", "infopage")));
      

      【讨论】:

      • 感谢您的意见。我有一个 SearchReportDocument 类,但我的问题是我仍想保留其余的搜索并扩展它。所以这部分应该还是搜索的一部分q =&gt; q.MultiMatch(m =&gt; m.OnFieldsWithBoost(f =&gt; f.Add(b =&gt; b.MetaTitle, 5).Add(b =&gt; b.RawText, 1)).Type(TextQueryType.PhrasePrefix).Query(searchQuery))
      • 我刚刚尝试使用 .Query(q =&gt; q.Term(p =&gt; p.Category, "InfoPage"))); 进行搜索,但没有从索引中得到任何结果。
      • 我尝试添加过滤器。如果我在字段类别上使用 Exists,我会得到结果。但是,如果我在过滤器中添加一个术语,例如Filter&lt;SearchReportDocument&gt;.Term("category", "InfoPage");,那么搜索将找不到结果。
      • 对不起,起初我不太明白你想做什么。我已经更新了我的答案。现在希望得到帮助
      • 谢谢。我发现了一件事,搜索条件是小写非常重要。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-31
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 2016-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多