[英]Can search Elasticsearch 5.0 (Windows) using simple query, but prefixed with field name fails (search by example)
我正在嘗試從ElasticSearch中獲取“通過示例搜索”功能。 我有許多具有字段的對象,例如名稱,描述,對象ID等。我想執行一個搜索,例如“ name = 123”和“ description = ABC”
對應:
{
"settings": {
"number_of_replicas": 1,
"number_of_shards": 3,
"refresh_interval": "5s",
"index.mapping.total_fields.limit": "500"
},
"mappings": {
"CFS": {
"_routing": {
"required": true
},
"properties": {
"objectId": {
"store": true,
"type": "keyword",
"index": "not_analyzed"
},
"name": {
"type": "text",
"analyzer": "standard"
},
"numberOfUpdates": {
"type": "long"
},
"dateCreated": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"lastModified": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis",
"index": "not_analyzed"
}
}
}
}
}
嘗試不使用字段名稱的非常簡單的搜索,即可得出正確的結果:
請求:GET http:// localhost:9200 / repository / CFS / _search?routing = CFS&q = CFS3
返回值:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.7831944,
"hits": [
{
"_index": "repository",
"_type": "CFS",
"_id": "589a9a62-1e4d-4545-baf9-9cc7bf4d582a",
"_score": 0.7831944,
"_routing": "CFS",
"_source": {
"doc": {
"name": "CFS3",
"description": "CFS3Desc",
"objectId": "589a9a62-1e4d-4545-baf9-9cc7bf4d582a",
"lastModified": 1480524291530,
"dateCreated": 1480524291530
}
}
}
]
}
}
但是嘗試使用字段名作為前綴會失敗(這會發生在所有字段上,例如objectId):
請求:GET http:// localhost:9200 / repository / CFS / _search?routing = CFS&q = name:CFS3
返回值:
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
最終我想做些類似的事情:
{
"bool" : {
"must" : [
{
"wildcard" : {
"name" : {
"wildcard" : "*CFS3*",
"boost" : 1.0
}
}
},
{
"wildcard" : {
"description" : {
"wildcard" : "*CFS3Desc*",
"boost" : 1.0
}
}
}
]
}
}
可能相關嗎? 當我嘗試使用“ multi_match”執行此操作時,我必須在字段名前加上通配符,例如
POST http://localhost:9200/repository/CFS/_search?routing=CFS
{
"query": {
"multi_match" : {
"query" : "CFS3",
"fields" : ["*name"]
}
}
}
如果我不加前綴,它什么也找不到。 我花了2天的時間搜索StackOverflow和ElasticSearch文檔。 但是似乎沒有提到這些問題。 關於搜索字詞的通配符有很多,甚至在字段名之后提到了通配符,但是在字段名之前沒有其他內容。 我需要通過指定通配符來處理字段名稱中缺少的哪些信息?
我認為映射中的字段類型是正確的。 我指定了一個分析器。
我找到了答案:(我一直熱衷於使用“ upserts”,以避免必須檢查對象是否已經存在,從而保持較高的性能。
如您在此鏈接上看到的https://www.elastic.co/guide/en/elasticsearch/guide/current/partial-updates.html和這一個https://www.elastic.co/guide/en/elasticsearch/ reference / current / docs-update.html在調用Update REST調用時,您將有效負載指定為:
{
"doc" : {
"tags" : [ "testing" ],
"views": 0
}
}
當使用Java客戶端實現等效功能時,我沒有完全遵循示例。 而不是建議:
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("index");
updateRequest.type("type");
updateRequest.id("1");
updateRequest.doc(jsonBuilder()
.startObject()
.field("gender", "male")
.endObject());
client.update(updateRequest).get();
我已經實現了:
JsonObject state = extrapolateStateFromEvent( event );
JsonObject doc = new JsonObject();
doc.add( "doc", state );
UpdateRequest updateRequest = new UpdateRequest( indexName, event.getEntity().getType(), event.getEntity().getObjectId() );
updateRequest.routing( event.getEntity().getType() );
updateRequest.doc( doc.toString() );
updateRequest.upsert( doc.toString() );
UpdateResponse response = client.update( updateRequest ).get();
我用“ doc”對象包裝了有效載荷/“狀態”,以為需要。
但這對我與數據交互的方式產生了很大的影響,並且絲毫沒有警告我。 我想我是不小心創建了一個嵌套對象。 雖然我想知道為什么它會這么大地影響搜索API?
如何改善呢? 也許映射可以默認為禁止嵌套對象? 還是可能存在某種程序員可以執行的驗證?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.