【问题标题】:Elasticsearch search for a value across multiple fieldsElasticsearch 跨多个字段搜索值
【发布时间】:2014-12-24 05:50:26
【问题描述】:

我的目的是跨多个字段搜索一个值,并返回这些值的计数和不同的值。

为此,我意识到我必须使用构面。

这是数据库架构:

index:   
    analysis:   
        analyzer:
            custom_search_analyzer:
                type: custom
                tokenizer: standard
                filter   : [standard, snowball, lowercase, asciifolding]
            custom_index_analyzer:
                type: custom
                tokenizer: standard
                filter   : [standard, snowball, lowercase, asciifolding, custom_filter]
        filter:
            custom_filter:
                type: edgeNGram
                side: front
                min_gram: 1
                max_gram: 20

{
 "structure": {
   "properties": {
     "name": {"type": "string", "search_analyzer": "custom_search_analyzer", "index_analyzer": "custom_index_analyzer"},
     "locality": {"type": "string", "search_analyzer": "custom_search_analyzer", "index_analyzer": "custom_index_analyzer"},
     "province": {"type": "string", "search_analyzer": "custom_search_analyzer", "index_analyzer": "custom_index_analyzer"},
     "region": {"type": "string", "search_analyzer": "custom_search_analyzer", "index_analyzer": "custom_index_analyzer"}
   }
 }
}

这是我尝试使用的查询:

{
"query": {
    "bool": {
      "should": [
        {
          "match": {
            "locality": "bolo"
          }
        },
        {
          "match": {
            "region": "bolo"
          }
        },
        {
          "match": {
            "name": "bolo"
          }
        }
      ]
    }
  },
  "facets": {
    "region": {
      "query": {
        "term": {
          "region": "bolo"
        }
      }
    },
    "locality": {
      "query": {
        "term": {
          "locality": "bolo"
        }
      }
    },
    "name": {
      "query": {
        "term": {
          "name": "bolo"
        }
      }
    }
  }
}

在我所做的所有测试中,这是最接近我想要的结果的查询,但是,它并没有告诉我不同​​字段的计数,我发现它可以计算总字段。

例如上面的查询返回如下结果:

facets: {
       region: {
       _type: query
       count: 0
    }
    locality: {
       _type: query
       count: 2
    }
    name: {
       _type: query
       count: 0
    }
}

我希望得到这样的结果(不是很明显写是正确的,但确实明白我需要什么):

facets: {
    ....
    locality: {
       _type: query
       "terms": [
           {"term": "Bologna", "count": 1},
           {"term": "Bolognano", "count": 1}
       ]

    }

我该怎么办?

我已经尝试在 facets 中使用“terms”而不是“query”,并将“index: not_analyzed”放在研究领域,但只有在我尝试确切范围而不是它的一部分时才会返回!

【问题讨论】:

  • 如果我对您的理解正确,您将需要创建分面过滤器以仅匹配一个字段和一个涵盖所有字段的分面。
  • 不完全是,我希望不要给我所有方面的总数,而是与搜索匹配的字段的内容,以及找到的任何内容的计数。作为我上面写的一个例子。这样的事情是使用构面terms 而不是query 获得的,只有terms 只能找到该字段的完全匹配!

标签: elasticsearch


【解决方案1】:

这可以使用值计数聚合来完成。 在 value count aggregation 中,它为您提供唯一术语的数量。 虽然术语聚合为您提供唯一术语及其文档数。

我相信您正在寻找价值计数聚合 - http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多