ElasticSearch NEST DSL 查询 跨字段查询
ElasticSearch NEST DSL Query Cross Fields Query
我正在尝试将以下 ElasticSearch DSL 查询转换为 NEST,但似乎有些地方不正确。
这是我的 DSL 查询:
{
"query": {
"multi_match": {
"query": "AJ",
"type": "cross_fields",
"fields": ["name", "shortname", "shortname2", "number"],
"operator": "and"
}
}
}
我有一个 POCO class。我想获得如下所示的列表结果:
public class SearchDto
{
public Guid Id { get; set; }
public string Number { get; set; }
public string Name { get; set; }
public string ShortName2 { get; set; }
public string ShortName1 { get; set; }
}
由于是跨字段查询,我创建了这样的字段:
Fields nameField = Infer.Field<SearchDto>(p => p.Name);
var shortName2 = Infer.Field<SearchDto>(p => p.ShortName2);
var shortName1 = Infer.Field<SearchDto>(p => p.ShortName1);
var number = Infer.Field<SearchDto>(p => p.Number);
这是我的 NEST 查询:
var searchRequest = new SearchRequest() {
Query = new MultiMatchQuery() {
Fields = nameField
.And(shortName2)
.And(shortName1)
.And(number),
Query = value,
Operator = Operator.And,
Type = TextQueryType.CrossFields
}
}
当我为搜索请求获取 Json 字符串时,它仅使用以下内容打印“{}”:
var json = _client.RequestResponseSerializer.SerializeToString(searchRequest);
它还将“{}”作为请求正文发布
我还尝试了以下方法:
var response = _client.Search <List<SearchDto>> (s => s
.Size(500)
.Index("mysearchIndex")
.Query(q => q
.MultiMatch(m => m
.Type(TextQueryType.CrossFields)
.Fields(nameField)
.Fields(shortName1)
.Fields(shortName2)
.Fields(number)
.Operator(Operator.And)
.Query(value)
)
));
以上查询仅将“{"size" : 500}" 发送到我的 elasticsearch 端点
有人可以建议我做错了什么 and/or 建议使用 NEST 处理我的查询的更好方法吗?由于某种原因,它甚至没有构建完整的查询。
嵌套查询条件较少。如果确定输入为 null 或空字符串,则请求中将省略查询。
在您的查询中“value”为空或 null ,则生成的查询将为“{}”。
如果您打算搜索空值。然后您需要将单个查询标记为 verbatim
An individual query can be marked as verbatim in order take effect; a verbatim query will be serialized and sent in the request to Elasticsearch, bypassing NEST’s conditionless checks.
例子
var searchRequest = new SearchRequest()
{
Query = new MultiMatchQuery()
{
Fields = nameField
.And(shortName2)
.And(shortName1)
.And(number),
Query = "",
Operator = Operator.And,
Type = TextQueryType.CrossFields,
IsVerbatim=true ---> note flag
}
};
对应查询
{"query":{"multi_match":{"fields":["name","shortName2","shortName1","number"],"operator":"and","query":"","type":"cross_fields"}}}
我正在尝试将以下 ElasticSearch DSL 查询转换为 NEST,但似乎有些地方不正确。 这是我的 DSL 查询:
{
"query": {
"multi_match": {
"query": "AJ",
"type": "cross_fields",
"fields": ["name", "shortname", "shortname2", "number"],
"operator": "and"
}
}
}
我有一个 POCO class。我想获得如下所示的列表结果:
public class SearchDto
{
public Guid Id { get; set; }
public string Number { get; set; }
public string Name { get; set; }
public string ShortName2 { get; set; }
public string ShortName1 { get; set; }
}
由于是跨字段查询,我创建了这样的字段:
Fields nameField = Infer.Field<SearchDto>(p => p.Name);
var shortName2 = Infer.Field<SearchDto>(p => p.ShortName2);
var shortName1 = Infer.Field<SearchDto>(p => p.ShortName1);
var number = Infer.Field<SearchDto>(p => p.Number);
这是我的 NEST 查询:
var searchRequest = new SearchRequest() {
Query = new MultiMatchQuery() {
Fields = nameField
.And(shortName2)
.And(shortName1)
.And(number),
Query = value,
Operator = Operator.And,
Type = TextQueryType.CrossFields
}
}
当我为搜索请求获取 Json 字符串时,它仅使用以下内容打印“{}”:
var json = _client.RequestResponseSerializer.SerializeToString(searchRequest);
它还将“{}”作为请求正文发布
我还尝试了以下方法:
var response = _client.Search <List<SearchDto>> (s => s
.Size(500)
.Index("mysearchIndex")
.Query(q => q
.MultiMatch(m => m
.Type(TextQueryType.CrossFields)
.Fields(nameField)
.Fields(shortName1)
.Fields(shortName2)
.Fields(number)
.Operator(Operator.And)
.Query(value)
)
));
以上查询仅将“{"size" : 500}" 发送到我的 elasticsearch 端点
有人可以建议我做错了什么 and/or 建议使用 NEST 处理我的查询的更好方法吗?由于某种原因,它甚至没有构建完整的查询。
嵌套查询条件较少。如果确定输入为 null 或空字符串,则请求中将省略查询。
在您的查询中“value”为空或 null ,则生成的查询将为“{}”。
如果您打算搜索空值。然后您需要将单个查询标记为 verbatim
An individual query can be marked as verbatim in order take effect; a verbatim query will be serialized and sent in the request to Elasticsearch, bypassing NEST’s conditionless checks.
例子
var searchRequest = new SearchRequest()
{
Query = new MultiMatchQuery()
{
Fields = nameField
.And(shortName2)
.And(shortName1)
.And(number),
Query = "",
Operator = Operator.And,
Type = TextQueryType.CrossFields,
IsVerbatim=true ---> note flag
}
};
对应查询
{"query":{"multi_match":{"fields":["name","shortName2","shortName1","number"],"operator":"and","query":"","type":"cross_fields"}}}