首页 > 解决方案 > 从搜索中排除某些字段 - Elasticsearch

问题描述

我已经为每个超过 100 个字段的文档编制了索引,每个字段都使用 Edge gram 标记器进行分析以支持自动建议。我确实需要搜索所有字段的自由文本搜索。当我尝试这样做时,搜索也会发生具有自动完成分析的字段(例如 Data.autocomplete_analyzed)。我必须通过仅搜索使用“文本”类型(例如数据)分析的字段来限制这一点。1.索引时间2.查询时间有没有办法做到这一点。映射文件:

    "mappings": {
                "_doc": {
                    "properties": {
                        "Data": {
                            "type": "text",
                            "fields": {
                                "autocomplete_analyzed": {
                                    "type": "text",
                                    "analyzer": "autocomplete"
                                },
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                } 
               }

搜索查询:

 {
"query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "aim",
            "type": "phrase",
            "slop": "2",
            "fields": []
          }
        },
        {
          "multi_match": {
            "query": "aim",
            "fuzziness": "1",
            "fields": []
          }
        }
      ],
      "minimum_should_match": 1 
}

标签: elasticsearch

解决方案


在查询时,您可以使用源过滤来选择您想要的字段。

    GET /_search
{
    "_source": [ "obj1.*", "obj2.*" ],
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

如果您使用 query_string 进行搜索,您可以使用字段

    GET /_search
{
  "query": {
    "query_string": {
      "query": "this AND that OR thus",
      "fields": [
          "docfilename",
          "filepath",
          "singlewordfield"
        ]
    }
  }
}

推荐阅读