1

I want to create top search query using elasticsearch.

I want to match category_name, brand_name and title from elasticsearch table. The match should be phrase and with or condition.

My query:

$query = [
  "bool" => [
    "must" => [
      ["match" => ["is_published" => 1]],
      [
        "multi_match" => [
          "query" => $searchKey,
          "fields" => ["category_name", "brand_name", "title"]
        ]
      ],
      [
        'nested' => [
          'path' => 'category_with_in_title.parent_cat',
          'query' => [
            'bool' => [
              'must' => [

                ['match' => [
                  'category_with_in_title.parent_cat.status' => 1,
                ]],
              ],
            ],
          ],
        ],
      ],
      [
        'nested' => [
          'path' => 'brand',
          'query' => [
            'bool' => [
              'must' => [
                'match' => [
                  'brand.approved_status' => 1,
                ],
              ],
            ],
          ],
        ],
      ],
      [
        'nested' => [
          'path' => 'default_product_low_price_with_seller.seller_detail',
          'query' => [
            'bool' => [
              'must' => [
                'match' => [
                  'default_product_low_price_with_seller.seller_detail.approve_status' => 1,
                ],
              ],
            ],
          ],
        ],

      ],
      [
        'nested' => [
          'path' => 'default_product_low_price_with_seller',
          'query' => [
            'bool' => [
              'must' => [
                'match' => [
                  'default_product_low_price_with_seller.status' => 1,
                ],
              ],
            ],
          ],
        ],
      ],
    ],

  ],
];

I use multi_match for that but how to use pharse in this query? I have to write whole word to search.

For example :

I want to search the record whose category_name = Tops

I want result if i write "tops" or "top" or "to". But right now I have to write "Tops" the exact word.

Thanks in advance...

2 Answers 2

1

You could use match_phrase_prefix, which is the same as match_phrase, except that it allows for prefix matches on the last term in the text.

All that you need to do, is add "type": "phrase_prefix" to your multi_match query, like this:

"multi_match" => [
  "query" => $searchKey,
  "type": "phrase_prefix",
  "fields" => ["category_name", "brand_name", "title"]
]

Let me know if this is what you're looking for.

Sign up to request clarification or add additional context in comments.

2 Comments

This approach has a limitation. Default max_expansions is 50 and increasing it might affect your query response time. Reference: elastic.co/guide/en/elasticsearch/guide/current/…
Thanks sir, but this is not working i am not getting any records
1

Ideally you should use multi_match with cross_fields type, which will treat all fields as ONE big field and run your query on it. However, due to fuzziness being disabled on these, only the exact match will show.

See Revelant Github Issue https://github.com/elastic/elasticsearch/issues/6866

Hence, my recommendation is to replace multi_match with fuzzy_like_this (elasticsearch version 1.x) or more_like_this (elasticsearch version 2.x).

$query = [
  "bool" => [
    "must" => [
      ["match" => ["is_published" => 1]],
      [
        "fuzzy_like_this" => [
            "fields" => ["category_name", "brand_name", "title"],
            "like_text" => $searchKey
        ]
      ],
      ....

6 Comments

Thanks sir it works but I have to write full word to search. For example i want to get records which category_name = Tops then i have to write 'tops' or 'top' to get that records. I am not get any record if I write 'to' (to search tops).
In the fuzzy_like_this array, add "fuzziness" => 3. This will increase the likelihood of matches. It may also point to the fact that your indexes may not be analyzed. see: elastic.co/guide/en/elasticsearch/guide/current/…
Sir may I use query without change the structure to analyzed ?
i want to search partial search from sentence but without change the structure
You'll have to rebuild your index from ground up if you switch the mapping around.
|

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.