NestedQuery in BoolQuery using Java API



我试图了解EL中的查询是如何工作的,老实说有很多问题。

这是我带有属性的文档:

{"statusError":null,
"fileHash":"da8620bad21685c5e385fb1b43a7e744",
"project":{"id":7687},
"error":null,
"ocrFile64":"JVBERi0xL...."
"isInElastic":false,
"originalName":"test.pdf",
"lastUpdated":"2018-10-18T12:47:59Z",
"dateCreated":"2018-10-18T12:40:19Z",
"ocrAvailable":true,
"attachment":{"date":"2018-07-05T07:20:06Z",
"content_type":"application/pdf",
"language":"en","title":"Untitled",
"content":"blah blah blahblahblahblahblah"
"company":{"id":1},
"id":25850,
"tag":[{"id":3},{"id":2}],
"contentType":"application/pdf",
"imageHash":"",
"label":null,
"size":47680,
"user":{"id":7563},
"md5":[100,97,56,54,50,48,98,97,100,50,49,54,56,53,99,53,101,51,56,53,102,98,49,98,52,51,97,55,101,55,52,52],
"status":{"name":"CLASSIFIED"}}

EL 安装了采集模块插件,用于上传文件内容。实际上,管道是ocrFile64,文件的内容位于内容属性中。

我想做的很简单,我想做一个这样的查询:给我所有文档,其中 originalName 包含"test",user.id 等于 1,内容包含"blah"。

到目前为止,我已经写了这个:

client = new RestHighLevelClient(builder)
SearchRequest searchRequest = new SearchRequest("testEL")
searchRequest.types("test")
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS))
BoolQueryBuilder boolQuery = new BoolQueryBuilder();
boolQuery.filter(new MatchPhrasePrefixQueryBuilder("originalName", "test"))
boolQuery.filter(new NestedQueryBuilder("user", new MatchQueryBuilder("user.id", "1"), ScoreMode.None))
boolQuery.filter(new MatchPhrasePrefixQueryBuilder("content", "blah"))
searchSourceBuilder.query(boolQuery)
searchRequest.source(searchSourceBuilder)
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

如果我只查询原始名称,它可以工作。如果我不再添加内容,如果我添加嵌套查询,则会导致错误:

org.elasticsearch.ElasticsearchStatusException: Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]

谢谢

这里是映射:

{
"mapping": {
"test": {
"properties": {
"attachment": {
"properties": {
"content": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"content_length": {
"type": "long"
},
"content_type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"date": {
"type": "date"
},
"language": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"company": {
"properties": {
"id": {
"type": "long"
}
}
},
"contentType": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"dateCreated": {
"type": "date"
},
"fileHash": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"id": {
"type": "long"
},
"imageHash": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"isClassified": {
"type": "boolean"
},
"isInElastic": {
"type": "boolean"
},
"label": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"lastUpdated": {
"type": "date"
},
"md5": {
"type": "long"
},
"ocrAvailable": {
"type": "boolean"
},
"ocrFile64": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"originalName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"project": {
"properties": {
"id": {
"type": "long"
}
}
},
"size": {
"type": "long"
},
"status": {
"properties": {
"enumType": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"storageName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"tag": {
"properties": {
"id": {
"type": "long"
}
}
},
"user": {
"properties": {
"id": {
"type": "long"
}
}
}
}
}
}
}

Elasticsearch 抱怨是因为您的"user"字段不是nested类型字段。您可以对"user.id"使用标准matchterm查询。

JSON 查询如下所示:

POST <your_index>/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"originalName": "test"
}
},
{
"match": {
"user.id": 1
}
},
{
"match": {
"content": "blah"
}
}
]
}
}
}

最新更新