1

I'm seeing different results from Model.search and SearchKick.search with a index_name.

I'm trying to apply apply a cross_fields query to multiple indexes. I got some strange results so I started sanity checking the query against a single index and in that scenario I'm getting sero results returned despite the searchkick query object being identical as far as I can tell

cross_field_query = {
        body: {
            query: {
               multi_match: {
                            query: query,
                            type: "cross_fields",
                            operator: "and"
                        }
                   }
          }
        }
  results = TaggedTree.search cross_field_query
  results.total_count  -> 29
  results = Searchkick.search cross_field_query, index_name: [TaggedTree]
  results.total_count  -> 0

I ran the queries with execute: false and they seemed to be the same. Here are the to_curl outputs

results = TaggedTree.search advanced_query, execute: false
results.to_curl

"curl http://localhost:9200/tagged_trees_development/_search?pretty -H 'Content-Type: application/json' -d '{\"query\":{\"dis_max\":{\"queries\":[{\"multi_match\":{\"query\":\"{:body=\\u003e{:query=\\u003e{:multi_match=\\u003e{:query=\\u003e\\\"oak malahide\\\", :type=\\u003e\\\"cross_fields\\\", :operator=\\u003e\\\"and\\\"}}}}\",\"boost\":10,\"operator\":\"and\",\"analyzer\":\"searchkick_search\",\"fields\":[\"*.analyzed\"],\"type\":\"best_fields\"}},{\"multi_match\":{\"query\":\"{:body=\\u003e{:query=\\u003e{:multi_match=\\u003e{:query=\\u003e\\\"oak malahide\\\", :type=\\u003e\\\"cross_fields\\\", :operator=\\u003e\\\"and\\\"}}}}\",\"boost\":10,\"operator\":\"and\",\"analyzer\":\"searchkick_search2\",\"fields\":[\"*.analyzed\"],\"type\":\"best_fields\"}},{\"multi_match\":{\"query\":\"{:body=\\u003e{:query=\\u003e{:multi_match=\\u003e{:query=\\u003e\\\"oak malahide\\\", :type=\\u003e\\\"cross_fields\\\", :operator=\\u003e\\\"and\\\"}}}}\",\"boost\":1,\"operator\":\"and\",\"analyzer\":\"searchkick_search\",\"fuzziness\":1,\"prefix_length\":0,\"max_expansions\":3,\"fuzzy_transpositions\":true,\"fields\":[\"*.analyzed\"],\"type\":\"best_fields\"}},{\"multi_match\":{\"query\":\"{:body=\\u003e{:query=\\u003e{:multi_match=\\u003e{:query=\\u003e\\\"oak malahide\\\", :type=\\u003e\\\"cross_fields\\\", :operator=\\u003e\\\"and\\\"}}}}\",\"boost\":1,\"operator\":\"and\",\"analyzer\":\"searchkick_search2\",\"fuzziness\":1,\"prefix_length\":0,\"max_expansions\":3,\"fuzzy_transpositions\":true,\"fields\":[\"*.analyzed\"],\"type\":\"best_fields\"}}]}},\"timeout\":\"11s\",\"_source\":false,\"size\":10000,\"from\":0}'"

results = Searchkick.search index_advanced_query, index_name: TaggedTree, execute: false
results.to_curl

"curl http://localhost:9200/tagged_trees_development/_search?pretty -H 'Content-Type: application/json' -d '{\"query\":{\"dis_max\":{\"queries\":[{\"multi_match\":{\"query\":\"{:body=\\u003e{:query=\\u003e{:multi_match=\\u003e{:query=\\u003e\\\"oak malahide\\\", :type=\\u003e\\\"cross_fields\\\", :operator=\\u003e\\\"and\\\"}}}}\",\"boost\":10,\"operator\":\"and\",\"analyzer\":\"searchkick_search\",\"fields\":[\"*.analyzed\"],\"type\":\"best_fields\"}},{\"multi_match\":{\"query\":\"{:body=\\u003e{:query=\\u003e{:multi_match=\\u003e{:query=\\u003e\\\"oak malahide\\\", :type=\\u003e\\\"cross_fields\\\", :operator=\\u003e\\\"and\\\"}}}}\",\"boost\":10,\"operator\":\"and\",\"analyzer\":\"searchkick_search2\",\"fields\":[\"*.analyzed\"],\"type\":\"best_fields\"}},{\"multi_match\":{\"query\":\"{:body=\\u003e{:query=\\u003e{:multi_match=\\u003e{:query=\\u003e\\\"oak malahide\\\", :type=\\u003e\\\"cross_fields\\\", :operator=\\u003e\\\"and\\\"}}}}\",\"boost\":1,\"operator\":\"and\",\"analyzer\":\"searchkick_search\",\"fuzziness\":1,\"prefix_length\":0,\"max_expansions\":3,\"fuzzy_transpositions\":true,\"fields\":[\"*.analyzed\"],\"type\":\"best_fields\"}},{\"multi_match\":{\"query\":\"{:body=\\u003e{:query=\\u003e{:multi_match=\\u003e{:query=\\u003e\\\"oak malahide\\\", :type=\\u003e\\\"cross_fields\\\", :operator=\\u003e\\\"and\\\"}}}}\",\"boost\":1,\"operator\":\"and\",\"analyzer\":\"searchkick_search2\",\"fuzziness\":1,\"prefix_length\":0,\"max_expansions\":3,\"fuzzy_transpositions\":true,\"fields\":[\"*.analyzed\"],\"type\":\"best_fields\"}}]}},\"timeout\":\"11s\",\"_source\":false,\"size\":10000,\"from\":0}'"

searchkick version: 3.1.2 Elastic Search Version: Version: 6.5.2, Build: default/tar/9434bed/2018-11-29T23:58:20.891072Z, JVM: 1.8.0_20

Any help at all with getting to the bottom of this would be greatly appreciated

Conor
  • 775
  • 3
  • 10
  • 23
  • what happens if you try `Searchkick.search cross_field_query` without index name? – lacostenycoder Mar 11 '19 at 22:20
  • @lacostenycoder thanks for replying. It seems to just hit the first index but I do get a set of results. The issue is that ultimately I would like to run the query across two indexes, I can't find anything in the searchkick docs about where else to specify the set of indexes to be targeted – Conor Mar 12 '19 at 09:12
  • how is `query: query,` the value of query defined? – lacostenycoder Mar 12 '19 at 14:35
  • it's just a string, so something like "baked potatoes", 'restaurants london' etc. – Conor Mar 12 '19 at 16:38

1 Answers1

0

I'm not sure you can search across multiple indices with Searchkick, see this issue https://github.com/ankane/searchkick/issues/744

But you can do stuff like:

Searchkick.search "milk", index_name: [Product, Category]

However you can probably get it to work with some monkey patching if you have a look here elasticsearch - querying multiple indexes is possible?

lacostenycoder
  • 10,623
  • 4
  • 31
  • 48
  • @lacotenycoder thanks for you reply. I really appreciate it. Yeah, I have been using the multi-index query but I wanted to move to using cross_fields to improve the results. I thought reading the docs that it should be possible to pass in query body and specify the indexes but it seems not. Maybe I should look at using the elasticsearch-ruby client directly – Conor Mar 12 '19 at 14:48
  • @Conor yeah worth a shot. You using rails or not? – lacostenycoder Mar 12 '19 at 21:58