当前位置: 首页 > 知识库问答 >
问题:

弹性搜索中如何进行多字段匹配

颜志业
2023-03-14
use Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()->build();

$params = [
    'index' => 'my_deezer',
    'type' => 'song',
    'body' => [
        'query' => [
            'match' => [
                'artist' => 'blah',
                'year' => 2004
            ]   
        ]
    ]
];

try {
   $response = $client->search($params); 
    print_r($response);
} catch (Exception $e) {
    echo $e->getMessage();
}

//错误

SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[hIY53G4ySJm0xQz0Lpqjxg][my_deezer][0]: SearchParseException[[my_deezer][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"match":{"artist":"jal","year":2001}}}]]]; nested: QueryParsingException[[my_deezer] [match] query parsed in simplified form, with direct field name, but included more options than just the field name, possibly use its 'options' form, with 'query' element?]; }{[hIY53G4ySJm0xQz0Lpqjxg][my_deezer][1]: SearchParseException[[my_deezer][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"match":{"artist":"jal","year":2001}}}]]]; nested: QueryParsingException[[my_deezer] [match] query parsed in simplified form, with direct field name, but included more options than just the field name, possibly use its 'options' form, with 'query' element?]; }{[hIY53G4ySJm0xQz0Lpqjxg][my_deezer][2]: SearchParseException[[my_deezer][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"match":{"artist":"jal","year":2001}}}]]]; nested: QueryParsingException[[my_deezer] [match] query parsed in simplified form, with direct field name, but included more options than just the field name, possibly use its 'options' form, with 'query' element?]; }{[hIY53G4ySJm0xQz0Lpqjxg][my_deezer][3]: SearchParseException[[my_deezer][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"match":{"artist":"jal","year":2001}}}]]]; nested: QueryParsingException[[my_deezer] [match] query parsed in simplified form, with direct field name, but included more options than just the field name, possibly use its 'options' form, with 'query' element?]; }{[hIY53G4ySJm0xQz0Lpqjxg][my_deezer][4]: SearchParseException[[my_deezer][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"match":{"artist":"jal","year":2001}}}]]]; nested: QueryParsingException[[my_deezer] [match] query parsed in simplified form, with direct field name, but included more options than just the field name, possibly use its 'options' form, with 'query' element?]; }]

共有1个答案

颜均
2023-03-14

您有两个条件,您可以使用bool必须查询,查询将

{

  "query": {
  "bool" : {
    "should" : [
            {
                "term" : { "artist" : "blah" }
            },
            {
                "term" : { "year" : "2004" }
            }
        ]
  }
}

第二部分是范围查询,用于获取大于小于的结果

 {
    "range" : {
           "age" : { "from" : 10, "to" : 20 }
       }
 }

{
    "range" : {
        "age" : {
            "gte" : 10,
            "lte" : 20
        }
    }
}
 类似资料:
  • 我还没有为索引创建任何显式设置。谢了。

  • 我使用Elasticsearch允许用户输入要搜索的术语。例如,我要搜索以下属性'name': 如果使用以下代码搜索或,我希望返回此文档。 我尝试过做一个bool must和做多个术语,但它似乎只有在整个字符串都匹配的情况下才起作用。 所以我真正想做的是,这个词是否以任何顺序包含两个词。 有人能帮我走上正轨吗?我已经在这上面砸了一段时间了。

  • 我已经完成了名称索引。它包含椅子、椅子等数据。 当我尝试用“cha”搜索时,它不会返回任何椅子。这是返回“毛毯”,我期待的文件有椅子。 下面是我的代码: 搜索请求: 搜索响应: } 映射: 我该怎么解决这个问题? 谢谢,Sri

  • 在弹性搜索中,我将记录存储在命名空间和主题中。这些是简单的对象,只有(string)和。 我想通过自动完成搜索名称,为此我试图使用索引搜索。 ElasticSearch::Transport::Transport::Transport::Errors::BadRequest:[400]{“error”:{“root_cause”:[{“type”:“illegal_argument_excepti

  • 我在这里遵循这个例子: https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html 以下查询: 与预期的一条记录匹配。假设我只想返回用户为“约翰·史密斯”和“艾利斯·怀特”的文档。 我试过: 但这返回零结果。我如何获得同时包含“Alice White”和“John Smith”的文档(这应该只是在原始结果中