在.NEST elasticsearch查询中将.keyword附加到fieldname

时间:2017-09-01 19:20:25

标签: elasticsearch nest elasticsearch-5

想象一下,我的查询为:

.Query(query =>
            query.Bool(b => b.Must(m =>     
                m.Wildcard(w => w.Field(f => f.userName).Value(string.Format("*{0}*", searchModel.username).Suffix("keyword")))
)));

输出查询(来自DeubgInformation)将如下:

{
    "query": {
        "bool": {
            "must": [{
                    "wildcard": {
                        "userName": "*alex*"
                    }
                }
            ],
            "must_not": [],
            "should": []
        }
    }
}

这怎么不行。它需要在用户名结尾附加“.keyword”。下面的查询有效,但我无法通过NEST生成它:

{
    "query": {
        "bool": {
            "must": [{
                    "wildcard": {
                        "userName.keyword": "*alex*"
                    }
                }
            ],
            "must_not": [],
            "should": []
        }
    }
}

任何想法如何让NEST在字段名称的末尾添加“.keyword”? (当然是流利的,否则w.Field("userName.keyword")有效)

1 个答案:

答案 0 :(得分:2)

Suffix()调用需要是成员访问表达式的一部分

.Query(query => query
    .Bool(b => b
        .Must(m => m
            .Wildcard(w => w
                .Field(f => f.userName.Suffix("keyword"))
                .Value(string.Format("*{0}*", searchModel.username)
            )
        )
    )
));