我有一个以下模型类,该模型类用于在Elasticsearch中为文档建立索引。
public class SearchModel
{
public string Id { get; set; }
public string Category { get; set; }
public string CategoryId { get; set; }
public string SubCategory { get; set; }
public List<SearchItem> SearchItems = new List<SearchItem>();
}
public class SearchItem
{
public string ItemName { get; set; }
public string ItemValue { get; set; }
}
我想在Category,SubCategory和ItemValue中进行搜索,并返回所有SearchModel类。我当前的搜索方法是
var response = await _elasticClient.SearchAsync<SearchModel>(
s =>
//s.Query(q => q
// .Match(m => m
// .Field(f => f.Category)
// .Field(f => f.SubCategory)
// .Field(f => f.SearchItems)
// .Query(String.Format("*{0}*", query))
// )
//));
s.Query(q => q.QueryString(d => d.Query(String.Format("*{0}*", query))))
.From((page - 1) * pageSize)
.Size(pageSize));
示例搜索结果是
{
"aggregations": {},
"aggs": {},
"documents": [
{
"searchItems": [
{
"itemName": "SerialNumber",
"itemValue": "DP5996"
},
{
"itemName": "SkuNumber",
"itemValue": "SKU5996"
},
{
"itemName": "WorkOrderNumber",
"itemValue": "WO-DP5996"
},
{
"itemName": "ComponentId",
"itemValue": "5c5d5830468ccf615cf80244"
},
{
"itemName": "ComponentName",
"itemValue": "Pressure Gauge"
},
{
"itemName": "bandColor",
"itemValue": "YELLOW"
},
{
"itemName": "pressure",
"itemValue": "15599"
},
{
"itemName": "pressureUOM",
"itemValue": "PSI"
},
{
"itemName": "size",
"itemValue": "2 3/8"
},
{
"itemName": "sizeUOM",
"itemValue": "FT_IN"
},
{
"itemName": "boxInnerDia",
"itemValue": "16"
},
{
"itemName": "boxInnerDiaUOM",
"itemValue": "IN"
},
{
"itemName": "boxOuterDia",
"itemValue": "18"
},
{
"itemName": "boxOuterDiaUOM",
"itemValue": "IN"
},
{
"itemName": "classColor",
"itemValue": "RED"
},
{
"itemName": "grade",
"itemValue": "E-75 EU"
},
{
"itemName": "hardBanding",
"itemValue": ""
},
{
"itemName": "hbVendor",
"itemValue": ""
},
{
"itemName": "ipc",
"itemValue": ""
},
{
"itemName": "makeUpLength",
"itemValue": "0"
},
{
"itemName": "makeUpLengthUOM",
"itemValue": ""
},
{
"itemName": "manufacturer",
"itemValue": "American Oil drilling Inc."
},
{
"itemName": "pipeInnerDia",
"itemValue": "0"
},
{
"itemName": "pipeInnerDiaUOM",
"itemValue": "IN"
},
{
"itemName": "pipeOuterDia",
"itemValue": "0"
},
{
"itemName": "pipeOuterDiaUOM",
"itemValue": "IN"
},
{
"itemName": "pipeLength",
"itemValue": "5"
},
{
"itemName": "tubularType",
"itemValue": ""
},
{
"itemName": "weight",
"itemValue": "50"
},
{
"itemName": "weightUOM",
"itemValue": "KG"
}
],
"id": "5c65772d01577532bccfeba6",
"category": "Asset",
"categoryId": "5c65772d01577532bccfeba6",
"subCategory": "gauge"
}
],
"fields": [
null
],
"hits": [
{
"explanation": null,
"fields": null,
"highlights": {},
"id": "5c65772d01577532bccfeba6",
"index": "blackgold",
"innerHits": {},
"matchedQueries": [],
"nested": null,
"parent": null,
"routing": null,
"score": 1,
"sorts": [],
"source": {
"searchItems": [
{
"itemName": "SerialNumber",
"itemValue": "DP5996"
},
{
"itemName": "SkuNumber",
"itemValue": "SKU5996"
},
{
"itemName": "WorkOrderNumber",
"itemValue": "WO-DP5996"
},
{
"itemName": "ComponentId",
"itemValue": "5c5d5830468ccf615cf80244"
},
{
"itemName": "ComponentName",
"itemValue": "Pressure Gauge"
},
{
"itemName": "bandColor",
"itemValue": "YELLOW"
},
{
"itemName": "pressure",
"itemValue": "15599"
},
{
"itemName": "pressureUOM",
"itemValue": "PSI"
},
{
"itemName": "size",
"itemValue": "2 3/8"
},
{
"itemName": "sizeUOM",
"itemValue": "FT_IN"
},
{
"itemName": "boxInnerDia",
"itemValue": "16"
},
{
"itemName": "boxInnerDiaUOM",
"itemValue": "IN"
},
{
"itemName": "boxOuterDia",
"itemValue": "18"
},
{
"itemName": "boxOuterDiaUOM",
"itemValue": "IN"
},
{
"itemName": "classColor",
"itemValue": "RED"
},
{
"itemName": "grade",
"itemValue": "E-75 EU"
},
{
"itemName": "hardBanding",
"itemValue": ""
},
{
"itemName": "hbVendor",
"itemValue": ""
},
{
"itemName": "ipc",
"itemValue": ""
},
{
"itemName": "makeUpLength",
"itemValue": "0"
},
{
"itemName": "makeUpLengthUOM",
"itemValue": ""
},
{
"itemName": "manufacturer",
"itemValue": "American Oil drilling Inc."
},
{
"itemName": "pipeInnerDia",
"itemValue": "0"
},
{
"itemName": "pipeInnerDiaUOM",
"itemValue": "IN"
},
{
"itemName": "pipeOuterDia",
"itemValue": "0"
},
{
"itemName": "pipeOuterDiaUOM",
"itemValue": "IN"
},
{
"itemName": "pipeLength",
"itemValue": "5"
},
{
"itemName": "tubularType",
"itemValue": ""
},
{
"itemName": "weight",
"itemValue": "50"
},
{
"itemName": "weightUOM",
"itemValue": "KG"
}
],
"id": "5c65772d01577532bccfeba6",
"category": "Asset",
"categoryId": "5c65772d01577532bccfeba6",
"subCategory": "gauge"
},
"type": "searchmodel",
"version": null
}
],
"hitsMetadata": {
"hits": [
{
"explanation": null,
"fields": null,
"highlights": {},
"id": "5c65772d01577532bccfeba6",
"index": "blackgold",
"innerHits": {},
"matchedQueries": [],
"nested": null,
"parent": null,
"routing": null,
"score": 1,
"sorts": [],
"source": {
"searchItems": [
{
"itemName": "SerialNumber",
"itemValue": "DP5996"
},
{
"itemName": "SkuNumber",
"itemValue": "SKU5996"
},
{
"itemName": "WorkOrderNumber",
"itemValue": "WO-DP5996"
},
{
"itemName": "ComponentId",
"itemValue": "5c5d5830468ccf615cf80244"
},
{
"itemName": "ComponentName",
"itemValue": "Pressure Gauge"
},
{
"itemName": "bandColor",
"itemValue": "YELLOW"
},
{
"itemName": "pressure",
"itemValue": "15599"
},
{
"itemName": "pressureUOM",
"itemValue": "PSI"
},
{
"itemName": "size",
"itemValue": "2 3/8"
},
{
"itemName": "sizeUOM",
"itemValue": "FT_IN"
},
{
"itemName": "boxInnerDia",
"itemValue": "16"
},
{
"itemName": "boxInnerDiaUOM",
"itemValue": "IN"
},
{
"itemName": "boxOuterDia",
"itemValue": "18"
},
{
"itemName": "boxOuterDiaUOM",
"itemValue": "IN"
},
{
"itemName": "classColor",
"itemValue": "RED"
},
{
"itemName": "grade",
"itemValue": "E-75 EU"
},
{
"itemName": "hardBanding",
"itemValue": ""
},
{
"itemName": "hbVendor",
"itemValue": ""
},
{
"itemName": "ipc",
"itemValue": ""
},
{
"itemName": "makeUpLength",
"itemValue": "0"
},
{
"itemName": "makeUpLengthUOM",
"itemValue": ""
},
{
"itemName": "manufacturer",
"itemValue": "American Oil drilling Inc."
},
{
"itemName": "pipeInnerDia",
"itemValue": "0"
},
{
"itemName": "pipeInnerDiaUOM",
"itemValue": "IN"
},
{
"itemName": "pipeOuterDia",
"itemValue": "0"
},
{
"itemName": "pipeOuterDiaUOM",
"itemValue": "IN"
},
{
"itemName": "pipeLength",
"itemValue": "5"
},
{
"itemName": "tubularType",
"itemValue": ""
},
{
"itemName": "weight",
"itemValue": "50"
},
{
"itemName": "weightUOM",
"itemValue": "KG"
}
],
"id": "5c65772d01577532bccfeba6",
"category": "Asset",
"categoryId": "5c65772d01577532bccfeba6",
"subCategory": "gauge"
},
"type": "searchmodel",
"version": null
}
],
"maxScore": 1,
"total": 1
},
"maxScore": 1,
"numberOfReducePhases": 0,
"profile": null,
"scrollId": null,
"shards": {
"failed": 0,
"failures": [],
"successful": 5,
"total": 5
},
"suggest": {},
"terminatedEarly": false,
"timedOut": false,
"took": 20,
"total": 1,
"apiCall": {
"auditTrail": [
{
"ended": "2019-02-20T09:46:31.4591331Z",
"event": 10,
"exception": null,
"node": {
"clientNode": false,
"deadUntil": "0001-01-01T00:00:00",
"failedAttempts": 0,
"holdsData": true,
"httpEnabled": true,
"id": null,
"ingestEnabled": false,
"isAlive": true,
"isResurrected": false,
"masterEligible": true,
"masterOnlyNode": false,
"name": null,
"settings": {},
"uri": "http://localhost:9200/"
},
"path": "blackgold/searchmodel/_search?typed_keys=true",
"started": "2019-02-20T09:46:31.1551544Z"
}
],
"debugInformation": "Successful low level call on POST: /blackgold/searchmodel/_search?typed_keys=true\r\n# Audit trail of this API call:\r\n - [1] HealthyResponse: Node: http://localhost:9200/ Took: 00:00:00.3039787\r\n# Request:\r\n<Request stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>\r\n# Response:\r\n<Response stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>\r\n",
"deprecationWarnings": [],
"httpMethod": 1,
"httpStatusCode": 200,
"originalException": null,
"requestBodyInBytes": null,
"responseBodyInBytes": null,
"responseMimeType": "application/json",
"serverError": null,
"success": true,
"successOrKnownError": true,
"uri": "http://localhost:9200/blackgold/searchmodel/_search?typed_keys=true"
},
"debugInformation": "Valid NEST response built from a successful low level call on POST: /blackgold/searchmodel/_search?typed_keys=true\r\n# Audit trail of this API call:\r\n - [1] HealthyResponse: Node: http://localhost:9200/ Took: 00:00:00.3039787\r\n# Request:\r\n<Request stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>\r\n# Response:\r\n<Response stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>\r\n",
"isValid": true,
"originalException": null,
"serverError": null
}
已更新
在c#中,索引是通过以下代码完成的,还需要避免包含“ 5c65772d07894532bccfeba”之类的ID字段
var searchModel = new SearchModel();
var searchItems = new List<SearchItem>();
searchModel.Id = "5c65772d07894532bccfeba";
searchModel.Category = "Asset";
searchModel.CategoryId = "5c65772d07894532bccfeba";
searchModel.SubCategory = "test category";
var item1 = new SearchItem
{
ItemName = "SerialNumber",
ItemValue = "DP123"
};
searchItems.Add(item1);
var item2 = new SearchItem
{
ItemName = "SkuNumber",
ItemValue = "test"
};
searchItems.Add(item2);
var item3 = new SearchItem
{
ItemName = "WorkOrderNumber",
ItemValue = "TestOrder123"
};
searchItems.Add(item3);
var item4 = new SearchItem
{
ItemName = "ComponentId",
ItemValue = "5c65772d01577532bccfeba"
};
searchItems.Add(item4);
var item5 = new SearchItem
{
ItemName = "ComponentName",
ItemValue = "TestComponent"
};
searchItems.Add(item5);
searchModel.SearchItems = searchItems;
var response = await _elasticClient.IndexDocumentAsync(searchModel);
当我使用查询字符串作为SerialNumber中包含的'dp'时得到此结果。我想避免在id字段中搜索“ dp”。 我尝试了注释的代码,但未返回任何数据。如何解决此问题?