如图:
请教一个问题,这种情况用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 将上面关系表达出来也可以
可以参考下
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)))
);