0

I am trying to build a small web application to demo elasticsearch's capabilities, but I am running into an issue with my query. My goal is to search a string of keywords over all indexes and fields. What I currently have seems to only search AttachmentBody, as when I search for BugID xyz or AttachmentTitle xyz nothing is displayed. I would greatly appreciate any help you may have to offer!

<?php

require_once 'es/esconnect.php';

if (isset($_GET['q'])){
    $q = $_GET['q'];
    $query = $Client->search([
        'body' =>[
            'query' =>[
                'bool' =>[
                    'should' =>[
                        'match' => ['BugID' => $q],
                        'match' => ['AttachmentTitle' => $q],
                        'match' => ['AttachmentBody' => $q]
                    ]
                ]
            ]
        ]
    ]);

    if($query['hits']['total'] >=1){
        $results = $query['hits']['hits'];
    }
}

?>

BugID: CSCzo56214

AttachmentTitle: 15624_note_21844

AttachmentBody: this is a note


BugID: CSCzo56214

AttachmentTitle: 15624_description_21846

AttachmentBody: this is a description


Working query:

    'body' =>[
        'query' =>[
            'bool' =>[
                'should' => array(
        array('match' => array('BugID' => $q)),
        array('match' => array('AttachmentTitle' => $q)),
        array('match' => array('AttachmentBody' => $q))
    )
8
  • 2
    Welcome to SO. Please consider adding a Minimal, Complete, and Verifiable example so we can better help you. So if you can share a sample document that should match as well as what $q would be, it'll be easier for people to help. Commented Jan 26, 2016 at 3:23
  • Can you maybe show a sample document with sample values? Commented Jan 26, 2016 at 13:15
  • The inner-most array only consists of one element (key: match, value: ['AttachmentBody' => $q]), because each consecutive mention of the same key, overwrites the previous. Without knowing elasticsearch, my best guess is that you want something like: 'match' => ['BugID' => $q, 'AttachmentTitle' => $q, 'AttachmentBody' => $q]. Commented Jan 26, 2016 at 13:26
  • Hi Val, sure. I added it to the initial post. Thanks for the help! Yoshi, I thought that would be the problem too but it throws an exception: [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?" Commented Jan 26, 2016 at 14:58
  • As I said, unfortunately I don't have any experience with elasticsearch. But I'm pretty sure, that you simply have to find the correct query-structure (as elasticsearch requires it). Commented Jan 26, 2016 at 15:10

1 Answer 1

1

Functioning query:

'body' => [
    'query' => [
        'bool' => [
            'should' => [
                ['match' => ['BugID'           => $q]],
                ['match' => ['AttachmentTitle' => $q]],
                ['match' => ['AttachmentBody'  => $q]]
            ]
        ]
    ]
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.