简体   繁体   English

ElasticSearch多个术语搜索json

[英]ElasticSearch multiple terms search json

I have a ton of items in a Db with many columns. 我在Db中有很多项目,有很多列。 I need to search across two of these columns to get one data set. 我需要搜索其中两列来获取一个数据集。 The first column, genericCode , would group together any of the rows that have that code. 第一列genericCode将任何具有该代码的行组合在一起。 The second column, genericId , is calling out a specific row to add because it is missing the list of genericCode 's i'm looking for. 第二列, genericId ,正在调用要添加的特定行,因为它缺少我正在寻找的genericCode列表。

The back-end C# sets up my json for me as follows, but it returns nothing. 后端C#为我设置了我的json,如下所示,但它什么也没有返回。

{
  "from": 0,
  "size": 50,
  "aggs": {
    "paramA": {
      "nested": {
        "path": "paramA"
      },
      "aggs": {
        "names": {
          "terms": {
            "field": "paramA.name",
            "size": 10
          },
          "aggs": {
            "specValues": {
              "terms": {
                "field": "paramA.value",
                "size": 10
              }
            }
          }
        }
      }
    }
  },
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "locationId": {
              "value": 1
            }
          }
        },
        {
          "terms": {
            "genericCode": [
              "10A",
              "20B"
            ]
          }
        },
        {
          "terms": {
            "genericId": [
              11223344
            ]
          }
        }
      ]
    }
  }
}

I get and empty result set. 我得到并清空结果集。 If I remove either of the "terms" I get what I would expect. 如果我删除任何一个"terms"我会得到我所期望的。 So, I just need to combine those terms into one search. 所以,我只需要将这些术语组合成一个搜索。

I've gone through a lot of the documentation here: https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html and still can't seem to find what I'm looking for. 我在这里经历了很多文档: https//www.elastic.co/guide/en/elasticsearch/reference/current/search.html但仍然无法找到我正在寻找的东西。

Let's say I'm Jelly Belly and I want to create a bag of jelly beans with all the Star Wars and Disney jelly beans, and I also want to add all beans of the color green. 让我们说我是Jelly Belly,我想用所有的星球大战和迪士尼果冻豆制作一袋果冻豆,我还想添加绿色的所有豆子。 That is basically what I'm trying to do. 这基本上就是我想要做的。

EDIT: Changing the "must" to '"should"` isn't quite right either. 编辑:改变"must"到“应该”`也不是很正确。 I need it to be (in pseudo sql): 我需要它(在伪sql中):

SELECT *
FROM myTable
Where locationId = 1
AND (
  genericCode = "this", "that
  OR
  genericId = 1234, 5678
  )

The locationId separates our data in an important way. locationId以一种重要的方式分隔我们的数据。

I found this post: elasticsearch bool query combine must with OR and it has gotten me closer, but not all the way there... 我发现这篇文章: elasticsearch bool查询结合必须与OR ,它让我更接近,但不是所有的方式...

I've tried several iterations of should > must > should > must building this query and get varying results, but nothing accurate. 我已经尝试过几次迭代应该>必须>应该>必须构建这个查询并获得不同的结果,但没有准确的。

Here is the query that is working. 这是正在运行的查询。 It helped when I realized I was passing in the wrong data for one of my parameters. 当我意识到我为我的一个参数传递了错误的数据时,它有所帮助。 doh 卫生署

Nest the should inside the must as @khachik noted in the comment above. 将the should嵌入must如上面的评论中所述@khachik。 I had this some time ago but it wasn't working due to the above blunder. 我不久前有这个,但由于上述错误而无法正常工作。

{
  "from": 0,
  "size": 10,
  "aggs": {
    "specs": {
      "nested": {
        "path": "paramA"
      },
      "aggs": {
        "names": {
          "terms": {
            "field": "paramA.name",
            "size": 10
          },
          "aggs": {
            "specValues": {
              "terms": {
                "field": "paramA.value",
                "size": 10
              }
            }
          }
        }
      }
    }
  },
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "locationId": {
              "value": 1
            }
          }
        },
        {
          "bool": {
            "should": [
              {
                "terms": {
                  "genericCode": [
                    "101",
                    "102"
                  ]
                }
              },
              {
                "terms": {
                  "genericId": [
                    3078711,
                    3119430
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM