如何在query_string中使用'fields'搜索绝对字段名称?

时间:2015-06-25 16:23:33

标签: elasticsearch

我有多个索引,并且我通过所有这些索引进行全局搜索。

我如何告诉elasticsearch区分article.author.name字段author.name(其中'article'是类型,'author.name'是嵌套字段)来自curl -X GET 'http://localhost:9200/*/author,article/_search?pretty' -d '{ "query": { "filtered": { "filter": { "bool": { "must": [ { "term": { "tag": "programming" } } ] } }, "query": { "query_string": { "query": "John Doe", "fields": ["author.name"] } } } } (其中' author'是类型,'name'是顶级属性)?

因此,例如,如果执行此类搜索:

name

我只想通过author类型中的author.name字段进行搜索。但不在article类型的article字段内。那么如何才能使查询中的字段名称被视为“绝对”字段名称?因为,鉴于它是全局搜索,我想通过被查询的字段定义搜索范围。

在这种情况下,我不能简单地从查询URI中删除ssh user@domain.com cd /directory/of/gitProject git init git add . git commit 'First commit' 字符串。

2 个答案:

答案 0 :(得分:0)

你可以在bool查询字符串上使用 should 而不是必须。

curl -X GET 'http://localhost:9200/*/author,article/_search?pretty' -d '{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "tag": "programming"
              }
            }
          ]
        }
      },
      "query": {
        "bool": {
          "must": [
                {
                  "term": {
                    "author.name": "John Doe"
                  }
                }
          ]
        }
      }
    }
  }
}'

答案 1 :(得分:0)

我发现一个可能的解决方案是更改索引的映射。

令我沮丧的是,我使用' object'设置了我拥有的所有嵌套字段。键入,而不是嵌套'。我在这里做了一个测试,如果我使用嵌套的' (我显然必须重新索引我的文档)我得到了所需的结果,而不必更改我的搜索查询,这是我基本上寻找的。有关详细信息:https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-nested-type.html

但我必须承认,重建指数和重新索引数据是一种我不太喜欢的方法。