dsjfrkvn818747 2013-07-26 09:38
浏览 161
已采纳

Elasticsearch - 使用Bool(MUST&AND)DSL查询创建报告过滤器

I am trying to create some report filters where the user can search for profiles using any fields on the report. For example: search for any profile with firstname that starts with ann and grade that starts with vi etc.

Here is a query I have written so far:

{
    from: 20,
    size: 20,
    query: {
        filtered: {
            query: {
                match_all: [ ]
            },
            filter: {
                bool: {
                    must: [
                        {
                            prefix: {
                                firstname: "ann"
                            }
                        },
                        {
                            prefix: {
                                grade: "vi"
                            }
                        }
                    ]
                }
            }
       }
   },
   sort: {
       grade: {
           order: "asc"
       }
   }
}

If I remove one child of must (in the bool filter), it works. But it doesn't return any results once I use more than one filters and I need to be able to use any number of entries in there.

Also, if I use should instead of must, it works. I'm not sure if I'm misunderstanding the logic, but to my understanding (in this case) must should return ONLY results with firstname that starts with ann and grade that starts with vi.

They do exist, but this query just doesn't find them.

Am I missing something here?

Thanks

  • 写回答

1条回答 默认 最新

  • douxie7738 2013-07-26 11:29
    关注

    Since, I cannot post comments yet. I'm answering with some assumptions.

    First of all, I'm using ES 0.90.2 version and your query works fine for my inputs. However, depending on your input size and the platform that you executed your query, my answer may not be the right one.

    Assumption: Number of data in the index is less than 20.

    I've added following inputs to my index:

    '{"name": "ann", "grade": "vi"}'
    '{"name": "ann", "grade": "ii"}'
    '{"name": "johan", "grade": "vi"}'
    '{"name": "johan", "grade": "ii"}'
    

    And my test query was the same as yours, and here is the result:

    "hits" : {
        "total" : 2,
        "max_score" : null,
        "hits" : [ ]          // <-- see this part is blank
      }
    

    As you can see, it didn't listed hits, but there are two hits. That's because of the from:20 code segment. If you change that value, you can see some results. If you want to see all results just delete that part.

    Note: Well if this is not the case, sorry for bothering :(

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?