首页 新闻 会员 周边 捐助

Elasticsearch自动降维模糊检索

0
悬赏园豆:20 [待解决问题]

如图:
请教一个问题,这种情况用Elasticsearch如何实现最好?
根据查询条件,按最低级street如果查询不到,自动向上一级查询;使用一个查询完成,类似自动降维模糊检索。

问题补充:

{
"query": {
"bool": {
"must": [
{
"term": {
"to_province": "河南省"
}
},
{
"term": {
"to_city": "信阳市"
}
},
{
"term": {
"to_district": "新县"
}
},
{
"bool": {
"should": [
{
"term": {
"from_city": "湖北省"
}
},
{
"bool": {
"must_not": {
"exists": {
"field": "from_city"
}
}
}
}
]
}
},
{
"bool": {
"should": [
{
"term": {
"from_city": "武汉市"
}
},
{
"bool": {
"must_not": {
"exists": {
"field": "from_city"
}
}
}
}
]
}
},
{
"bool": {
"should": [
{
"term": {
"from_city": "江岸区"
}
},
{
"bool": {
"must_not": {
"exists": {
"field": "from_city"
}
}
}
}
]
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 10,
"sort": [],
"aggs": {}
}

能用Elasticsearch.Net 将上面关系表达出来也可以

三人成虎的主页 三人成虎 | 初学一级 | 园豆:150
提问于:2018-11-16 09:11
< >
分享
所有回答(1)
0

可以参考下

var mustQuerys = new List<Func<QueryContainerDescriptor<数据所对应的实体>, QueryContainer>>();
var shouldQuerys = new List<Func<QueryContainerDescriptor<数据所对应的实体>, QueryContainer>>();
// Term => must
mustQuerys.Add(mt => mt.Term(tm => tm.Field(fd => fd.from_province).Value("河南省")));
mustQuerys.Add(mt => mt.Term(tm => tm.Field(fd => fd.from_city).Value("信阳市")));
mustQuerys.Add(mt => mt.Term(tm => tm.Field(fd => fd.from_district).Value("新县")));
// Term => must not
mustQuerys.Add(mt => !mt.Exists(tm => tm.Field(fd => fd.from_city)));
// Term => should
shouldQuerys.Add(mt => mt.Term(tm => tm.Field(fd => fd.from_city).Value("武汉市")));
Func<SortDescriptor<BaseQuestion>, IPromise<IList<ISort>>> sortCondition = sort =>
{
    // 根据分值排序
    sort.Descending(SortSpecialField.Score);
    sort.Descending(s => s.UpdateTime);
    return sort;
};
var result = client.Search<数据所对应的实体>
 (
     s => s
     .Index(indexName)
    .TrackTotalHits()
    .From((pageIndex - 1) * pageSize)
    .Size(pageSize)
    .Sort(sortCondition)
    .Query(s => s.Bool(b => b.Must(mustQuerys).Should(shouldQuerys)))
);
大志若愚 | 园豆:2138 (老鸟四级) | 2020-01-13 19:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册