ElasticSearch - 带空格和过滤的前缀

 手机用户2702936061 发布于 2022-12-28 12:39

我的ElasticSearch服务器包含以下格式的文档:

{
    "_index": "xindex",
    "_type": "xtype",
    "_id": "1100",
    "_score": 3.00010,
    "_source": {
        "_id": "2333345",
        "field1": "11111111111111",
        "field2": "y",
        "name": "hello world",
    }
}

我需要获取名称前缀为"hello wo"和field2 "y"的所有文档.尝试了很多查询,但都没有.有空格问题的前缀有各种解决方案,但是当为field2添加过滤/另一个查询时,结果会被破坏.

谢谢.

1 个回答
  • 您可以通过3个步骤实现此目的:

      将字段名称的映射更改为not_analyzed

      使用match_phrase_prefix查询(此处的文档)

      通过将此查询结果包装在已过滤的查询中并在字段2上使用值为"y" 的术语过滤器来过滤此查询结果

    您可以看到它使用以下数据集:

    PUT test/prefix/_mapping
    {
      "properties": {
        "name":{
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
    
    //should match
    PUT test/prefix/2333345
    {
      "field1": "11111111111111",
      "field2": "y",
      "name": "hello world"
    }
    
    //should match    
    PUT test/prefix/1112223
    {
      "field1": "22222222222222",
      "field2": "y",
      "name": "hello wombat"
    }
    
    //should not match (field2 value is different)
    PUT test/prefix/4445556
    {
      "field1": "33333333333333",
      "field2": "z",
      "name": "hello world"
    }
    
    //should not match (second word not starting with wo)
    PUT test/prefix/4445556
    {
      "field1": "33333333333333",
      "field2": "y",
      "name": "hello zombie"
    }
    

    然后,查询是:

    GET test/prefix/_search
    {
      "query": {
        "filtered": {
          "query": {
            "match_phrase_prefix" : {
            "name" : "hello wo"
            }
          },
          "filter": {
            "term": {
              "field2": "y"
            }  
          }
        } 
      }
    }
    

    按预期输出文件1112223和2333345:

    {
       "took": 20,
       "timed_out": false,
       "_shards": {
          "total": 5,
          "successful": 5,
          "failed": 0
       },
       "hits": {
          "total": 2,
          "max_score": 1.592944,
          "hits": [
             {
                "_index": "test",
                "_type": "prefix",
                "_id": "2333345",
                "_score": 1.592944,
                "_source": {
                   "field1": "11111111111111",
                   "field2": "y",
                   "name": "hello world"
                }
             },
             {
                "_index": "test",
                "_type": "prefix",
                "_id": "1112223",
                "_score": 1.592944,
                "_source": {
                   "field1": "22222222222222",
                   "field2": "y",
                   "name": "hello wombat"
                }
             }
          ]
       }
    }
    

    2022-12-28 12:41 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有