嵌套排序未按预期运行elasticsearch

ippsafx7  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(502)

要求
使用与自定义\u字段\u id匹配的自定义\u字段\u值对产品进行排序
Map

  1. {
  2. "mapping": {
  3. "product": {
  4. "properties": {
  5. "user_id": {
  6. "type": "integer"
  7. }
  8. "custom_field_values": {
  9. "type": "nested",
  10. "properties": {
  11. "custom_field_id": {
  12. "type": "integer"
  13. },
  14. "id": {
  15. "type": "integer"
  16. },
  17. "value": {
  18. "type": "text",
  19. "fields": {
  20. "keyword": {
  21. "type": "keyword"
  22. }
  23. }
  24. }
  25. }
  26. }
  27. }
  28. }
  29. }
  30. }

示例数据

  1. {
  2. {
  3. "_type": "product",
  4. "_source": {
  5. "user_id": 1,
  6. "custom_field_values": [
  7. { "id": 1, "custom_field_id": 1, "value": "A"},
  8. { "id": 2, "custom_field_id": 2, "value": "B"},
  9. { "id": 3, "custom_field_id": 3, "value": "C"},
  10. ]
  11. }
  12. },
  13. {
  14. "_type": "product",
  15. "_source": {
  16. "user_id": 2,
  17. "custom_field_values": [
  18. { "id": 4, "custom_field_id": 1, "value": "Y"},
  19. { "id": 5, "custom_field_id": 2, "value": "Z"},
  20. ]
  21. }
  22. },
  23. {
  24. "_type": "product",
  25. "_source": {
  26. "user_id": 3,
  27. "custom_field_values": [
  28. { "id": 6, "custom_field_id": 2, "value": "P"},
  29. { "id": 7, "custom_field_id": 3, "value": "Q"},
  30. ]
  31. }
  32. }
  33. }

预期
我应该可以把整个 product 筛选依据 custom_field_values.custom_field_id ,排序依据 custom_field_values.value 示例查询

  1. {
  2. "size":100,
  3. "from":0,
  4. "query":{
  5. "bool":{
  6. "filter":{
  7. "match":{
  8. "user_id":1
  9. }
  10. }
  11. }
  12. },
  13. "sort":[
  14. {
  15. "custom_field_values.value.keyword":{
  16. "order":"desc",
  17. "nested":{
  18. "path":"custom_field_values",
  19. "filter":{
  20. "match":{
  21. "custom_field_values.custom_field_id": 2
  22. }
  23. }
  24. }
  25. }
  26. }
  27. ]
  28. }

更新的查询

  1. {
  2. "size":100,
  3. "from":0,
  4. "query":{
  5. "bool":{
  6. "filter":{
  7. "match":{
  8. "user_id":1
  9. }
  10. }
  11. },
  12. "nested": {
  13. "path": "comments",
  14. "filter": {
  15. "custom_field_values.custom_field_id": 2
  16. }
  17. }
  18. },
  19. "sort":[
  20. {
  21. "custom_field_values.value.keyword":{
  22. "order":"desc",
  23. "nested":{
  24. "path":"custom_field_values",
  25. "filter":{
  26. "match":{
  27. "custom_field_values.custom_field_id": 2
  28. }
  29. }
  30. }
  31. }
  32. }
  33. ]
  34. }

结果顺序应为 2nd product ,那么 3rd product 以及 1st product . 如果我想在产品上循环打印 custom_field_values.value 我应该去 Z , P , B .

e5nqia27

e5nqia271#

所以,问题在于区分大小写的数据。https://www.elastic.co/guide/en/elasticsearch/reference/current/normalizer.html 解决了我的问题。

  1. "settings": {
  2. "analysis": {
  3. "normalizer": {
  4. "my_normalizer": {
  5. "type": "custom",
  6. "char_filter": [],
  7. "filter": ["lowercase", "asciifolding"]
  8. }
  9. }
  10. }
  11. }

现在我们可以将此规范化器与关键字字段类型一起使用: field :field_name, type: 'keyword', normalizer: 'my_normalizer' 希望这有帮助。

相关问题