V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
GeekHao
V2EX  ›  程序员

请教各位一个 ES 关于 nested 检索问题

  •  
  •   GeekHao · 344 天前 · 682 次点击
    这是一个创建于 344 天前的主题,其中的信息可能已经有所发展或是发生改变。

    需求:查询出满足以下两个条件的人员

    • 两项实用新型专利
    • 论文以 [第一作者] 在 [ SCI ] 发布过两篇

    es 版本:7.4.2

    mapping 设计:

    {
      "mappings": {
        "person": {
          "properties": {
            "patents": {
              "type": "nested",
              "properties": {
                "patent_type": {"type": "text"},
                "patent_name": {"type": "text"}
              }
            },
            "awards": {
              "type": "nested",
              "properties": {
                "award_name": {"type": "text"},
                "award_level": {"type": "keyword"}
              }
            },
            "papers": {
              "type": "nested",
              "properties": {
                "paper_name": {"type": "text"},
                "role": {"type": "keyword"},
                "publication_level": {"type": "keyword"}
              }
            }
          }
        }
      }
    }
    

    希望各位可以给出参考建议,方便的话可以直接贴出 DSL

    3 条回复    2023-05-18 15:23:06 +08:00
    baozhibo
        1
    baozhibo  
       344 天前   ❤️ 1
    以下是满足您提供条件的 Elasticsearch 查询 DSL:

    ```json
    {
    "query": {
    "bool": {
    "must": [
    {
    "nested": {
    "path": "patents",
    "query": {
    "term": {
    "patents.patent_type": "实用新型专利"
    }
    }
    }
    },
    {
    "nested": {
    "path": "papers",
    "query": {
    "bool": {
    "must": [
    {
    "term": {
    "papers.role": "第一作者"
    }
    },
    {
    "term": {
    "papers.publication_level": "SCI"
    }
    }
    ]
    }
    }
    }
    }
    ],
    "minimum_should_match": 2
    }
    }
    }
    ```

    您可以使用上述查询 DSL 在 Elasticsearch 中执行搜索,以获得满足条件的人员。请注意,该查询假设您的索引类型为 "person",并且在索引中的字段和嵌套对象的属性名称与提供的映射一致。如果您的实际情况不同,请相应地调整查询。
    baozhibo
        2
    baozhibo  
       344 天前
    @baozhibo chatGPT 的回答。
    GeekHao
        3
    GeekHao  
    OP
       344 天前
    @baozhibo minimum_should_match 是最少满足两个 bool 条件吧, 我用 gpt 尝试解决这个问题,但是它的回答似乎都不太正确
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2870 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:50 · PVG 20:50 · LAX 05:50 · JFK 08:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.