弹性搜索搜索字段值不返回

huangapple go评论92阅读模式
英文:

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查询中确实存在一些问题:

  1. 似乎 provider 字段是对象或嵌套类型,但在你的查询中只提到了 Coursera,实际上应该与 provider 字段的 name 子字段匹配。根据对象或嵌套数据类型,你需要修改你的查询。

  2. 你正在使用 term查询,该查询不会被分析,只会进行关键字匹配,但如果你的 name 字段被定义为 text,则在索引时会被转换为小写,因此大写字母 C 开头的 Coursera 不会匹配。你需要在文本字段上使用 match 查询。

英文:

There is definitely few issues with your Elasticsearch query

  1. Seems provider field is of object or nested type, while in your query you are just mentioning Coursera but it should be matched against the name subfield of provider field and based on object or nested data type, you need to modify your query.

  2. You are using the term query which is not analyzed and used for keyword ie extact match while if your name field is defined as text it would be lowercased at index time and Coursera with captial C won't match, you need to use the match 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);

huangapple
  • 本文由 发表于 2020年8月27日 22:19:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63618058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定