Elasticsearch - Nest - 特定搜索
Elasticsearch - Nest - specific search
关于问题:
我有一个看起来像这样的索引:
{
"_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))
)
);
我想将其扩展到只有 return 个结果 "category" 等于 InfoPage。
根据您的索引结构,我假设您有一个 Page
class ,因此您可以找到具有该类别的页面,如下所示:
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")));
现在,如果您 SearchReportDocument
class 具有与 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")));
字段:"category"
值:"infopage"
示例:
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)
关于问题:
我有一个看起来像这样的索引:
{
"_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))
)
);
我想将其扩展到只有 return 个结果 "category" 等于 InfoPage。
根据您的索引结构,我假设您有一个 Page
class ,因此您可以找到具有该类别的页面,如下所示:
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")));
现在,如果您 SearchReportDocument
class 具有与 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")));
字段:"category"
值:"infopage"
示例:
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)