elasticsearch 如何按定义了多个字段的嵌套字段进行排序

jqjz2hbq  于 2022-12-03  发布在  ElasticSearch
关注(0)|答案(1)|浏览(209)

首先,这是我在这里的第一篇文章,所以请耐心等待
我试图找到一个合适的方法来排序一个类中的elasticsearch嵌套字段,其定义如下:

public class SignalementConflitRechercheAvanceeDocument extends SignalementConflitNestedRechercheAvanceeDocument{

private static final long serialVersionUID = 1L;

// Objet(s) de croisement
@Field(type = FieldType.Nested)
private EtablissementNestedRechercheAvanceeDocument etablissementRechercheAvanceeDocument;

}
此嵌套对象定义如下:

public class EtablissementNestedRechercheAvanceeDocument implements Serializable {
    /**
     *
     */
    protected static final long serialVersionUID = 1L;

    @Id
    @MultiField(mainField = @Field(type = FieldType.Text, analyzer = "index-analyzer", searchAnalyzer = "search-analyzer"), otherFields = {
            @InnerField(suffix = "tri", type = FieldType.Keyword, normalizer = "sort-normalizer") })
    protected String siret;

    @Field(type = FieldType.Boolean)
    protected boolean secteur;

    @Field(type = FieldType.Keyword)
    protected String siren;

    @Field(type = FieldType.Long)
    protected Long regId;

    @MultiField(mainField = @Field(type = FieldType.Text), otherFields = {
            @InnerField(suffix = "tri", type = FieldType.Keyword) })
    protected String libelleRegion;

}

或者

"regId": {
    "type": "long"
},
"secteur": {
    "type": "boolean"
},
"siren": {
    "type": "keyword"
},
"siret": {
    "type": "text",
    "fields": {
        "tri": {
            "type": "keyword",
            "normalizer": "sort-normalizer"
        }
    },
    "analyzer": "index-analyzer",
    "search_analyzer": "search-analyzer"
},
"libelleService": {
    "type": "text",
    "fields": {
        "tri": {
            "type": "keyword"
        }
    }
},

在纯弹性语法中。
我已经尝试了许多组合,但最接近干净请求的组合应该是

{
    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "bool": {
                                            "should": [
                                                {
                                                    "match": {
                                                        "etablissementRechercheAvanceeDocument.secteur": {
                                                            "query": "false",
                                                            "operator": "OR",
                                                            "prefix_length": 0,
                                                            "max_expansions": 50,
                                                            "fuzzy_transpositions": true,
                                                            "lenient": false,
                                                            "zero_terms_query": "NONE",
                                                            "auto_generate_synonyms_phrase_query": true,
                                                            "boost": 1
                                                        }
                                                    }
                                                }
                                            ],
                                            "adjust_pure_negative": true,
                                            "boost": 1
                                        }
                                    }
                                ],
                                "adjust_pure_negative": true,
                                "boost": 1
                            }
                        },
                        "path": "etablissementRechercheAvanceeDocument",
                        "ignore_unmapped": false,
                        "score_mode": "max",
                        "boost": 1,
                        "inner_hits": {
                            "ignore_unmapped": false,
                            "from": 0,
                            "size": 100,
                            "version": false,
                            "seq_no_primary_term": false,
                            "explain": false,
                            "track_scores": false
                        }
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1
        }
    },
    "sort": [
        {
            "etablissementRechercheAvanceeDocument.siret.tri": {
                "order": "asc",
                "nested":{
                  "path": "etablissementRechercheAvanceeDocument.siret",
                }
            }
        }
    ]
}

在所有情况下,我都会收到相同的400错误堆栈

Error 400.
{"error":{"root_cause":[{"type":"query_shard_exception","reason":"[nested] failed to find nested object under path [etablissementRechercheAvanceeDocument.siret]","index_uuid":"lGwiXFs4RDCIZ6jqGVzPYQ","index":"signalement-conflit-recherche-avancee-doc"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"signalement-conflit-recherche-avancee-doc","node":"WKtmnvO8SdOBplHuGeLxIw","reason":{"type":"query_shard_exception","reason":"[nested] failed to find nested object under path [etablissementRechercheAvanceeDocument.siret]","index_uuid":"lGwiXFs4RDCIZ6jqGVzPYQ","index":"signalement-conflit-recherche-avancee-doc"}}]},"status":400}

看在上帝的份上,我在这里找不到我所缺少的东西。任何帮助都是感激不尽的

v2g6jxz6

v2g6jxz61#

您正在尝试的查询只是有一个小错误,在“path”中您必须设置父嵌套字段:

POST test_nest/_search
{
  "sort": [
    {
      "etablissementRechercheAvanceeDocument.siret.tri": {
        "order": "asc",
        "nested": {
          "path": "etablissementRechercheAvanceeDocument"
        }
      }
    }
  ]
}

有关嵌套字段的详细信息

若要运行嵌套查询,字段必须为“嵌套”类型:
https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html
巢状排序:
https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html#nested-sorting
我在这里写了一篇关于嵌套字段类型和对象之间区别的文章:
https://opster.com/guides/elasticsearch/data-architecture/elasticsearch-nested-field-object-field/
简而言之,嵌套字段类型保留了嵌套文档属性之间的关系,对象平面化了这一切。
使用嵌套字段,您可以执行以下操作:
具有子项的文档。颜色=红色AND子项。大小=大在同一子项中
如果没有嵌套类型,则只能在所有文档子级中执行子级。color = red AND child.size = large**

相关问题