Manybubbles has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/135023

Change subject: Fix phrase_as_terms on different fields
......................................................................

Fix phrase_as_terms on different fields

Fixes a problem where if some fields set phrase_as_terms and some don't then
the first field to be highlighted "wins".

Change-Id: I8674d65e085a691aeacc1ba52f62576028f04dc8
---
M 
experimental-highlighter-elasticsearch-plugin/src/main/java/org/elasticsearch/search/highlight/ExperimentalHighlighter.java
M 
experimental-highlighter-elasticsearch-plugin/src/test/java/org/wikimedia/highlighter/experimental/elasticsearch/ExperimentalHighlighterTest.java
2 files changed, 68 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/search/highlighter 
refs/changes/23/135023/1

diff --git 
a/experimental-highlighter-elasticsearch-plugin/src/main/java/org/elasticsearch/search/highlight/ExperimentalHighlighter.java
 
b/experimental-highlighter-elasticsearch-plugin/src/main/java/org/elasticsearch/search/highlight/ExperimentalHighlighter.java
index 3fc545c..b323da4 100644
--- 
a/experimental-highlighter-elasticsearch-plugin/src/main/java/org/elasticsearch/search/highlight/ExperimentalHighlighter.java
+++ 
b/experimental-highlighter-elasticsearch-plugin/src/main/java/org/elasticsearch/search/highlight/ExperimentalHighlighter.java
@@ -55,27 +55,28 @@
                 entry = new CacheEntry();
                 context.hitContext.cache().put(CACHE_KEY, entry);
             }
-            BasicQueryWeigher weigher = 
entry.queryWeighers.get(context.query.originalQuery());
+            boolean phraseAsTerms = false;
+            if (context.field.fieldOptions().options() != null) {
+                Boolean phraseAsTermsOption = (Boolean) 
context.field.fieldOptions().options()
+                        .get("phrase_as_terms");
+                if (phraseAsTermsOption != null) {
+                    phraseAsTerms = phraseAsTermsOption;
+                }
+            }
+            QueryCacheKey key = new 
QueryCacheKey(context.query.originalQuery(), phraseAsTerms);
+            BasicQueryWeigher weigher = entry.queryWeighers.get(key);
             if (weigher == null) {
                 // TODO recycle. But addReleasble doesn't seem to close it
                 // properly later. I believe this is fixed in later
                 // Elasticsearch versions.
                 BytesRefHashTermInfos infos = new 
BytesRefHashTermInfos(BigArrays.NON_RECYCLING_INSTANCE);
 //                context.context.addReleasable(infos);
-                boolean phraseAsTerms = false;
-                if (context.field.fieldOptions().options() != null) {
-                    Boolean phraseAsTermsOption = (Boolean) 
context.field.fieldOptions().options()
-                            .get("phrase_as_terms");
-                    if (phraseAsTermsOption != null) {
-                        phraseAsTerms = phraseAsTermsOption;
-                    }
-                }
                 weigher = new BasicQueryWeigher(
                         new ElasticsearchQueryFlattener(100, phraseAsTerms), 
infos,
                         context.hitContext.topLevelReader(), 
context.query.originalQuery());
                 // Build the QueryWeigher with the top level reader to get all
                 // the frequency information
-                entry.queryWeighers.put(context.query.originalQuery(), 
weigher);
+                entry.queryWeighers.put(key, weigher);
             }
             HighlightExecutionContext executionContext = new 
HighlightExecutionContext(context,
                     weigher);
@@ -91,7 +92,44 @@
     }
 
     static class CacheEntry {
-        private final Map<Query, BasicQueryWeigher> queryWeighers = new 
HashMap<Query, BasicQueryWeigher>();
+        private final Map<QueryCacheKey, BasicQueryWeigher> queryWeighers = 
new HashMap<QueryCacheKey, BasicQueryWeigher>();
+    }
+
+    static class QueryCacheKey {
+        private final Query query;
+        private final boolean phraseAsTerms;
+        public QueryCacheKey(Query query, boolean phraseAsTerms) {
+            this.query = query;
+            this.phraseAsTerms = phraseAsTerms;
+        }
+
+        @Override
+        public int hashCode() {
+            final int prime = 31;
+            int result = 1;
+            result = prime * result + (phraseAsTerms ? 1231 : 1237);
+            result = prime * result + ((query == null) ? 0 : query.hashCode());
+            return result;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj)
+                return true;
+            if (obj == null)
+                return false;
+            if (getClass() != obj.getClass())
+                return false;
+            QueryCacheKey other = (QueryCacheKey) obj;
+            if (phraseAsTerms != other.phraseAsTerms)
+                return false;
+            if (query == null) {
+                if (other.query != null)
+                    return false;
+            } else if (!query.equals(other.query))
+                return false;
+            return true;
+        }
     }
 
     static class HighlightExecutionContext {
diff --git 
a/experimental-highlighter-elasticsearch-plugin/src/test/java/org/wikimedia/highlighter/experimental/elasticsearch/ExperimentalHighlighterTest.java
 
b/experimental-highlighter-elasticsearch-plugin/src/test/java/org/wikimedia/highlighter/experimental/elasticsearch/ExperimentalHighlighterTest.java
index 0e357e9..71901b2 100644
--- 
a/experimental-highlighter-elasticsearch-plugin/src/test/java/org/wikimedia/highlighter/experimental/elasticsearch/ExperimentalHighlighterTest.java
+++ 
b/experimental-highlighter-elasticsearch-plugin/src/test/java/org/wikimedia/highlighter/experimental/elasticsearch/ExperimentalHighlighterTest.java
@@ -109,6 +109,25 @@
         }
     }
 
+    @Test
+    public void phraseAsTermsSwitch() throws IOException {
+        buildIndex();
+        client().prepareIndex("test", "test", "1").setSource("test", "phrase 
test test", "test2", "phrase phrase test").get();
+        refresh();
+
+        Map<String, Object> options = new HashMap<String, Object>();
+        SearchRequestBuilder search = testSearch(matchPhraseQuery("test", 
"phrase test"))
+                .addHighlightedField(new 
HighlightBuilder.Field("test2").options(options));
+        options.put("phrase_as_terms", true);
+        for (String hitSource : HIT_SOURCES) {
+            SearchResponse response = setHitSource(search, hitSource).get();
+            assertHighlight(response, 0, "test", 0,
+                    equalTo("<em>phrase</em> <em>test</em> test"));
+            assertHighlight(response, 0, "test2", 0,
+                    equalTo("<em>phrase</em> <em>phrase</em> <em>test</em>"));
+        }
+    }
+
     /**
      * Makes sure we skip fields if the query is just phrases and none of them 
are on the field.
      */

-- 
To view, visit https://gerrit.wikimedia.org/r/135023
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8674d65e085a691aeacc1ba52f62576028f04dc8
Gerrit-PatchSet: 1
Gerrit-Project: search/highlighter
Gerrit-Branch: master
Gerrit-Owner: Manybubbles <never...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to