Elasticsearch:按值和动态模板排序

时间:2015-06-24 16:30:59

标签: java elasticsearch

我需要为以下文档进行排序:

{
  customer: {
    fullname: "Lorem ipsum"
  },
  order_number: "12313131",
  company: {
    name: "Test Inc."
  },
  date: "10.06.2015 18:00"
}

但据我所知,我不能按分析字段中的值排序。在那里,我试图创建一个映射:

 { 
      "mappings": { 
        "_default_": { 
          "dynamic_templates": [ 
            { 
              "base": { 
                "match": "*", 
                "mapping": { 
                  "type": "multi_field", 
                  "fields": { 
                    "{name}": {"type": "string"}, 
                    "_sort": {"type": "string", "analyzer": "sort"} 
                  } 
                } 
              } 
            } 
          ] 
        } 
      }, 
      "settings": { 
        "analysis": { 
          "analyzer": { 
            "sort": { 
              "type": "custom", 
              "tokenizer": "keyword", 
              "filter": "lowercase" 
            } 
          } 
        } 
      } 
    } 

但是如果我把这个配置,我得到一个例外:ElasticsearchIllegalArgumentException:未知属性。如果没有这种映射,我的索引工作正常。

我想要做的是创建一个名为name_sort(not_analysed)的多字段,以便按值排序。

**** 
At leas I can able to create a mapping correctly. My mapping looks like: 

 {
  "muhamo": {
    "mappings": {
      "bookings": {
        "dynamic_templates": [
          {
            "base": {
              "mapping": {
                "index": "analyzed",
                "type": "{dynamic_type}",
                "fields": {
                  "{name}_sort": {
                    "index": "not_analyzed",
                    "type": "{dynamic_type}"
                  }
                }
              },
              "match": "*",
              "match_mapping_type": "string"
            }
          },
          {
            "catch_all": {
              "mapping": {
                "fields": {
                  "{name}_sort": {
                    "index": "not_analyzed",
                    "type": "{dynamic_type}"
                  }
                }
              },
              "match": "*",
              "match_mapping_type": "*"
            }
          }
        ],
        "properties": {
          "bookingType": {
            "type": "string",
            "fields": {
              "bookingType_sort": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          },
          "comment": {
            "type": "string",
            "fields": {
              "comment_sort": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          },
          "costLocation": {
            "type": "string",
            "fields": {
              "costLocation_sort": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          },
          "customer": {
            "properties": {
              "fullname": {
                "type": "string",
                "fields": {
                  "fullname_sort": {
                    "type": "string",
                    "index": "not_analyzed"
                  }
                }
              }
            }
          },
          "date": {
            "type": "string",
            "fields": {
              "date_sort": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          },
          "deleted": {
            "type": "boolean"
          },
          "toAirport": {
            "type": "boolean"
          }
        }
      }
    }
  }
}

但是,如果我尝试按customer.fullname_sort对结果进行排序,我会得到一个例外

 query[ConstantScore(*:*)],from[-1],size[-1]: Parse Failure [No mapping found for [customer.fullname_sort] in order to sort on]

1 个答案:

答案 0 :(得分:0)

您应该对customer.fullname.fullname_sort进行排序。根据索引的映射,这是你的领域之路。