Elasticsearch-如何在一个字符串中搜索多个单词

时间:2019-10-28 10:06:10

标签: elasticsearch foselasticabundle

我在获取所需的elasticsearch结果时遇到问题。

我的映射如下:

"mappings": {
    "product": {
        "_meta": {
            "model": "App\\Entity\\Product"
        },
        "dynamic_date_formats": [],
        "properties": {
            "articleNameSearch": {
                "type": "text",
                "analyzer": "my_analyzer"
            },
            "articleNumberSearch": {
                "type": "text",
                "fielddata": true
            },
            "brand": {
                "type": "nested",
                "properties": {
                    "name": {
                        "type": "text"
                    }
                }
            }
        }
    }
},

我的设置:

"settings": {
    "index": {
        "number_of_shards": "5",
        "provided_name": "my_index",
        "creation_date": "1572252785482",
        "analysis": {
            "filter": {
                "standard": {
                    "type": "standard"
                }
            },
            "analyzer": {
                "my_analyzer": {
                    "filter": [
                        "standard"
                    ],
                    "type": "custom",
                    "tokenizer": "lowercase"
                }
            }
        },
        "number_of_replicas": "1",
        "uuid": "bwmc7NZ9RXqB1lpQ3e8HTQ",
        "version": {
            "created": "5060399"
        }
    }
}

内部数据:

"hits": [
  {
    "_index": "my_index",
    "_type": "product",
    "_id": "14",
    "_score": 1.0,
    "_source": {
      "articleNumberSearch": "5003xx843",
      "articleNameSearch": "this is a test string",
      "brand": {
        "name": "Brand name"
      }
    }
},

当前查询的PHP代码如下(不会返回正确的记录):

$searchQuery = new BoolQuery();
$formattedQuery = "*" . str_replace(['.', '|'], '', trim(mb_strtolower($query))) . "*"; 

/**
 * Test NGRAM analyzer
 */
$matchQuery = new Query\MultiMatch();
$matchQuery->setFields([
    'articleNumberSearch',
    'articleNameSearch',
]);
$matchQuery->setQuery($formattedQuery);
$searchQuery->addMust($matchQuery);


/**
 * Nested query
 */
$nestedQuery = new Nested();
$nestedQuery->setPath('brand');
$nestedQuery->setQuery(
    new Match('brand.name', 'Brand name')
);
$searchQuery->addMust($nestedQuery);

我正在创建并自动完成搜索字段,您可以在其中搜索articleNumberSearcharticleNameSearch,而品牌名称始终是固定值。

我希望能够搜索例如: 因为articleNumberSearch中有500个,所以500个会找到此匹配。

但是还可以搜索: this is string

夫妇问题:

  • 我需要使用哪个查询?
  • 我要使用正确的分析仪吗?
  • 分析仪配置正确吗?

1 个答案:

答案 0 :(得分:1)

您应该创建一个@Component public class RequestEntityPredicates { private final RequestHostelUtils requestHostelUtils; @Autowired public RequestEntityPredicates(RequestHostelUtils requestHostelUtils) { this.requestHostelUtils = requestHostelUtils; } } 类型的令牌生成器。

ngram令牌生成器在遇到指定字符列表之一时首先将文本分解为单词。

类似的东西:

ngram

NGram Tokenizer

enter image description here