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 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿