gpt4 book ai didi

elasticsearch - ElasticSearch:是否可以在搜索请求期间生成 “Temporary Field”?

转载 作者:行者123 更新时间:2023-12-03 00:41:51 26 4
gpt4 key购买 nike

样本文件:

{
"text": "this is my text",
"categories": [
{"category": "sample category"},
{"category": "local news"}
]
}

当前的映射为:
{
"topic": {
"properties": {
"categories": {
"properties": {
"category": {
"type": "string",
"store": "no",
"term_vector": "with_positions_offsets",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word",
"include_in_all": "true",
"boost": 8,
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
}

搜索查询:
{
"_source": false,
"query":{
"match":{
"categories.category":"news"
}
},
"aggs": {
"match_count": {
"terms" : {"field": "categories.category.raw"}
}
}
}

我想要的结果是:
{
...
"buckets": [
{
"key": "local news",
"doc_count": 1
}

]
...
}

结果实际上是(它汇总了所有匹配文档的category.category):
{
...
"buckets": [
{
"key": "local news",
"doc_count": 1
},{
"key": "sample category", //THIS PART IS NOT NEEDED
"doc_count": 1
}
]
...
}

在搜索过程中是否可以添加 temporary field?在这种情况下,假设所有匹配的 categories.category都命名为 categories.match_category,并通过此临时字段 categories.match_category进行汇总?如果为真,我该怎么办?如果不可以,该怎么办?

最佳答案

以下是另一种方法,但它更符合您的需求逻辑:

映射

 {
"topic": {
"properties": {
"categories": {
"type":"nested",
"properties": {
"category": {
"type": "string",
"store": "no",
"analyzer": "simple",
"include_in_all": "true",
"boost": 8,
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
}

数据
{
"text": "this is my text",
"categories": [
{"category": "sample category"},
{"category": "local news"}
]
}

查询
{
"query":{
"nested":{
"path":"categories",
"query":{
"filtered":{
"query":{
"match":{
"categories.category":"news"
}
}
}
}
}
},
"aggs": {
"nest":{
"nested":{
"path":"categories"

},
"aggs":{
"filt":{
"filter" : {
"script": {
"script" : "doc['categories.category'].values.contains('news')"
}
},
"aggs":{
"match_count": {
"terms" : {"field": "categories.category.raw"}
}
}
}
}
}
}
}

产生的结果
{
"_shards": {
"failed": 0,
"successful": 5,
"total": 5
},
"aggregations": {
"nest": {
"doc_count": 2,
"filt": {
"doc_count": 1,
"match_count": {
"buckets": [
{
"doc_count": 1,
"key": "local news"
}
],
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0
}
}
}
},
"hits": {
"hits": [],
"max_score": 0.0,
"total": 1
},
"timed_out": false,
"took": 3
}

这里的问题是您必须根据聚合中的脚本过滤器来创建自己的脚本,上面的示例在“类别”映射中使用一个简单的分析器为我工作

关于elasticsearch - ElasticSearch:是否可以在搜索请求期间生成 “Temporary Field”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35861856/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com