php - elasticsearch匹配词组前缀和其他

标签 php elasticsearch elastica

嗨,我对elasticsearch有问题,
我有一些结果

  • modul'ion
  • test lithium file


如果我键入'mod'而不执行查询,则找不到结果,我将类型:“phrase_prefix”添加到查询中,现在我找到了结果

modul'ion



但是现在当我输入锂时,a找不到结果

test lithium file



我的请求
    $query ['match'] ['_all'] ["query"] = strtolower ( $keyword );
    $query ['match'] ['_all'] ["type"] = "phrase_prefix";
    $query ['match'] ['_all'] ["analyzer"] = "synonym";

我也使用一个包含“锂=>可充电锂”的同义词分析器
我的问题是如果不使用分析仪或我将其移除

$query ['match'] ['_all'] ["type"] = "phrase_prefix";



我找到结果了,但是'mod'的问题又回来了,所以我想在两种情况下都能得到结果,您能帮我吗?

我用这个查询设置分析器
 {"analysis" : {
    "analyzer" : {
        "synonym" : {
            "tokenizer" : "whitespace",
            "filter" : ["synonym"]
        }
    },
"filter" : {
            "synonym" : {
                "type" : "synonym",
                "synonyms_path" : "synonym.txt",
                "ignore_case" : true
            }
        }
    }
}

最佳答案

首先,我看不到您的映射有任何问题,它们在后端工作正常。您的问题是您要查询_all字段,该字段需要单独配置。如果未指定,它将具有默认参数,可以在here中看到。为了更改此设置,我使用了以下设置和映射:

PUT /test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "whitespace",
          "char_filter": ["my_mapping"],
          "filter": [
            "lowercase",
            "my_synonym"
          ]
        }
      },
      "filter": {
        "my_synonym": {
          "type": "synonym",
          "ignore_case": true,
          "synonyms": [
            "rechargeable lithium => lithium"
          ]
        }
      },
      "char_filter": {
        "my_mapping": {
          "type": "mapping",
          "mappings": [
            "'=>"
          ]
        }
      }
    }
  },
  "mappings": {
    "test": {
      "_all": {
        "enabled": true,
        "analyzer": "my_analyzer"
      }
    }
  }
}

这些设置将在空白处断开您的 token ,删除 token 中的引号并将其小写,以便:
  • modul'ion将被索引为modulion,只要用户键入这些短语中的任何一个,他都会找到它。
  • rechargeable lithium替换为lithium作为同义词。
  • 由于使用lowercase过滤器,因此您的搜索不区分大小写。

  • 使用这些映射,我使用以下方式将您的数据添加到索引中:
    PUT /test/test/1
    {
      "text": "modul'ion"
    }
    
    PUT /test/test/2
    {
      "text": "test lithium file"
    }
    

    因此,现在运行此查询:
    POST /test/test/_search
    {
      "query": {
        "match": {
          "_all": {
            "query": "rechargeable lithium",
            "type": "phrase_prefix"
          }
        }
      }
    }
    

    还给我这个文件:
    {
      "took": 2,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 0.15342641,
        "hits": [
          {
            "_index": "test",
            "_type": "test",
            "_id": "2",
            "_score": 0.15342641,
            "_source": {
              "text": "test lithium file"
            }
          }
        ]
      }
    }
    

    以下两个查询:
    POST /test/test/_search
    {
      "query": {
        "match": {
          "_all": {
            "query": "mod",
            "type": "phrase_prefix"
          }
        }
      }
    }
    
    POST /test/test/_search
    {
      "query": {
        "match": {
          "_all": {
            "query": "modulion",
            "type": "phrase_prefix"
          }
        }
      }
    }
    

    返回此:
    {
      "took": 1,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 0.30685282,
        "hits": [
          {
            "_index": "test",
            "_type": "test",
            "_id": "1",
            "_score": 0.30685282,
            "_source": {
              "text": "modul'ion"
            }
          }
        ]
      }
    }
    

    这只是RAW JSON查询,但我想您将可以使用PHP很好地处理这些查询。

    关于php - elasticsearch匹配词组前缀和其他,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33607542/

    相关文章:

    php - 如何测试地理位置标识符功能?

    Elastica:检查 ID 为 x 的文档是否存在的最佳方法?

    php - 如何使用 Doctrine 2 在 Zend Framework 2 中集成 Elasticsearch

    php - 如何正确缩进 PHP/HTML 混合代码?

    php - 从具有不同模式的多个 MySQL 表中选择 id 和时间戳

    elasticsearch - 如何在Elasticsearch中为现有索引更新动态模板的效果

    elasticsearch - 在 Elasticsearch 的响应字段中格式化日期

    php - 查找键的数组值

    elasticsearch - Elasticsearch范围不适用于双索引