英文:
ElasticSearch Search for Field Values does not return
问题
假设我在Elasticsearch中有一个文档,该文档在_source中包含字段"provider"。
我尝试了许多查询,但似乎没有一个查询能够返回带有搜索值的文档。
文档:
"_source": {
"jobs": [],
"provider": {
"id": "1",
"name": "Coursera"
},
"sckLevels": [],
"scks": [],
"trArea": [],
"trElems": [],
"training": {
"description": "网络安全描述",
"id": "0",
"img": "图片链接",
"link": "https://google.com",
"name": "网络安全",
"trainingProvID": "1"
}
}
而我的查询代码是:
SearchRequest searchRequest = new SearchRequest(index);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.termQuery("provider", "Coursera"));
searchRequest.source(sourceBuilder);
this.multiRequest.add(searchRequest);
我的响应是空白的。
谢谢。
英文:
Lets say I have document in the Elastic Search which does containt field "provider" in the _source.
I have tryied many queries but none of them seem to return the document with searched value.
Doc:
"_source" : {
"jobs" : [ ],
"provider" : {
"id" : "1",
"name" : "Coursera"
},
"sckLevels" : [ ],
"scks" : [ ],
"trArea" : [ ],
"trElems" : [ ],
"training" : {
"description" : "Cyber sec desc",
"id" : "0",
"img" : "img link",
"link" : "https://google.com",
"name" : "Cyber sec",
"trainingProvID" : "1"
}
And my code for the query is:
SearchRequest searchRequest = new SearchRequest(index);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.termQuery("provider", "Coursera"));
searchRequest.source(sourceBuilder);
this.multiRequest.add(searchRequest);
My response is blank.
Thank you.
答案1
得分: 2
以下是翻译好的内容:
你的Elasticsearch查询中确实存在一些问题:
-
似乎
provider
字段是对象或嵌套类型,但在你的查询中只提到了Coursera
,实际上应该与provider
字段的name
子字段匹配。根据对象或嵌套数据类型,你需要修改你的查询。 -
你正在使用
term查询
,该查询不会被分析,只会进行关键字匹配,但如果你的name
字段被定义为text
,则在索引时会被转换为小写,因此大写字母C
开头的Coursera
不会匹配。你需要在文本字段上使用match
查询。
英文:
There is definitely few issues with your Elasticsearch query
-
Seems
provider
field is of object or nested type, while in your query you are just mentioningCoursera
but it should be matched against thename
subfield ofprovider
field and based on object or nested data type, you need to modify your query. -
You are using the
term query
which is not analyzed and used for keyword ie extact match while if yourname
field is defined astext
it would be lowercased at index time andCoursera
with captialC
won't match, you need to use thematch
query on text fields.
答案2
得分: 1
谢谢,嵌套是问题所在。我以为它会在对象上执行搜索。
看到了一个不错的讨论线程。
我是这样解决的:
SearchRequest searchRequest = new SearchRequest(index);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchQuery("provider.name", "Coursera"));
searchRequest.source(sourceBuilder);
this.multiRequest.add(searchRequest);
英文:
thank you, nesting was the problem. I thought that it will do the search on the object.
Came across good thread.
Solved it like this:
SearchRequest searchRequest = new SearchRequest(index);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchQuery("provider.name", "Coursera"));
searchRequest.source(sourceBuilder);
this.multiRequest.add(searchRequest);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论